-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrgmacro.sl
186 lines (168 loc) · 6.46 KB
/
grgmacro.sl
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
%==========================================================================%
% GRGmacro.sl Lisp Macro Functions %
%==========================================================================%
% GRG 3.2 Standard Lisp Source Code (C) 1988-96 Vadim V. Zhytnikov %
%==========================================================================%
% This file is distributed without any warranty. You may modify it but you %
% are not allowed to remove author's name and/or distribute modified file. %
%==========================================================================%
% Various macro for loops ...
% makes (cond ((not bool) (go lab)))
(de mkcng!> (bool lab)
(list2
(quote cond)
(list2
(list2 (quote not) bool)
(list2 (quote go) lab))))
% makes (cond (bool (go lab)))
(de mkcg!> (bool lab)
(list2
(quote cond)
(list2
bool
(list2 (quote go) lab))))
% proc - prog with while, repeat, loop, tohead and exitif
(dm proc (u)
(prog (body w wa wb wc)
(setq body (list2 (cadr u) (quote prog)))
(setq u (cddr u))
label1
(cond ((and (null u) (null wa)) (go label2)))
(cond ((null u) (go label3)))
(cond
((atom(car u)) (prog2 (setq body (cons (car u) body))
(setq u (cdr u))))
((or (setq wb (eq (caar u) (quote while!>)))
(eq (caar u) (quote loop!>))
(eq (caar u) (quote repeat!>)))
(progn
(setq wa (cons (cdr u) wa))
(setq u (cdar u))
(setq w (cons (gensym) w))
(setq w (cons (gensym) w))
(cond
(wb (setq body (cons (mkcng!> (car u) (car w))
(cons (cadr w) body))))
(t (setq body (cons (cadr w) body))))
(cond (wb (setq u (cdr u))))
(setq wc (cons nil wc))))
((eq (caar u) (quote exitif))
(prog2 (setq body (cons (mkcg!> (cadar u)(car w)) body))
(setq u (cdr u)) ))
((eq (caar u) (quote tohead))
(prog2 (setq body (cons (mkcg!> (cadar u)(cadr w)) body))
(setq u (cdr u)) ))
((eq (caar u) (quote until))
(progn
(setq body (cons (car w) (cons (mkcng!> (cadar u)(cadr w)) body)))
(setq u (cdr u))
(setq wc (cons t wc))))
(t (prog2 (setq body (cons (car u) body)) (setq u (cdr u)) )))
label3
(cond((and wa (null u))
(progn
(cond ((null (car wc))
(setq body (cons (car w)
(cons (list2 (quote go) (cadr w)) body)))))
(setq w (cddr w))
(setq u (car wa))
(setq wa (cdr wa))
(setq wc (cdr wc)))))
(go label1)
label2
(return (reversip body))))
(dm loop!> (u) (list (quote proc) nil (cons (quote loop!>) (cdr u))))
(dm while!> (u) (list (quote proc) nil (cons (quote while!>) (cdr u))))
(dm repeat!> (u) (list (quote proc) nil (cons (quote repeat!>) (cdr u))))
(dm for!> (u)
(prog (action body exp incr lab1 lab2 result tail var x)
(setq var (cadr u))
(setq incr (caddr u))
(setq action (cadddr u))
(setq body (car (cddddr u)))
(setq result (list (list 'setq var (car incr))))
(setq incr (cdr incr))
(setq x (list 'difference (cadr incr) var))
(cond
((not (equal (car incr) 1))
(setq x (list 'times (car incr) x))))
(setq lab1 (gensym))
(setq lab2 (gensym))
(setq x (list 'minusp x))
(setq result
(nconc
result
(cons
lab1
(cons
(list 'cond (list x (list 'go lab2)))
(cons
body
(cons
(list
'setq
var
(list 'plus2 var (car incr)) )
(cons (list 'go lab1) (cons lab2 tail)))) ))) )
(return (mkprog (cons var exp) result))))
(dm fordim!> (u)
(prog (action body exp incr lab1 lab2 result tail var x)
(setq var (cadr u))
(setq incr (list 0 1 '![dim1!]))
(setq action (caddr u))
(setq body (car (cdddr u)))
(setq result (list (list 'setq var (car incr))))
(setq incr (cdr incr))
(setq x (list 'difference (cadr incr) var))
(cond
((not (equal (car incr) 1))
(setq x (list 'times (car incr) x))))
(setq lab1 (gensym))
(setq lab2 (gensym))
(setq x (list 'minusp x))
(setq result
(nconc
result
(cons
lab1
(cons
(list 'cond (list x (list 'go lab2)))
(cons
body
(cons
(list
'setq
var
(list 'plus2 var (car incr)) )
(cons (list 'go lab1) (cons lab2 tail)))) ))) )
(return (mkprog (cons var exp) result))))
(dm foreach!> (u)
(prog (action body fn lst mod var)
(setq var (cadr u))
(setq u (cddr u))
(setq mod (car u))
(setq u (cdr u))
(setq lst (car u))
(setq u (cdr u))
(setq action (car u))
(setq u (cdr u))
(setq body (car u))
(setq fn
(cond
((eq action 'do) (cond ((eq mod 'in) 'mapc) (t 'map)))
((eq action 'conc)
(cond ((eq mod 'in) 'mapcan) (t 'mapcon)))
((eq action 'collect)
(cond ((eq mod 'in) 'mapcar) (t 'maplist)))
(t (rederr (list action "invalid in foreach statement")))) )
(return
(list
fn
lst
(list 'function (list 'lambda (list var) body)))) ))
%(dm signature!> (w) (list 'signature0!> (list 'quote (cdr w))))
%(dm off!> (w) (list 'off0!> (list 'quote (cdr w))))
%(dm on!> (w) (list 'on0!> (list 'quote (cdr w))))
%(dm package!> (w) (list 'package0!> (list 'quote (cdr w))))
%(dm synonymous!> (u) (list 'synonymous0!> (list 'quote (cdr u))))
%========== End of GRGmacro.sl ============================================%