Plaster

common-lisp
(defstruct (defcomponent-info (:constructor %make-defcomponent-info)) (process-reads (make-hash-table)) (process-writes (make-hash-table))) (defvar *defcomponent-info* (make-hash-table :test #'equalp)) (defun defcomponent-info (cid) (values (gethash cid *defcomponent-info*))) (defun (setf defcomponent-info) (info cid) (setf (gethash cid *defcomponent-info*) info)) (defun ensure-defcomponent-info (cid) (orf (defcomponent-info cid) (%make-defcomponent-info))) (defun clear-defcomponent-info () (setf *defcomponent-info* (make-hash-table :test #'equalp))) (defun process-reads (cid pid) (let ((info (ensure-defcomponent-info cid))) (values (gethash pid (defcomponent-info-process-reads info))))) (defun (setf process-reads) (signals cid pid) (let ((info (ensure-defcomponent-info cid))) (setf (gethash pid (defcomponent-info-process-reads info)) signals))) (defun process-writes (cid pid) (let ((info (ensure-defcomponent-info cid))) (values (gethash pid (defcomponent-info-process-writes info))))) (defun (setf process-writes) (signals cid pid) (let ((info (ensure-defcomponent-info cid))) (setf (gethash pid (defcomponent-info-process-writes info)) signals))) (defun make-process-lambda (cid pid body signals) (alexandria:with-gensyms (read-sym) `(lambda () (macrolet ((,read-sym (sym) (pushnew sym (process-reads ',cid ',pid)) sym) (<- (sym value) (pushnew sym (process-writes ',cid ',pid)) `(%<- ',sym ,value))) (symbol-macrolet ,(mapcar (lambda (s) `(,s (,read-sym ,s))) signals) ,@body))))) (defmacro defcomponent (name pins &body options) (let* ((cid (gensym)) (wires (assoc-collect :signals options)) (signals (append pins wires)) (processes (assoc-value-all :process options)) (connections (assoc-value-all :<= options)) (subcomponents (assoc-collect :map options)) (process-function-exprs (mapcar (lambda (p) (let* ((pid (gensym)) (process-function-sym (gensym)) (name (car p)) (body (cdr p))) `(let ((,process-function-sym ;; The process-lambda returned my MAKE-PROCESS-LAMBDA ;; and interpolated within this expansion will be ;; compiled/expanded on another "pass". That's when the ;; information is generated. ,(make-process-lambda cid pid body signals))) ;; I want to use the computed information here, as arguments ;; to :reads and :writes. (list 'process-spec :name ,name :body ,process-function-sym ;; ,(process-reads cid pid) ;; (process-reads ',cid ',pid) :reads |???| ;; ,(process-writes cid pid))))) ;; (process-writes ',cid ',pid) :writes |???| processes))) `(ensure-component-spec ',name :pins ',pins :wires ',wires :connections ',connections :subcomponents ',subcomponents :processes (list ,@process-function-exprs))))