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 |
3 (defvar *cargo-target* #p"/Users/ellis/dev/otom8/demo/target/") 5 (defvar *rs-macros* nil) 7 (defun mkstr (&rest args) 8 (with-output-to-string (s) 9 (dolist (a args) (princ a s)))) 11 (defun symb (&rest args) 12 (values (intern (apply #'mkstr args)))) 15 (format NIL "~8,'0x-~8,'0x" (random #xFFFFFFFF) (get-universal-time))) 17 (defun scan-dir (dir filename callback) 18 (dolist (path (directory (merge-pathnames (merge-pathnames filename "**/") dir))) 19 (funcall callback path))) 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 28 (get-macro-character #\`) stream nil))) 31 (set-dispatch-macro-character 32 #\# #\` #'demo:sbq-reader)) 35 (defmacro rs-find-dll (name &optional debug) 36 "Find the rust dll specified by NAME." 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)) 43 ,(uiop:run-program '("cargo" "build" (unless debug "--release")) :output t) 44 (rs-find-dll ,name ,debug))))) 46 (defun rs-mod-form (crate &optional mods pub) 47 "Generate a basic mod form (CRATE . [MODS] [PUB])" 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))) 55 (defun rs-use (crate &optional mods pub) 56 "Generate a single Rust use statement." 64 (lambda (x y) (format nil "~A,~A" x y)) 69 (defun rs-mod (mod &optional pub) 70 "Generate a single Rust mod statement." 76 (defun rs-imports (&rest imports) 77 "Generate a string of Rust 'use' statements." 80 (mapcar (lambda (x) (apply #'rs-use (apply #'rs-mod-form x))) imports)) 83 (defmacro rs-extern-c-fn (name args &optional pub unsafe no-mangle &body body) 84 "Generate a Rust extern 'C' fn." 87 ,(when no-mangle (format nil "#[no_mangle]~&")) 89 ,(when unsafe "unsafe ") 90 "extern \"C\" fn " ,name "(" 92 ((consp args) (reduce (lambda (x y) (format nil "~A,~A" x y)) args)) 96 (defun rs-obj-impl (obj) 97 "Implement Objective for give OBJ." 98 (format nil "impl Objective for ~A {};" obj)) 100 ;; (defun rs-macroexpand-1 (form &optional env)) 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.")