-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterpit.lua
More file actions
353 lines (306 loc) · 10.5 KB
/
interpit.lua
File metadata and controls
353 lines (306 loc) · 10.5 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
343
344
345
346
347
348
349
350
351
352
-- interpit.lua
-- Mark Underwood
-- Based upon interpit.lua by Glenn G. Chappell, with thanks
-- 3 Apr 2019
--
-- For CS F331 / CSCE A331 Spring 2019
-- Interpret AST from parseit.parse
-- For Assignment 6, Exercise B
-- *******************************************************************
-- * To run a Jerboa program, use jerboa.lua (which uses this file). *
-- *******************************************************************
local interpit = {} -- Our module
-- ***** Variables *****
-- Symbolic Constants for AST
local STMT_LIST = 1
local WRITE_STMT = 2
local FUNC_DEF = 3
local FUNC_CALL = 4
local IF_STMT = 5
local WHILE_STMT = 6
local RETURN_STMT = 7
local ASSN_STMT = 8
local CR_OUT = 9
local STRLIT_OUT = 10
local BIN_OP = 11
local UN_OP = 12
local NUMLIT_VAL = 13
local BOOLLIT_VAL = 14
local READNUM_CALL = 15
local SIMPLE_VAR = 16
local ARRAY_VAR = 17
-- ***** Utility Functions *****
-- numToInt
-- Given a number, return the number rounded toward zero.
local function numToInt(n)
assert(type(n) == "number")
if n >= 0 then
return math.floor(n)
else
return math.ceil(n)
end
end
-- strToNum
-- Given a string, attempt to interpret it as an integer. If this
-- succeeds, return the integer. Otherwise, return 0.
local function strToNum(s)
assert(type(s) == "string")
-- Try to do string -> number conversion; make protected call
-- (pcall), so we can handle errors.
local success, value = pcall(function() return 0+s end)
-- Return integer value, or 0 on error.
if success then
return numToInt(value)
else
return 0
end
end
-- numToStr
-- Given a number, return its string form.
local function numToStr(n)
assert(type(n) == "number")
return ""..n
end
-- boolToInt
-- Given a boolean, return 1 if it is true, 0 if it is false.
local function boolToInt(b)
assert(type(b) == "boolean")
if b then
return 1
else
return 0
end
end
-- astToStr
-- Given an AST, produce a string holding the AST in (roughly) Lua form,
-- with numbers replaced by names of symbolic constants used in parseit.
-- A table is assumed to represent an array.
-- See the Assignment 4 description for the AST Specification.
--
-- THIS FUNCTION IS INTENDED FOR USE IN DEBUGGING ONLY!
-- IT SHOULD NOT BE CALLED IN THE FINAL VERSION OF THE CODE.
function astToStr(x)
local symbolNames = {
"STMT_LIST", "WRITE_STMT", "FUNC_DEF", "FUNC_CALL", "IF_STMT",
"WHILE_STMT", "RETURN_STMT", "ASSN_STMT", "CR_OUT",
"STRLIT_OUT", "BIN_OP", "UN_OP", "NUMLIT_VAL", "BOOLLIT_VAL",
"READNUM_CALL", "SIMPLE_VAR", "ARRAY_VAR"
}
if type(x) == "number" then
local name = symbolNames[x]
if name == nil then
return "<Unknown numerical constant: "..x..">"
else
return name
end
elseif type(x) == "string" then
return '"'..x..'"'
elseif type(x) == "boolean" then
if x then
return "true"
else
return "false"
end
elseif type(x) == "table" then
local first = true
local result = "{"
for k = 1, #x do
if not first then
result = result .. ","
end
result = result .. astToStr(x[k])
first = false
end
result = result .. "}"
return result
elseif type(x) == "nil" then
return "nil"
else
return "<"..type(x)..">"
end
end
-- ***** Primary Function for Client Code *****
-- interp
-- Interpreter, given AST returned by parseit.parse.
-- Parameters:
-- ast - AST constructed by parseit.parse
-- state - Table holding Jerboa variables & functions
-- - AST for function xyz is in state.f["xyz"]
-- - Value of simple variable xyz is in state.v["xyz"]
-- - Value of array item xyz[42] is in state.a["xyz"][42]
-- incall - Function to call for line input
-- - incall() inputs line, returns string with no newline
-- outcall - Function to call for string output
-- - outcall(str) outputs str with no added newline
-- - To print a newline, do outcall("\n")
-- Return Value:
-- state, updated with changed variable values
function interpit.interp(ast, state, incall, outcall)
-- Each local interpretation function is given the AST for the
-- portion of the code it is interpreting. The function-wide
-- versions of state, incall, and outcall may be used. The
-- function-wide version of state may be modified as appropriate.
-- Forward declare local functions
local interp_stmt_list
local interp_stmt
local interp_write_stmt
local interp_expr
function interp_stmt_list(ast)
assert(ast[1] == STMT_LIST,
"stmt list AST must start w/ STMT_LIST")
for i = 2, #ast do
interp_stmt(ast[i])
end
end
function interp_stmt(ast)
if ast[1] == WRITE_STMT then
for i = 2, #ast do
if ast[i][1] == CR_OUT then
outcall('\n')
elseif ast[i][1] == STRLIT_OUT then
local strLength = ast[i][2]:len()
outcall(ast[i][2]:sub(2,strLength-1))
else
outcall(numToStr(interp_expr(ast[i])))
end
end
elseif ast[1] == FUNC_DEF then
state.f[ast[2]] = ast[3]
elseif ast[1] == FUNC_CALL then
local name = ast[2]
local body = state.f[name]
if body == nil then
body = { STMT_LIST } -- Default AST
end
interp_stmt_list(body)
elseif ast[1] == ASSN_STMT then
if ast[2][2] ~= "return" then
if ast[2][1] == SIMPLE_VAR then
state.v[ast[2][2]] = interp_expr(ast[3])
elseif ast[2][1] == ARRAY_VAR then
local arrayName = ast[2][2]
if state.a[arrayName] == nil then
state.a[arrayName] = {}
end
state.a[arrayName][interp_expr(ast[2][3])] = interp_expr(ast[3])
end
end
elseif ast[1] == IF_STMT then
local condition
for i = 2, #ast do
if ast[i][1] ~= STMT_LIST then
condition = interp_expr(ast[i])
else
if condition > 0 then
interp_stmt_list(ast[i])
break
end
condition = 1
end
end
elseif ast[1] == WHILE_STMT then
local condition = interp_expr(ast[2])
while condition > 0 do
interp_stmt_list(ast[3])
condition = interp_expr(ast[2])
end
elseif ast[1] == RETURN_STMT then
state.v["return"] = interp_expr(ast[2])
end
end
function interp_expr(ast)
if ast[1] == NUMLIT_VAL then
return strToNum(ast[2])
elseif ast[1] == BOOLLIT_VAL then
if ast[2] == "true" then
return 1
else
return 0
end
elseif ast[1] == READNUM_CALL then
local value = strToNum(incall())
return value
elseif ast[1] == SIMPLE_VAR then
local varValue = state.v[ast[2]]
if varValue ~= nil then
return varValue
else
return 0
end
elseif ast[1] == ARRAY_VAR then
local arrayName = state.a[ast[2]]
if arrayName ~= nil then
local arrayVal = state.a[ast[2]][interp_expr(ast[3])]
if arrayVal ~= nil then
return arrayVal
else
return 0
end
else
return 0
end
elseif ast[1] == FUNC_CALL then
interp_stmt(ast)
if state.v["return"] ~= nil then
return state.v["return"]
else
return 0
end
elseif ast[1][1] == BIN_OP then
if ast[1][2] == "+" then
return (interp_expr(ast[2]) + interp_expr(ast[3]))
elseif ast[1][2] == "-" then
return (interp_expr(ast[2]) - interp_expr(ast[3]))
elseif ast[1][2] == "*" then
return (interp_expr(ast[2]) * interp_expr(ast[3]))
elseif ast[1][2] == "/" then
local divisor = interp_expr(ast[3])
if divisor ~= 0 then
return (numToInt(interp_expr(ast[2]) / divisor))
else
return 0
end
elseif ast[1][2] == "%" then
local divisor = interp_expr(ast[3])
if divisor ~= 0 then
return (numToInt(interp_expr(ast[2]) % divisor))
else
return 0
end
elseif ast[1][2] == "==" then
return boolToInt(interp_expr(ast[2]) == interp_expr(ast[3]))
elseif ast[1][2] == "!=" then
return boolToInt(interp_expr(ast[2]) ~= interp_expr(ast[3]))
elseif ast[1][2] == "<" then
return boolToInt(interp_expr(ast[2]) < interp_expr(ast[3]))
elseif ast[1][2] == ">" then
return boolToInt(interp_expr(ast[2]) > interp_expr(ast[3]))
elseif ast[1][2] == ">=" then
return boolToInt(interp_expr(ast[2]) >= interp_expr(ast[3]))
elseif ast[1][2] == "<=" then
return boolToInt(interp_expr(ast[2]) <= interp_expr(ast[3]))
elseif ast[1][2] == "&&" then
return boolToInt(interp_expr(ast[2]) > 0 and interp_expr(ast[3]) > 0)
elseif ast[1][2] == "||" then
return boolToInt(interp_expr(ast[2]) > 0 or interp_expr(ast[3]) > 0)
end
elseif ast[1][1] == UN_OP then
if ast[1][2] == "!" then
local oldBool = interp_expr(ast[2])
if oldBool > 0 then
return 0
else
return 1
end
elseif ast[1][2] == "+" then
return interp_expr(ast[2])
elseif ast[1][2] == "-" then
return -interp_expr(ast[2])
end
end
end
interp_stmt_list(ast)
return state
end
-- ***** Module Export *****
return interpit