changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > demo / rs.lisp

changeset 13: 1fedeaa5bfc5
child: 2bbf5ce73537
author: ellis <ellis@rwest.io>
date: Fri, 19 May 2023 22:38:49 -0400
permissions: -rw-r--r--
description: tweaks to ffi and makefile
1 ;;; RUST DSL
2 (in-package :demo)
3 
4 (defvar *cargo-target* #p"/Users/ellis/dev/otom8/demo/target/")
5 (defvar *rs-macros* nil)
6 
7 (defun rs-mod-form (crate &optional mods pub)
8  "Generate a basic mod form (CRATE . [MODS] [PUB])"
9  `(,crate ,mods ,pub))
10 
11 (defmacro with-rs-env (imports &body body)
12  "Generate an environment for use within a Rust generator macro."
13  `(let ((imports ,(mapcar #'rs-mod-form imports)))
14  (format nil "~A~&~A" imports ',body)))
15 
16 (defun rs-use (crate &optional mods pub)
17  "Generate a single Rust use statement."
18  (concatenate
19  'string
20  (if pub "pub " "")
21  "use " crate "::{"
22  (cond
23  ((consp mods)
24  (reduce
25  (lambda (x y) (format nil "~A,~A" x y))
26  mods))
27  (t mods))
28  "};"))
29 
30 (defun rs-mod (mod &optional pub)
31  "Generate a single Rust mod statement."
32  (concatenate
33  'string
34  (if pub "pub " "")
35  "mod " mod ";"))
36 
37 (defun rs-imports (&rest imports)
38  "Generate a string of Rust 'use' statements."
39  (cond
40  ((consp imports)
41  (mapcar (lambda (x) (apply #'rs-use (apply #'rs-mod-form x))) imports))
42  (t imports)))
43 
44 (defmacro rs-extern-c-fn (name args &optional pub unsafe no-mangle &body body)
45  "Generate a Rust extern 'C' fn."
46  `(concatenate
47  'string
48  ,(when no-mangle (format nil "#[no_mangle]~&"))
49  ,(when pub "pub ")
50  ,(when unsafe "unsafe ")
51  "extern \"C\" fn " ,name "("
52  ,(cond
53  ((consp args) (reduce (lambda (x y) (format nil "~A,~A" x y)) args))
54  (t args))
55  ")" "{" ,@body "}"))
56 
57 (defun rs-obj-impl (obj)
58  "Implement Objective for give OBJ."
59  (format nil "impl Objective for ~A {};" obj))
60 
61 ;; (defun rs-macroexpand-1 (form &optional env))
62 
63 ;; (defun rs-macroexpand (env &rest body)
64 ;; "Cbindgen is quite the menace and really doesn't like our macros used
65 ;; to generate C FFI bindings. To compensate for this, we use a tool
66 ;; called cargo-expand by the most excellent dtolnay which expands Rust
67 ;; macros. The expansions are assembled into an equivalent Rust source
68 ;; file which cbindgen won't get stuck in an infinite compile loop on.")
69 
70 ;;;