Clean up macros
This commit is contained in:
parent
3f5c2fd8a7
commit
7820d5529b
@ -1,13 +1,22 @@
|
|||||||
(ns propeller.push.utils.macros
|
(ns propeller.push.utils.macros
|
||||||
(:require [propeller.push.core :as push]
|
(:require [propeller.push.core :as push]
|
||||||
[propeller.push.state :as state]
|
|
||||||
[propeller.push.utils.helpers :refer [get-vector-literal-type]]))
|
[propeller.push.utils.helpers :refer [get-vector-literal-type]]))
|
||||||
|
|
||||||
;; Defines a Push instruction as a keyword-function pair, and adds it to the
|
;; Defines a Push instruction as a keyword-function pair, and adds it to the
|
||||||
;; instruction table
|
;; instruction table
|
||||||
(defmacro def-instruction
|
(defmacro def-instruction
|
||||||
[instruction definition]
|
[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
|
;; 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
|
;; 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
|
;; instructions, the placeholder :elem will be replaced with the stack of the
|
||||||
;; corresponding element type (e.g. for :vector_integer, with :integer)
|
;; corresponding element type (e.g. for :vector_integer, with :integer)
|
||||||
(defmacro generate-instructions [stacks functions]
|
(defmacro generate-instructions [stacks functions]
|
||||||
`(do ~@(for [stack stacks
|
`(do
|
||||||
|
~@(for [stack stacks
|
||||||
func functions
|
func functions
|
||||||
:let [instruction-name (keyword (str (name stack) func))
|
:let [instruction-name (keyword (str (name stack) func))
|
||||||
old-stack-data `(:stacks (meta ~func))
|
metadata (make-metadata func stack)
|
||||||
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)]]
|
new-func `(with-meta (partial ~func ~stack) ~metadata)]]
|
||||||
`(def-instruction ~instruction-name ~new-func))))
|
`(def-instruction ~instruction-name ~new-func))))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user