Repository navigation
Expand file tree
/
Copy pathstack.ex
More file actions
342 lines (278 loc) · 6.66 KB
/
Copy pathstack.ex
File metadata and controls
342 lines (278 loc) · 6.66 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* Stack Programming */
-----------------------------------------------------------
include std/sequence.e
include std/get.e
include std/text.e
enum ADD, SUB, MUL, DIV
sequence WORDS =
{"SWAP", "DUP", "OVER", "ROT","DROP", "2SWAP",
"2DUP", "2OVER", "2DROP", ".S", "+", "-", "*", "/"}
sequence dictionary = {}
sequence stack = {}
integer stop = 0
-----------------------------------------------------------
--tests()
main()
-----------------------------------------------------------
procedure main()
puts(1," *** Stack Programming ***\n")
puts(1,"-----------------------------------------\n")
puts(1,"Show stack: '.s'. Exit: 'bye'.\n")
puts(1,"-----------------------------------------\n")
while 1 do
sequence line = gets(0)
processInput(upper(slice(line, ,length(line)-1)))
if stop then
exit
end if
end while
end procedure
procedure tests()
processInput(": RODUP ROT DUP ;")
processInput(": PRO SWAP RODUP SWAP RODUP SWAP ;")
processInput(": SFUNC + 2 * 66 ;")
processInput(": FNS SFUNC SFUNC ;")
puts(1,"Dictionary\n")
showDictionary()
puts(1,"Stack: ")
showStack()
processInput("5 6 SFUNC +")
puts(1,"Stack: ")
showStack()
end procedure
-----------------------------------------------------------
procedure addDefinition(sequence input)
-- Drop ':' and ';' symbols
input = tail(slice(input, ,length(input)-1))
-- First word is definition name
sequence newDefinition = {input[1]}
-- Add other words
for i=2 to length(input) do
sequence currentWord = input[i]
-- Simple word
if simpleWord(currentWord) then
newDefinition = append(newDefinition,currentWord)
else
-- Number
sequence maybeNumber = value(currentWord)
if maybeNumber[1] = GET_SUCCESS then
newDefinition = append(newDefinition,currentWord)
-- Previously defined word
else
sequence definition = getDefinition(currentWord)
if not equal(definition,{}) then
newDefinition = splice(newDefinition,definition,length(newDefinition)+1)
else
puts(1,"Error\n")
end if
end if
end if
end for
dictionary = append(dictionary,newDefinition)
end procedure
-- Execute sequence of simple words
procedure executeWords(sequence input)
for i=1 to length(input) do
sequence word = input[i]
if equal(word,"BYE") then
stop = 1
exit
else
execute(word)
end if
end for
end procedure
-- Check if user input is word definition
function isDefinition(sequence input)
return input[1][1] = 58 and input[length(input)][1] = 59
end function
-- Process word definition or sequence of words
procedure processInput(sequence input)
input = split(input)
if isDefinition(input) then
addDefinition(input)
else
executeWords(input)
end if
end procedure
-----------------------------------------------------------
procedure push(integer n)
stack = append(stack,n)
end procedure
procedure swap()
integer b = length(stack)
integer a = b-1
integer temp = stack[b]
stack[b] = stack[a]
stack[a] = temp
end procedure
procedure dup()
stack = append(stack,stack[length(stack)])
end procedure
procedure over()
stack = append(stack,stack[length(stack)-1])
end procedure
procedure rot()
integer c = length(stack)
integer b = c - 1
integer a = b - 1
integer temp = stack[a]
stack[a] = stack[b]
stack[b] = stack[c]
stack[c] = temp
end procedure
procedure drop()
integer le = length(stack)
if le = 1 then
stack = {}
else
stack = slice(stack, ,le-1)
end if
end procedure
-----------------------------------------------------------
procedure dup2()
for i=1 to 2 do
over()
end for
end procedure
procedure drop2()
for i=1 to 2 do
drop()
end for
end procedure
procedure swap2()
integer d = length(stack)
integer c = d - 1
integer b = c - 1
integer a = b - 1
integer t1 = stack[a]
integer t2 = stack[b]
stack[a] = stack[c]
stack[b] = stack[d]
stack[c] = t1
stack[d] = t2
end procedure
procedure over2()
integer a = length(stack)-3
integer b = a+1
stack = append(append(stack,stack[a]),stack[b])
end procedure
-----------------------------------------------------------
procedure add()
math(ADD)
end procedure
procedure sub()
math(SUB)
end procedure
procedure mul()
math(MUL)
end procedure
procedure div()
math(DIV)
end procedure
procedure math(integer op)
integer le = length(stack)
integer v = stack[le]
integer i = le-1
switch op do
case ADD then
stack[i] += v
case SUB then
stack[i] -= v
case MUL then
stack[i] *= v
case DIV then
stack[i] /= v
end switch
drop()
end procedure
-----------------------------------------------------------
procedure showStack()
integer le = length(stack)
printf(1,"<%d> ",le)
if le != 0 then
for i=1 to le do
printf(1,"%d ",stack[i])
end for
puts(1,"\n")
else
puts(1,"--\n")
end if
end procedure
procedure showDictionary()
for i=1 to length(dictionary) do
sequence definition = dictionary[i]
printf(1,"%s: ",{definition[1]})
for j=2 to length(definition) do
printf(1,"%s ",{definition[j]})
end for
puts(1,"\n")
end for
end procedure
function simpleWord(sequence w)
for i=1 to length(WORDS) do
if equal(w,WORDS[i]) then
return 1
end if
end for
return 0
end function
function getDefinition(sequence word)
for i=1 to length(dictionary) do
sequence definition = dictionary[i]
if equal(word,definition[1]) then
return tail(definition)
end if
end for
return {}
end function
procedure execute(sequence word)
-- Simple function
if simpleWord(word) then
switch word do
case "+" then
add()
case "-" then
sub()
case "*" then
mul()
case "/" then
div()
case ".S" then
showStack()
case "SWAP" then
swap()
case "DUP" then
dup()
case "OVER" then
over()
case "ROT" then
rot()
case "DROP" then
drop()
case "2SWAP" then
swap2()
case "2DUP" then
dup2()
case "2OVER" then
over2()
case "2DROP" then
drop2()
end switch
else
-- Defined word
sequence definition = getDefinition(word)
if not equal(definition,{}) then
executeWords(definition)
else
-- Number
sequence answer = value(word)
if answer[1] = GET_SUCCESS then
push(answer[2])
else
puts(1,"Wrong value\n")
end if
end if
end if
end procedure
-----------------------------------------------------------