changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > demo / tk.lisp

changeset 10: 79737134254d
parent: bebb76da449c
child: 1fedeaa5bfc5
author: ellis <ellis@rwest.io>
date: Fri, 12 May 2023 22:58:05 -0400
permissions: -rw-r--r--
description: messing with UI and rust codegen
1 (in-package :demo)
2 
3 (defvar *cargo-target* #p"/Users/ellis/dev/otom8/demo/target/")
4 
5 (defvar *rs-macros* nil)
6 
7 (defun mkstr (&rest args)
8  (with-output-to-string (s)
9  (dolist (a args) (princ a s))))
10 
11 (defun symb (&rest args)
12  (values (intern (apply #'mkstr args))))
13 
14 (defun random-id ()
15  (format NIL "~8,'0x-~8,'0x" (random #xFFFFFFFF) (get-universal-time)))
16 
17 (defun scan-dir (dir filename callback)
18  (dolist (path (directory (merge-pathnames (merge-pathnames filename "**/") dir)))
19  (funcall callback path)))
20 
21 (defun sbq-reader (stream sub-char numarg)
22  "The anaphoric sharp-backquote reader: #`((,a1))"
23  (declare (ignore sub-char))
24  (unless numarg (setq numarg 1))
25  `(lambda ,(loop for i from 1 to numarg
26  collect (symb 'a i))
27  ,(funcall
28  (get-macro-character #\`) stream nil)))
29 
30 (eval-when (:execute)
31  (set-dispatch-macro-character
32  #\# #\` #'demo:sbq-reader))
33 
34 ;;; RUST MACROS
35 (defmacro rs-find-dll (name &optional debug)
36  "Find the rust dll specified by NAME."
37  (cond
38  ((uiop:directory-exists-p (merge-pathnames *cargo-target* "release"))
39  `,(mkstr "./target/release/" name))
40  ((uiop:directory-exists-p (merge-pathnames *cargo-target* "debug"))
41  `,(mkstr "./target/debug/" name))
42  (t `(progn
43  ,(uiop:run-program '("cargo" "build" (unless debug "--release")) :output t)
44  (rs-find-dll ,name ,debug)))))
45 
46 (defun rs-mod-form (crate &optional mods pub)
47  "Generate a basic mod form (CRATE . [MODS] [PUB])"
48  `(,crate ,mods ,pub))
49 
50 (defmacro with-rs-env (imports &body body)
51  "Generate an environment for use within a Rust generator macro."
52  `(let ((imports ,(mapcar #'rs-mod-form imports)))
53  (format nil "~A~&~A" imports ',body)))
54 
55 (defun rs-use (crate &optional mods pub)
56  "Generate a single Rust use statement."
57  (concatenate
58  'string
59  (if pub "pub " "")
60  "use " crate "::{"
61  (cond
62  ((consp mods)
63  (reduce
64  (lambda (x y) (format nil "~A,~A" x y))
65  mods))
66  (t mods))
67  "};"))
68 
69 (defun rs-mod (mod &optional pub)
70  "Generate a single Rust mod statement."
71  (concatenate
72  'string
73  (if pub "pub " "")
74  "mod " mod ";"))
75 
76 (defun rs-imports (&rest imports)
77  "Generate a string of Rust 'use' statements."
78  (cond
79  ((consp imports)
80  (mapcar (lambda (x) (apply #'rs-use (apply #'rs-mod-form x))) imports))
81  (t imports)))
82 
83 (defmacro rs-extern-c-fn (name args &optional pub unsafe no-mangle &body body)
84  "Generate a Rust extern 'C' fn."
85  `(concatenate
86  'string
87  ,(when no-mangle (format nil "#[no_mangle]~&"))
88  ,(when pub "pub ")
89  ,(when unsafe "unsafe ")
90  "extern \"C\" fn " ,name "("
91  ,(cond
92  ((consp args) (reduce (lambda (x y) (format nil "~A,~A" x y)) args))
93  (t args))
94  ")" "{" ,@body "}"))
95 
96 (defun rs-obj-impl (obj)
97  "Implement Objective for give OBJ."
98  (format nil "impl Objective for ~A {};" obj))
99 
100 ;; (defun rs-macroexpand-1 (form &optional env))
101 
102 ;; (defun rs-macroexpand (env &rest body)
103 ;; "Cbindgen is quite the menace and really doesn't like our macros used
104 ;; to generate C FFI bindings. To compensate for this, we use a tool
105 ;; called cargo-expand by the most excellent dtolnay which expands Rust
106 ;; macros. The expansions are assembled into an equivalent Rust source
107 ;; file which cbindgen won't get stuck in an infinite compile loop on.")
108 
109 ;;;