-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvmc.sml
484 lines (365 loc) · 15.2 KB
/
vmc.sml
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
use "stack.sml";
(* use "AST.sml"; *)
(* signature VMC =
sig
exception VMCERROR
val mem_size : int
val garbage : int
val memory : int array
val index : int ref
structure StrHashmap : <sig>
val symTbl : int StrHashmap.hash_table
val err_msg : string -> unit
val check : string -> int
val insert_list : string list * 'a -> bool
val fill_helper : AST.DECLARATION -> bool
val fill_list : AST.DECLARATION list -> bool
val fill_symbol_table : AST.PROGRAM -> bool
val Id : 'a -> 'a
val toString : string FunStack.Stack * 'a * string FunStack.Stack -> unit
val Vstack : 'a FunStack.Stack
val Cstack : 'a FunStack.Stack
val delete_ith : 'a list * int -> 'a list
val eval : int * int * string -> int
val eval_1op : int * string -> int
val get_comm_list1 : string FunStack.Stack * int
-> string FunStack.Stack * string list
val get_comm_list2 : string FunStack.Stack * int
-> string FunStack.Stack * string list
val get_comm_list3 : string FunStack.Stack * int
-> string FunStack.Stack * string list
val get_comm_list4 : string FunStack.Stack * int
-> string FunStack.Stack * string list
val push_list_on_stack : 'a FunStack.Stack * 'a list -> 'a FunStack.Stack
val get_input : string -> string
val rules : string FunStack.Stack * int array * string FunStack.Stack
-> string FunStack.Stack * int array * string FunStack.Stack
end *)
structure Vmc =
struct
exception VMCERROR;
exception BOOLERROR;
exception DUPVARERROR;
val mem_size = 200
val garbage = 0
val memory = Array.array(mem_size, garbage)
val index = ref 0
fun printList [] = ""
| printList (x::tail) = x^"\n"^printList(tail)
fun exist (_, []) = false
| exist (x, y::ys) = x = y orelse exist (x, ys)
fun delete_ith ([], k) = []
| delete_ith (x::xs, 1) = xs
| delete_ith (x::xs, k) = x :: delete_ith (xs, k - 1)
(*Symbol table / Hashmap for storing the list of declared variables *)
structure StrHashmap = HashTableFn(
struct
type hash_key = string
val hashVal = HashString.hashString
val sameKey = (op=) : string * string -> bool
end)
val symTbl : int StrHashmap.hash_table = StrHashmap.mkTable(mem_size, Fail "Not found")
val var_int_list = ref [] : string list ref
val var_bool_list = ref [] : string list ref
(* check function to check if string is in hashmap or not
if it is in hashmap, it retuns its index in memory *)
val err_msg = fn variable => TextIO.output(TextIO.stdOut, "PARSE error\n"^"variable "^variable^" not defined\n\n\n")
val check = fn variable => case ( StrHashmap.find symTbl variable ) of
SOME idx => idx
| _ => (err_msg(variable); OS.Process.exit(OS.Process.failure); 2)
(* var_type, 1 is int, 0 is bool *)
(* function to insert list of variables from declaration to hashmap *)
fun insert_list (var::list, 1) = ( StrHashmap.insert symTbl (var, (!index));
if (exist (var, !var_int_list) = true) then raise DUPVARERROR else();
if (exist (var, !var_bool_list) = true) then raise DUPVARERROR else();
var_int_list := var:: (!var_int_list);
index:= (!index)+1; insert_list (list, 1) )
| insert_list (var::list, 0) = ( StrHashmap.insert symTbl (var, (!index));
if (exist (var, !var_int_list) = true) then raise DUPVARERROR else();
if (exist (var, !var_bool_list) = true) then raise DUPVARERROR else();
var_bool_list := var:: (!var_bool_list);
index:= (!index)+1; insert_list (list, 0) )
| insert_list ([], var_type) = true
fun fill_helper (AST.DECL(strL, AST.INT)) = insert_list(strL, 1)
| fill_helper (AST.DECL(strL, AST.BOOL)) = insert_list(strL, 0)
fun fill_list [] = true
| fill_list (x::xs) = fill_helper(x) andalso fill_list(xs);
fun fill_symbol_table(AST.PROG(_, DecL, CommL)) = fill_list(DecL)
fun string_var_list (M, []) = ""
| string_var_list (M, x::L) =
let
val idx = check(x)
val x_val = Int.toString(Array.sub(M, idx))
in x^": "^x_val^", "^string_var_list(M, L)
end
fun Id (x) = x
fun toString(V, M, C) = TextIO.output(TextIO.stdOut, "\n\nV: "^(FunStack.toString Id V) ^"\nM: "^ (string_var_list(M, !var_int_list)) ^"\n\t"^ (string_var_list(M, !var_bool_list))^"\nC: "^ (FunStack.toString Id C)^"\n\n")
val Vstack = FunStack.create
val Cstack = FunStack.create
fun eval(m, n, opn) = case opn of
"PLUS" => m+n
| "MINUS" => m-n
| "DIV" => if n <> 0 then (m div n) else (TextIO.output(TextIO.stdOut, "\n\nDivision by zero error. Exiting\n\n\n"); OS.Process.exit(OS.Process.failure);~1)
| "MOD" => if n <> 0 then (m mod n) else (TextIO.output(TextIO.stdOut, "\n\nDivision by zero error. Exiting\n\n\n"); OS.Process.exit(OS.Process.failure);~1)
| "TIMES" => m*n
| "GT" => if(m > n) then 1 else 0
| "LT" => if(m < n) then 1 else 0
| "GTE" => if(m >= n) then 1 else 0
| "LTE" => if(m <= n) then 1 else 0
| "EQ" => if(m = n) then 1 else 0
| "NEQ" => if(m <> n) then 1 else 0
| "AND" => Int.min(m, n)
| "OR" => Int.max(m, n)
| _ => raise VMCERROR
fun eval_1op(m, opn) = case opn of
"NEG" => ~m
| "NOT" => if m = 0 then 1 else 0
| _ => raise VMCERROR
fun get_comm_list1(C, num) = case ( FunStack.top(C) ) of
"IF" => let
val num1 = num+1
val x = FunStack.top(C)
val C1 = FunStack.pop(C)
val (C2, L) = get_comm_list1(C1, num1)
in (C2, x::L)
end
| "THEN" =>
let
val num1 = num-1
val x = FunStack.top(C)
val C1 = FunStack.pop(C)
in
if (num1 = 0)
then (C1, [])
else (let val (C2, L) = get_comm_list1(C1, num1) in (C2, x::L) end)
end
| _ =>
let
val x = FunStack.top(C)
val C1 = FunStack.pop(C)
val (C2, L) = get_comm_list1(C1, num)
in (C2, x::L)
end
fun get_comm_list2(C, num) = case ( FunStack.top(C) ) of
"IF" => let
val num1 = num+1
val x = FunStack.top(C)
val C1 = FunStack.pop(C)
val (C2, L) = get_comm_list2(C1, num1)
in (C2, x::L)
end
| "ELSE" =>
let
val num1 = num-1
val x = FunStack.top(C)
val C1 = FunStack.pop(C)
in
if (num1 = 0)
then (C1, [])
else (let val (C2, L) = get_comm_list2(C1, num1) in (C2, x::L) end)
end
| _ =>
let
val x = FunStack.top(C)
val C1 = FunStack.pop(C)
val (C2, L) = get_comm_list2(C1, num)
in (C2, x::L)
end
fun get_comm_list3(C, num) = case ( FunStack.top(C) ) of
"WHILE" => let
val num1 = num+1
val x = FunStack.top(C)
val C1 = FunStack.pop(C)
val (C2, L) = get_comm_list3(C1, num1)
in (C2, x::L)
end
| "DO" =>
let
val num1 = num-1
val x = FunStack.top(C)
val C1 = FunStack.pop(C)
in
if (num1 = 0)
then (C1, [x])
else (let val (C2, L) = get_comm_list3(C1, num1) in (C2, x::L) end)
end
| _ =>
let
val x = FunStack.top(C)
val C1 = FunStack.pop(C)
val (C2, L) = get_comm_list3(C1, num)
in (C2, x::L)
end
fun get_comm_list4(C, num) = case ( FunStack.top(C) ) of
"WHILE" => let
val num1 = num+1
val x = FunStack.top(C)
val C1 = FunStack.pop(C)
val (C2, L) = get_comm_list4(C1, num1)
in (C2, x::L)
end
| "ENDWH" =>
let
val num1 = num-1
val x = FunStack.top(C)
val C1 = FunStack.pop(C)
in
if (num1 = 0)
then (C1, [x])
else (let val (C2, L) = get_comm_list4(C1, num1) in (C2, x::L) end)
end
| _ =>
let
val x = FunStack.top(C)
val C1 = FunStack.pop(C)
val (C2, L) = get_comm_list4(C1, num)
in (C2, x::L)
end
fun push_list_on_stack (S, []) = S
| push_list_on_stack (S, x::L) =
let
val S1 = push_list_on_stack(S, L)
val S2 = FunStack.push(x, S1)
in
S2
end
fun get_input variable = ( TextIO.output(TextIO.stdOut, "Input "^variable^": ")
; TextIO.flushOut(TextIO.stdOut)
; valOf(TextIO.inputLine TextIO.stdIn)
)
fun rules (V, M, C) = case (FunStack.top(C)) of
"ID" => let
val x = FunStack.top(V)
val V1 = FunStack.pop(V)
val idx = check(x)
val x_val = Int.toString(Array.sub(M, idx))
val V2 = FunStack.push(x_val, V1)
val C1 = FunStack.pop(C)
in (V2, M, C1)
end
| "IDB" =>
let
val x = FunStack.top(V)
val V1 = FunStack.pop(V)
val idx = check(x)
val x_val = Int.toString(Array.sub(M, idx))
val V2 = FunStack.push(x_val, V1)
val C1 = FunStack.pop(C)
in (V2, M, C1)
end
| "TT" =>
let
val V1 = FunStack.push("1", V)
val C1 = FunStack.pop(C)
in (V1, M, C1)
end
| "FF" =>
let
val V1 = FunStack.push("0", V)
val C1 = FunStack.pop(C)
in (V1, M, C1)
end
| ( "PLUS" | "MINUS" | "DIV" | "MOD" | "TIMES"
| "GT" | "LT" | "GTE" | "LTE" | "EQ" | "NEQ"
| "AND" | "OR" ) =>
let
val n = FunStack.top(V)
val V1 = FunStack.pop(V)
val m = FunStack.top(V1)
val V2 = FunStack.pop(V1)
val opn = FunStack.top(C)
val res = eval(valOf(Int.fromString(m)), valOf(Int.fromString(n)), opn)
val V3 = FunStack.push(Int.toString(res), V2)
val C1 = FunStack.pop(C)
in (V3, M, C1)
end
| ("NOT" | "NEG") =>
let
val m = FunStack.top(V)
val V1 = FunStack.pop(V)
val opn = FunStack.top(C)
val res = eval_1op(valOf(Int.fromString(m)), opn)
val V2 = FunStack.push(Int.toString(res), V1)
val C1 = FunStack.pop(C)
in (V2, M, C1)
end
| "SET" =>
let
val m = FunStack.top(V)
val V1 = FunStack.pop(V)
val x = FunStack.top(V1)
val V2 = FunStack.pop(V1)
val idx = check(x)
val _ = Array.update(M, idx, valOf(Int.fromString(m)))
val C1 = FunStack.pop(C)
in (V2, M, C1)
end
| "IF" =>
let
val b_val = FunStack.top(V)
val V1 = FunStack.pop(V)
val C1 = FunStack.pop(C)
val (C2,comm_list1) = get_comm_list1(C1, 1)
(* val _ = toString(V1, M, C2 ) *)
val (C3,comm_list2) = get_comm_list2(C2, 1)
val C_comm1 = push_list_on_stack(C3, comm_list1)
val C_comm2 = push_list_on_stack(C3, comm_list2)
in
if (b_val = "1") then (V1, M, C_comm1)
else (V1, M, C_comm2)
end
| "WHILE" =>
let
val C1 = FunStack.pop(C)
val (C2,b) = get_comm_list3(C1, 1)
val (C3,c) = get_comm_list4(C2, 1)
val C4 = push_list_on_stack(C3, b)
val V1 = push_list_on_stack(V, b)
val V2 = push_list_on_stack(V1, c)
in
(V2, M, C4)
end
| "DO" =>
let
val b_val = FunStack.top(V)
val V1 = FunStack.pop(V)
val (V2,c) = get_comm_list4(V1, 1)
val (V3,b) = get_comm_list3(V2, 1)
val C1 = FunStack.pop(C)
val C_c = push_list_on_stack(C1, c)
val C_cb = push_list_on_stack(C_c, b)
val C_cbWH = FunStack.push("WHILE", C_cb)
val c_without_endwh = delete_ith(c, List.length c)
val C_cbWHc = push_list_on_stack(C_cbWH, c_without_endwh)
in
if (b_val="0") then (V3, M, C1)
else (V3, M, C_cbWHc)
end
| "READ" =>
let
val var = FunStack.top(V)
val V1 = FunStack.pop(V)
val var_val = get_input var
val int_val = valOf(Int.fromString(var_val))
val _ = if (exist (var, !var_bool_list) = true)
then (if (int_val <> 0 andalso int_val <> 1) then raise BOOLERROR else ())
else ()
val idx = check(var)
val _ = Array.update(M, idx, int_val)
val C1 = FunStack.pop(C)
in (V1,M,C1)
end
| "WRITE" =>
let
val m = FunStack.top(V)
val V1 = FunStack.pop(V)
val _ = TextIO.output(TextIO.stdOut, m^"\n")
val C1 = FunStack.pop(C)
in (V1,M,C1)
end
| anyx => let
val V1 = FunStack.push(anyx, V)
val C1 = FunStack.pop(C)
in (V1, M, C1)
end
end