-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterpreter.cl
443 lines (400 loc) · 10.5 KB
/
interpreter.cl
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
(*
Program to simulate and interpret a Stack Machine
Sukrut Rao
CS15BTECH11036
Classes Stack and NonEmptyStack are based on the List and Cons classes from
the examples. Functions atoi, itoa, etc. are also based on the examples.
*)
(*
class for representing a Stack of strings
*)
class Stack inherits IO {
(*
to check if the stack is empty and return accordingly
the current class deals with empty stacks
return value: true
*)
isEmpty() : Bool {
true
};
(*
to push a value into the stack
if the string is empty, then nothing is pushed
parameters:
input - the string to be pushed
return value : the modified stack
*)
push(input : String) : Stack {
if not input = ""
then (new NonEmptyStack).init(input,self)
else self
fi
};
(*
to pop the value at the top of the stack
for the empty stack, the empty string is always returned
return value : ""
*)
pop() : String {
""
};
(*
to provide the string at the top of the stack
for the empty stack, the empty string is always returned
return value : ""
*)
head() : String {
""
};
(*
to provide the rest of the stack except the top value
for the empty stack, the stack itself is returned
return value : self
*)
tail() : Stack {
self
};
(*
to display the stack
for the empty stack, nothing is displayed
return value : self
*)
display() : Stack {
self
};
};
(*
to represent a non-empty node in a stack
*)
class NonEmptyStack inherits Stack {
-- the value at the top of the stack
topValue : String;
-- the rest of the stack except the top value
restOfStack : Stack;
(*
to provide the string at the top of the stack
return value : topValue
*)
head() : String {
topValue
};
(*
to provide the rest of the stack except the top value
return value : restOfStack
*)
tail() : Stack {
restOfStack
};
(*
to check if the stack is empty and return accordingly
having any node from this class indicates that the stack is non-empty
return value : false
*)
isEmpty() : Bool {
false
};
(*
to create a new node and insert it in the stack
parameters:
input - the value to be pushed
stack - the existing stack
return value: the modified stack
*)
init(input : String, stack : Stack) : Stack {
{
topValue <- input;
restOfStack <- stack;
self;
}
};
(*
to pop the value at the top of the stack
return value : the current value of topValue
*)
pop() : String {
let currentTop : String <- topValue in
{
topValue <- restOfStack.head();
restOfStack <- restOfStack.tail();
currentTop;
}
};
(*
to display the stack
return value : self
*)
display() : Stack {
{
if not topValue = ""
then out_string(topValue.concat("\n"))
else false
fi;
restOfStack.display();
self;
}
};
};
(*
base class for all commands accepted by the stack
*)
class StackCommand inherits IO {
-- to store whether inputs are still being accepted
acceptingInputs : Bool <- true;
(*
to provide whether inputs are still being accepted
return value : acceptingInputs
*)
acceptsInput() : Bool {
acceptingInputs
};
(*
to accept a string and recognize the type of command
parameters:
input - the command
return value : the command recognized
*)
parseCommand(input : String) : StackCommand {
{
if input = "e"
then (new EvaluateCommand)
else if input = "d"
then (new DisplayCommand)
else if input = "x"
then (new StopCommand)
else (new PushCommand)
fi fi fi;
}
};
(*
to convert a string into its equivalent integer
parameters:
input_string - the string representation of the integer
return value : the integer corresponding to input_value
*)
atoi(input_string : String) : Int {
let result : Int <- 0, i : Int <- 0 in
{
while i < input_string.length()
loop
{
result <- result * 10 + convInt(input_string.substr(i,1));
i <- i + 1;
}
pool;
result;
}
};
(*
to convert a string representation of a digit into its equivalent integer
incorrect input gives 0
parameters:
input - the string representation of the digit
return value : the integer equivalent of the string
*)
convInt(input : String) : Int {
let result : Int <- 0 in
{
if input = "1" then result <- 1 else
if input = "2" then result <- 2 else
if input = "3" then result <- 3 else
if input = "4" then result <- 4 else
if input = "5" then result <- 5 else
if input = "6" then result <- 6 else
if input = "7" then result <- 7 else
if input = "8" then result <- 8 else
if input = "9" then result <- 9 else
result <- 0
fi fi fi fi fi fi fi fi fi;
result;
}
};
(*
to compute the modulo of a positive integer with respect to another positive integer
parameters:
a - the integer whose modulo is to be taken
b - the integer with respect to which the modulo is to be taken
return value : a modulo b
*)
modulo(a : Int, b : Int) : Int {
let q : Int, r : Int in
{
if b = 0
then 0
else
{
q <- a / b;
r <- a - b * q;
r;
}
fi;
}
};
(*
to convert an integer into its equivalent string
parameters:
input - the integer
return value : the string corresponding to input
*)
itoa(input : Int) : String {
let result : String <- "", r : Int in
{
if input = 0
then result <- "0"
else
{
while 0 < input
loop
{
r <- modulo(input,10);
result <- convString(r).concat(result);
input <- input / 10;
}
pool;
}
fi;
result;
}
};
(*
to convert an integer digit into its equivalent string
incorrect input gives "0"
parameters:
input - the integer digit
return value : the string equivalent of input
*)
convString(input : Int) : String {
let result : String in
{
if input = 1 then result <- "1" else
if input = 2 then result <- "2" else
if input = 3 then result <- "3" else
if input = 4 then result <- "4" else
if input = 5 then result <- "5" else
if input = 6 then result <- "6" else
if input = 7 then result <- "7" else
if input = 8 then result <- "8" else
if input = 9 then result <- "9" else
result <- "0"
fi fi fi fi fi fi fi fi fi;
result;
}
};
(*
to evaluate a given command
this function is meant to be used in inherited classes when redefined
paramters:
stack - the current stack
input - the command
return value : the current stack
*)
evaluateCommand(stack : Stack, input : String) : Stack {
stack
};
};
(*
class for commands involving pushing a string to the stack
*)
class PushCommand inherits StackCommand {
(*
to evaluate a given command
here, it evaluates the push command by pushing the input to the stack
parameters:
stack - the current stack
input - the command
return value : the modified stack
*)
evaluateCommand(stack : Stack, input : String) : Stack {
stack.push(input)
};
};
(*
class for the command to display the stack
*)
class DisplayCommand inherits StackCommand {
(*
to evaluate a given command
here, it evaluates the display command by displaying the stack
parameters:
stack - the current stack
input - the command
return value : the current stack
*)
evaluateCommand(stack : Stack, input : String) : Stack {
stack.display()
};
};
(*
class for the stop command to stop the program
*)
class StopCommand inherits StackCommand {
(*
to evaluate a given command
here, it evaluates the stop command by setting acceptingInputs to false
parameters:
stack - the current stack
input - the command
return value : the current stack
*)
evaluateCommand(stack : Stack, input : String) : Stack {
{
acceptingInputs <- false;
stack;
}
};
};
(*
class for the evaluate command to evaluate based on the value at the top
of the stack
*)
class EvaluateCommand inherits StackCommand {
(*
to evaluate a given command
here, it evaluates the evaluate command by appropriately performing
operations based on the top element of the stack
parameters:
stack - the current stack
input - the command
return value : the modified stack
*)
evaluateCommand(stack : Stack, input : String) : Stack {
let topValue : String, oneFromTop : String, twoFromTop : String in
{
topValue <- stack.pop();
oneFromTop <- stack.pop();
twoFromTop <- stack.pop();
if topValue = "+"
then stack.push(itoa(atoi(oneFromTop) + atoi(twoFromTop)))
else if topValue = "s"
then stack.push(oneFromTop).push(twoFromTop)
else stack.push(twoFromTop).push(oneFromTop).push(topValue)
fi fi;
}
};
};
(*
the Main class
*)
class Main inherits IO {
-- to parse input and provide the current command
stackCommand : StackCommand <- new StackCommand;
-- to store the current command and to evaluate it
currentStackCommand : StackCommand <- new StackCommand;
-- to represent the stack
stack : Stack <- new Stack;
-- to store the input command
input : String;
(*
the main function
*)
main() : Object {
while currentStackCommand.acceptsInput()
loop
{
out_string(">");
input <- in_string();
currentStackCommand <- stackCommand.parseCommand(input);
stack <- currentStackCommand.evaluateCommand(stack,input);
}
pool
};
};