From 7820d5529be81a25b5283c184d3b8e72c1854b19 Mon Sep 17 00:00:00 2001 From: mcgirjau Date: Tue, 14 Jul 2020 00:14:04 -0400 Subject: [PATCH] Clean up macros --- src/propeller/push/utils/macros.cljc | 32 ++++++++++++++++------------ 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/propeller/push/utils/macros.cljc b/src/propeller/push/utils/macros.cljc index d9045af..53a0f5c 100644 --- a/src/propeller/push/utils/macros.cljc +++ b/src/propeller/push/utils/macros.cljc @@ -1,13 +1,22 @@ (ns propeller.push.utils.macros (:require [propeller.push.core :as push] - [propeller.push.state :as state] [propeller.push.utils.helpers :refer [get-vector-literal-type]])) ;; Defines a Push instruction as a keyword-function pair, and adds it to the ;; instruction table (defmacro def-instruction [instruction definition] - `(swap! push/instruction-table assoc '~instruction ~definition)) + `(swap! push/instruction-table assoc ~instruction ~definition)) + +;; Given a generic function, e.g. _dup, and a stack type to instantiate it for, +;; e.g. :char, returns the appropriate stack metadata for that function instance +(defmacro make-metadata + [function stack] + `(->> (:stacks (meta ~function)) + (replace {:elem (get-vector-literal-type ~stack)}) + (cons ~stack) + set + (assoc-in (meta ~function) [:stacks]))) ;; Given a sequence of stacks, e.g. [:float :integer], and a sequence of suffix ;; function strings, e.g. [_add, _mult, _eq], automates the generation of all @@ -17,15 +26,10 @@ ;; instructions, the placeholder :elem will be replaced with the stack of the ;; corresponding element type (e.g. for :vector_integer, with :integer) (defmacro generate-instructions [stacks functions] - `(do ~@(for [stack stacks - func functions - :let [instruction-name (keyword (str (name stack) func)) - old-stack-data `(:stacks (meta ~func)) - vec-stack-data `(set - (replace - {:elem (get-vector-literal-type ~stack)} - ~old-stack-data)) - new-stack-data `(conj ~vec-stack-data ~stack) - metadata `(assoc-in (meta ~func) [:stacks] ~new-stack-data) - new-func `(with-meta (partial ~func ~stack) ~metadata)]] - `(def-instruction ~instruction-name ~new-func)))) + `(do + ~@(for [stack stacks + func functions + :let [instruction-name (keyword (str (name stack) func)) + metadata (make-metadata func stack) + new-func `(with-meta (partial ~func ~stack) ~metadata)]] + `(def-instruction ~instruction-name ~new-func))))