-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheval.ps
More file actions
322 lines (282 loc) · 6.79 KB
/
eval.ps
File metadata and controls
322 lines (282 loc) · 6.79 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/vars << >> def
/dictionary <<
(+) {{myop add}} (-) {{myop sub}} (*) {{myop mul}} (/) {{myop div}}
(not) {{getval not}} (>) {{myop gt}} (<) {{myop lt}} (>=) {{myop ge}}
(<=) {{myop le}} (=) {{myop eq}} (equal?) {{myop eq}} %(abs) {getVal {abs}}
(if) {{myif}} (quote) {{quote}} (define) {{mydef}} (set!) {{myset}}
(lambda) {lambda}
(begin) { prebegin {mybegin}} (equal?) {{eq}} (eq?) {{eq}}
(length) {{getVal length}} (cons) {{cons}} (car) {{getVal 0 get}}
(cdr) {{cdr}} (append) {{append}} (list) { list } (list?) {{isList}}
(null?) {{null}}
>> def
/append {
getVal exch getVal mark 3 1 roll % p1 p2 -- mark val2 val1
aload pop % mark val2 e1 e2 e3 ....
counttomark -1 roll % mark val1_e1 val1_e2 val1_e3 ... val2
aload pop % mark val1_e1 ... val2_e1 val2_e2 ...
]
} def
% ??? should be fixed with "nil" as parameter ???
/null {
getVal dup type [] type eq
{ length 0 eq }
{ pop false }
ifelse
} def
/isList {
getVal type [ ] type eq
} def
/cons {
mark 3 1 roll
exch getVal %first we have to getVal the first parameter (to convert it to a number)
exch getVal %then we have to getVal the list (maybe its a variable)
aload pop %store elements of the list on stack and pop list
] %create new list with the new element on the beginning
} def
/cdr {
mark exch %set mark on begin
getVal %get list if its a variable
aload pop %get only the items
counttomark -1 roll %get first item on top
pop ] %remove first item and store items into an array again
} def
/list {
] mark exch % make an array of all paramters
{
mark exch
{ getVal } forall
]
}
} def
/mybegin {
mark exch % set mark where this commands parameter begins
{ getVal } forall % evaluate each subexpression
] dup length 1 sub get % make array of all outputs and get last one
} def
/prebegin {
] mark exch % make an array of all parameters begin will get and recreate the used mark
} def
/eval % op p1..pn - evaluated
{
% ] dup /actual exch def aload length -1 roll % creates array, puts items behind it on stack, moves last arrayitem on top (operator)
counttomark -1 roll
dup dictionary exch known % test if command is known
{ dictionary exch get exec }
{
{ call_fun }
}
ifelse
%(asdfasdfasdf) print pstack
0 ] cvx dup dup length 1 sub {exec} putinterval
} def
/clearvars % clear var dict
{
/vars << >> def
} def
/myset { % myset chrashes when var is not defined or wrong type (currently not checked)
knownVar
{ vars exch get dup length 1 sub get } if
% { vars exch get } if
exch dup 3 1 roll mark exch % mark exch 3 -1 roll dup 4 1 roll
vars exch get aload pop pop
counttomark 2 add -1 roll getVal ]
vars 3 1 roll put
()
} def
/mydef {
knownVar
{ vars exch get dup length 1 sub get } if
exch dup 3 1 roll mark exch % mark exch 3 -1 roll dup 4 1 roll
knownVar
{ vars exch get aload pop pop }
{ pop }
ifelse
counttomark 2 add -1 roll ]
vars 3 1 roll put
()
} def
/myif % checks if the "if" has an else part and evaluates the correct output
{
3 -1 roll
exec
3 1 roll
dup type {} type ne
{ mark exch ] cvx }
if
exch
dup type {} type ne
{ mark exch ] cvx }
if
exch
ifelse
} def
/knownVar
{
dup vars exch known
{
dup vars exch get length 0 ne
}
{ false }
ifelse
} def
/call_fun {
/call exch
knownVar
{
vars exch get
dup length 1 sub get
exec_exp
}
{ exec_exp }
ifelse
} def
/exec_exp {
dup type {} type eq
{ exec }
{
dup dictionary exch known % test if command is known
{ dictionary exch get exec exec }
{ cvi }
ifelse
exch pop
}
ifelse
} def
/getVar {
vars exch get
dup length 1 sub get
dup type {} type eq
{ exec conv_str }
{ cvi }
ifelse
} def
/conv_str {
dup () ne exch dup type () type eq 3 -1 roll and
{
exch dup /call eq
{
pop % remove /call
dup dictionary exch known % test if command is known
{ dictionary exch get exec exec }
{ cvi }
ifelse
}
{ exch cvi }
ifelse
}
if
} def
/getVal % value or varname - value
{
knownVar
{ getVar }
{ dup type {} type eq
{ exec conv_str }
{
exch dup /call eq
{ pop }
{ exch }
ifelse
dup dictionary exch known % test if command is known
{ dictionary exch get exec exec }
{
dup /call eq
{ pop getVal }
{
dup type () type eq
{ cvi }
if
}
ifelse % remove /call
}
ifelse
}
ifelse
}
ifelse
} def
/myop % 2x geVal
{
getVal exch
getVal exch
} def
% -------------------- old ------------------
/myadd % evaluates output if 2 integers are given or an array for later evaluation (lambda functions)
{
2 copy % copy 2 parameter
isInteger exch isInteger and % check if both are integer
{ add } % add the 2 integers
{ mark {add} 4 2 roll ] } % create array for later evaluation
ifelse
} def
/mysub % evaluates output if 2 integers are given or an array for later evaluation (lambda functions)
{
2 copy % copy 2 parameter
isInteger exch isInteger and % check if both are integer
{ sub } % add the 2 integers
{ mark {sub} 4 2 roll ] } % create array for later evaluation
ifelse
} def
/isInteger % (param - bool) tests if param is from type integer
{
type 1 type eq % create "integertype" and compare
} def
/myif2 % checks if the "if" has an else part and evaluates the correct output
{
makeExec % top item is everytime true part of if
% checks if it is a if without else part
1 index dup true bool2token eq
exch false bool2token eq
or
{ % if without else part
exch token2bool exch % convert given token to bool
{ () }
ifelse
}
{ % if with else part
3 -1 roll token2bool 3 1 roll % convert given token to bool
exch makeExec exch % make false part executable
ifelse
}
ifelse
} def
/makeExec % (item - {item}) makes top item of stack to executable array
{
mark exch ] cvx
} def
/token2bool % (token - bool) false until tokenform of true given
{
true bool2token eq
} def
/bool2token % (bool - token) gives token form of true|false
{
{(true)}
{(false)}
ifelse
token pop exch pop
} def
/quote % exp - exp
{
} def
% /quote % cmd... - {cmd...}
% {
% actual cvx
% } def
%
% /quote % (p[0] ... p[n] - [ p[0] ... p[n] ])
% {
% actual length 1 sub dup dup % creates length of parameter
% [ exch % sets marker between the "lengths"
% 3 add 1 roll % set the marker behind all parameters
% {
% exch % get parameter count behind top element
%
% dup isInteger not % test if its not an integer
% { getTokenName } if % get name of token
%
% 1 index 1 add 1 roll % get element in last position
% } repeat % parameter count times
% pop % drop length parameter
% ] % create array so there is just 1 output element in eval
% } def