-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrgxmacr.sl
230 lines (213 loc) · 8.25 KB
/
grgxmacr.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
%==========================================================================%
% GRXmacro.sl Macro Expansion %
%==========================================================================%
% GRG 3.2 Standard Lisp Source Code (C) 1988-97 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. %
%==========================================================================%
(de expand!-file!> (ifile ofile)
(prog (ic oc w is os)
(setq ic (open ifile 'input))
(setq oc (open ofile 'output))
(terpri)
(prin2 "### Expanding `")(prin2 ifile)(prin2 "' to `")(prin2 ofile)(prin2 "' ...")(terpri)
(setq is (rds ic))
(setq os (wrs oc))
(prin2 "%==========================================================================%")(terpri)
(prin2 "% GRG 3.2 Standard Lisp Source Code (C) 1988-97 Vadim V. Zhytnikov %")(terpri)
(prin2 "%==========================================================================%")(terpri)
(prin2 "% This file is distributed without any warranty. You may modify it but you %")(terpri)
(prin2 "% are not allowed to remove author's name and/or distribute modified file. %")(terpri)
(prin2 "%==========================================================================%")(terpri)
(prin2 "% The file `")(prin2 ofile)(prin2 "' was generated from `")(prin2 ifile)(prin2 "' at ")
(terpri)
(prin2 "% ")(prin2 (date))
(terpri)
(prin2 "% Must be used on a ")
(cond ((getd 'rdf) (prin2 "CSL-based"))
(t (prin2 "PSL-based")))
(terpri)
(cond ((getd '!c!a!r) (prin2 "% Lower-Case system only!"))
(t (prin2 "% Upper-Case system only!")))
(terpri)
(terpri)
(terpri)
loop
(setq w (errorset '(read) nil nil))
(cond ((or (atom w) (eq (car w) !$eof!$)) (go end)))
(print (xpand!> (car w)))
(terpri)
(go loop)
end
(prin2 "%======== End of `")(prin2 ofile)(prin2 "' =============================================%")
(terpri)
(wrs os)
(rds is)
(close ic)
(close oc)
))
(de xpand!> (w)
(cond ((atom w) w)
((and (eq (car w) 'explode2) % CSL explode2 is buggy!
(getd 'rdf))
(list 'explode2!> (xpand!> (cadr w))))
((eq (car w) 'proc) (xproc w))
((eq (car w) 'loop!>) (xloop!> w))
((eq (car w) 'while!>) (xwhile!> w))
((eq (car w) 'repeat!>) (xrepeat!> w))
((eq (car w) 'for!>) (xfor!> w))
((eq (car w) 'fordim!>) (xfordim!> w))
((eq (car w) 'foreach!>) (xforeach!> w))
(t (cons (xpand!>(car w)) (xpand!>(cdr w))))))
(de mkcng!> (bool lab)
(list2
(quote cond)
(list2
(list2 (quote not) bool)
(list2 (quote go) lab))))
(de mkcg!> (bool lab)
(list2
(quote cond)
(list2
bool
(list2 (quote go) lab))))
(de xproc (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 (xpand!>(reverse body)))))
(de xloop!> (u) (xproc (list (quote proc) nil (cons (quote loop!>) (cdr u)))))
(de xwhile!> (u) (xproc (list (quote proc) nil (cons (quote while!>) (cdr u)))))
(de xrepeat!> (u) (xproc (list (quote proc) nil (cons (quote repeat!>) (cdr u)))))
(de xfor!> (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 (xpand!>(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))))
(de xfordim!> (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 (xpand!>(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))))
(de xforeach!> (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 (xpand!>(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)))) ))
%========== End of GRXmacro.sl ============================================%