honeylisp/ssc/macros.fnl

32 lines
1.1 KiB
Fennel

; Sufficiently Simple Syntax macro
; This is basically just a quote that converts fennel syntax to Lua tables.
; Turns symbols into strings and lists into table literals. Table literals can be used to escape into regular fennel.
; Examples:
; (let (x 1 y 2) (+ "x" :y))
; => [:let [:x 1 :y 2] [:+ :x :y]]
; (form mymacro [(fn [ssc] (ssc:compile-expr [:+ 1 2]))])
; => [:form :mymacro (fn [ssc] (ssc:compile-expr [:+ 1 2]))]
(fn attach-metadata [form result]
(if (list? form)
(let [{: filename : line : bytestart : byteend} form]
`(setmetatable ,result ,{: filename : line : bytestart : byteend}))
result))
(fn form-to-fnl [form]
(attach-metadata form
(if (sym? form) (tostring form)
(sequence? form) (. form 1) ; escape
(list? form) (icollect [_ inner-form (ipairs form)] (form-to-fnl inner-form))
form)))
(fn sss [...]
(let [result `(values)]
(for [i 1 (select :# ...)]
(table.insert result (form-to-fnl (select i ...))))
result))
(fn compile [ssc ...] `(: ,ssc :compile ,(sss ...)))
{: sss : form-to-fnl : compile}