Skip to content

Instantly share code, notes, and snippets.

@selfsame
Created December 11, 2014 22:41
Show Gist options
  • Save selfsame/86c01fc6c8876b37a405 to your computer and use it in GitHub Desktop.
Save selfsame/86c01fc6c8876b37a405 to your computer and use it in GitHub Desktop.
(ns sync.macros)
(defmacro to [chan value]
`(~'put! ~chan ~value))
'(macroexpand '(state ->e [x y] (prn x y)))
(defn walk [form]
(if (and (list? form) (< 1 (count form)))
(let [[a b c] form]
(if (and (= a '<-) (symbol? b))
(with-meta {b (walk c)} {:chan true})
form
))
form))
(defn recur-form [v]
(cond (:chan (meta v))
(list 'go (list 'let ['c (list '<! v)]))
:else (list 'recur v)))
(defmacro defmachine [args & body]
(let [hooks (mapv walk body)
chans (into {} (filter map? hooks))
alts (keys chans)
conds (cons 'cond (mapcat (fn [[k v]] [(list '= 'c k) (recur-form v)]) chans))
]
`(do
;(~'def ~sym (~'chan (async/sliding-buffer 1)))
(~'go
(~'loop ~args
(~'let [[~'v ~'c] (~'alts! [~@alts])]
~conds
))))))
'(macroexpand '(defmachine [ticker 0]
(<- reset 0)
(<- advance (do (inc ticker)
(prn ticker)
(put! advance true))
)))
'(macroexpand
'(defmachine [words []]
(<- string-reader [ch]
(if #{" "} ch)
(-> ws ch)
(-> non-ws ch))
(<- non-ws [s]
(let [word ])
(<- ws [_]))
(inc ticker)))
(comment
(do
(go
(loop [[ticker 0] nil]
(let [[v c] (alts! [reset pause])]
(cond [(= c reset) (recur 0)]
[(= c pause)
(go (let [c (<! {unpause nil})]))])
(<- reset 0)
(<- pause
(<- unpause))
(inc ticker))))))
;; (state []
;; (either
;; (mouse-move (ᓢ))
;; (mouse-down (…))
;; (mouse-move [x y]
;; (⊞ + x))
;; (mouse-up (ᓢ ⊞))))
(defmacro defstate [name state in & inputs]
(let [[sym init] state
pairs (mapv vec (partition 2 inputs))]
`(do
(~'def ~name (~'chan (async/sliding-buffer 100)))
(~'go
(~'loop [~sym ~init]
(~'prn ~sym)
(~'let [~in (~'<! ~name)]
(~'recur
(~'last
(~'first
(~'filter
(~'fn [[~'i ~'r]]
(~'or
(~'and (~'fn? ~'i)
(~'i ~in))
(~'= ~'i ~in)))
~pairs)))
)))))))
(eval '(+ 1 1))
(macroexpand
'(defstate timerj [t 0]
v
:reset 0
:pause (<! player)))
(defn -ogroup [form2]
(reduce
(fn [{:keys [stack cursor]} v]
(cond (= :v-par-start v) {:stack (conj stack []) :cursor (conj cursor 0)}
(= :v-par-end v) [stack] ;(first col)
:else {:stack (conj stack v) :cursor cursor}))
{:stack [] :cursor [0]} form2))
(defn -ooo [form]
(map
(fn [col]
(if (sequential? col) (-ooo col)
(case col
ᓢ :recur
… :...
⊞ :state
◦╱ :left
◦╲ :right
◦⋰ :...left
◦⋱ :...right
♘ :knight-b
♛ :queen-w
♒ :nile
ᔖ :sexp
╭╮ :v-par-start
╰╯ :v-par-end
◠ :v-par-start-small
◡ :v-par-end-small
△ :tri-up
▽ :tri-down
☲ :iching
☄ :comet
✩ :star
➶ :arrow-up
➷ :arrow-down
col))) form))
(defmacro ooo [state & more]
(let [body (-ogroup (reverse (-ooo more)))]
`(~'list ~@body)))
(macroexpand
'(ooo {} ╭╮
◦╱ ◦╲
◠ ◦⋰ ◦⋱
♒ ◠ ◠ (… ✩ ♘)
◡ ♘ ➶
☲ ♛
✩ ➷
◡ ◡ (… ✩ ♒ △)
╰╯ ⋱ ᓢ))
;❖
;➶
;➸
;➷
;ⶡ
;✈
⃞⃞;⊐
;⊹∺≙≟
;⋰
;⋱
;☾☉☽
;☭
;☹
;⑁⑂⑃⑄⑅⑆⑇
;♔♕♖♗♘♙♚♛♜♝♞♟2660♠♡♢♣♤♥♦♧♨♩♪♫♬
;♒⍡⍢
;╭╮╯╰╱╲╳
;▰▱▲△▴▵▶▷▸▹►▻▼▽▾▿25C0◀◁◂◃◄◅◆◇◈◉◊○◌◍◎●25D0◐◑◒◓◔◕◖◗◘◙◚◛◜◝◞◟25E0◠◡◢◣◤◥◦◧◨◩◪◫◬◭◮⏎
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment