Created
November 1, 2012 17:31
-
-
Save oskarth/3995225 to your computer and use it in GitHub Desktop.
chipper.core
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn- arrow? | |
"predicate that returns true if x is =>" | |
[x] | |
(and (symbol? x) (= (name x) "=>"))) | |
(defn- split-body | |
"splits expression into two parts, one with input | |
pins and the other with => and outputs" | |
[body] | |
(split-with (complement arrow?) body)) | |
(defn expand-defgate | |
"recursively expands a gate with the right scoping" | |
[forms outs] | |
(if (empty? forms) | |
outs | |
(let [[[name & body] & rest] forms | |
[[args] [_ & [outputs]]] (split-body body)] | |
`(let [~(vec outputs) (~name ~@args)] | |
~(expand-defgate rest outs))))) | |
(defmacro defgate | |
"defines a logical gate with a name, input/output | |
pins, and a implementation consisting of other gates" | |
[gate ins _ outs & forms] | |
`(defn ~gate ~ins | |
~(expand-defgate forms outs))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment