fogus (owner)

Revisions

gist: 162727 Download_button fork
public
Public Clone URL: git://gist.github.com/162727.git
Embed All Files: show embed
defmacro.qi #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
\
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)