changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > demo / rs.lisp

changeset 14: 2bbf5ce73537
parent: 1fedeaa5bfc5
author: ellis <ellis@rwest.io>
date: Tue, 23 May 2023 20:43:02 -0400
permissions: -rw-r--r--
description: rs updates
1 ;;; RUST DSL
2 
3 ;; So basically, this was born out of personal frustration with how
4 ;; cbindgen and Rust macros work (they don't). Rust macros in general
5 ;; are something of a pain in my opinion, so I thought why not just
6 ;; generate Rust code from Lisp instead?
7 
8 (in-package :demo)
9 
10 (defvar *cargo-target* #p"/Users/ellis/dev/otom8/demo/target/")
11 (defvar *rs-macros* nil)
12 
13 ;; TODO gensyms
14 (defmacro rs-defmacro (name args &body body)
15  "Define a macro which can be used within the body of a 'with-rs' form."
16  `(prog1
17  (defmacro ,name ,@(mapcar #`(,a1) args) ,@body)
18  (push ',name *rs-macros*)))
19 
20 (defun rs-mod-form (crate &optional mods pub)
21  "Generate a basic mod form (CRATE . [MODS] [PUB])"
22  `(,crate ,mods ,pub))
23 
24 (defmacro with-rs-env (imports &body body)
25  "Generate an environment for use within a Rust generator macro."
26  `(let ((imports ,(mapcar #'rs-mod-form imports)))
27  (format nil "~A~&~A" imports ',body)))
28 
29 (defun rs-use (crate &optional mods pub)
30  "Generate a single Rust use statement."
31  (concatenate
32  'string
33  (if pub "pub " "")
34  "use " crate "::{"
35  (cond
36  ((consp mods)
37  (reduce
38  (lambda (x y) (format nil "~A,~A" x y))
39  mods))
40  (t mods))
41  "};"))
42 
43 (defun rs-mod (mod &optional pub)
44  "Generate a single Rust mod statement."
45  (concatenate
46  'string
47  (if pub "pub " "")
48  "mod " mod ";"))
49 
50 (defun rs-imports (&rest imports)
51  "Generate a string of Rust 'use' statements."
52  (cond
53  ((consp imports)
54  (mapcar (lambda (x) (apply #'rs-use (apply #'rs-mod-form x))) imports))
55  (t imports)))
56 
57 (defmacro rs-extern-c-fn (name args &optional pub unsafe no-mangle &body body)
58  "Generate a Rust extern 'C' fn."
59  `(concatenate
60  'string
61  ,(when no-mangle (format nil "#[no_mangle]~&"))
62  ,(when pub "pub ")
63  ,(when unsafe "unsafe ")
64  "extern \"C\" fn " ,name "("
65  ,(cond
66  ((consp args) (reduce (lambda (x y) (format nil "~A,~A" x y)) args))
67  (t args))
68  ")" "{" ,@body "}"))
69 
70 (defun rs-obj-impl (obj)
71  "Implement Objective for give OBJ."
72  (format nil "impl Objective for ~A {};" obj))
73 
74 ;; (defun rs-macroexpand-1 (form &optional env))
75 
76 ;; (defun rs-macroexpand (env &rest body)
77 
78 ;;;