Skip to content

Instantly share code, notes, and snippets.

@fogus
Created August 5, 2009 14:48
Show Gist options
  • Save fogus/162727 to your computer and use it in GitHub Desktop.
Save fogus/162727 to your computer and use it in GitHub Desktop.
\
This code using sugar functions to implement something more close
to lisp macros.
Example:
QI-SESSION
(32-) (defmacro plus
[X Y Z] -> [+ X Y Z])
>> macro-defined
(33-) (plus 1 2 3)
>> 6
END-QI-SESSION
Note, you may want to use the macro recursively by using apply
e.g. (apply plus [2 4 5]).
\
\the macro\
(define defmacro
[F | L] -> (do (eval (macroexpand [define F | L]))
(reg-qi-macro F)
macro-defined))
(define apply-macro
[F | L] -> (macroexpand (F L)) where (qi-macro? F)
X -> X)
\macro logistics\
(define qi-macro?
Symbol -> (if (symbol? Symbol)
(get-prop Symbol qi-macro false)
false))
(define reg-qi-macro
Symbol -> (put-prop Symbol qi-macro true))
(define clear-qi-macro
Symbol -> (put-prop Symbol qi-macro false))
(reg-qi-macro defmacro)
\macroexpand\
(define expand-atom
X -> (qi::user-syntax-in (qi::proc_specialforms X)))
(define tree-apply
F [] -> (F [])
F [G|X] -> (F (map (tree-apply F) [G|X]))
F X -> (F X))
(define macroexpand
X -> (tree-apply expand-atom X))
(sugar in apply-macro 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment