Skip to content

Instantly share code, notes, and snippets.

@oskarth
Created November 1, 2012 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oskarth/3995225 to your computer and use it in GitHub Desktop.
Save oskarth/3995225 to your computer and use it in GitHub Desktop.
chipper.core
(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