-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpreter.icn
250 lines (233 loc) · 4.59 KB
/
interpreter.icn
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
#
global vars, expressionQueue
#TODO: Add a record for type checking
procedure main(args)
vars := table("NOOB")
filename := args[1]
if / filename
then write("Usage: interpreter file-name") & return
file := open(filename)
text := ""
while text := text || read(file) || "\n"
executeBlock(text)
end
#Executes a block of code
procedure executeBlock(block)
block ?
{
every loc := upto('\n') do
{
if loc < &pos
then next
line := fullTrim(tab(loc), ' \t\n')
if match("O RLY", line)
then
{
searchSpace := line || block[&pos:*block]
startBlockLocation := &pos - *line
ifCounter := 0
every wordLoc := upto(' \t\n') do
{
word := fullTrim(tab(wordLoc), ' \t\n')
if word == "O"
then ifCounter := ifCounter + 1
if word == "OIC"
then
{
if ifCounter = 0
then
{
ifStmt(block[startBlockLocation:&pos]||"\n")
moveUpto := &pos
break
}
if ifCounter > 0
then ifCounter := ifCounter - 1
}
}
&pos := moveUpto
}
else
{
parseLine(line)
}
}
}
end
#Parses and executes an individual line
procedure parseLine(line)
line ?
{
if ="I HAS A"
then addVar(line) & return
if ="O RLY"
then ifStmt(line) & return
if ="VISIBLE"
then visibleStmt(line) & return
}
end
procedure visibleStmt(line)
line ?
{
move(match("VISIBLE"))
expression := fullTrim(tab(0), ' \t\n')
if match('\"', expression)
then writes(fullTrim(expression, '\"'))
else writes(parseExpression(expression || "\n"))
}
end
procedure ifStmt(line)
line ?
{
move(match("O RLY"))
boolExpr := tab(upto('?'))
move(1) #Get rid of '?'
result := parseExpression(boolExpr || "\n")
tab(find("YA RLY"))
move(*"YA RLY")
startBlockLocation := &pos
winBlock := ""
ifCounter := 0
every wordLoc := upto(' \t\n') do
{
word := fullTrim(tab(wordLoc), ' \t\n')
if word == "O"
then ifCounter := ifCounter + 1
if word == "NO" & ifCounter = 0
then
{
winBlock := line[startBlockLocation:(&pos-2)]
move(4) #move past " WAI"
elseBlockLoc := &pos
}
if word == "OIC"
then
{
if ifCounter = 0
then
{
if \elseBlockLoc
then
{
elseBlock := line[elseBlockLoc:&pos-3]
}
else
{
winBlock := line[startBlockLocation:&pos-3]
}
break
}
if ifCounter > 0
then ifCounter := ifCounter - 1
}
}
if ifCounter ~= 0
then
{
write("ERROR O RLY? MISSING MATCHING OIC:\n" || line)
return
}
if result = 1
then
{
executeBlock(winBlock)
}
else
{
if \elseBlock
then
{
executeBlock(elseBlock)
}
}
}
end
procedure parseExpression(expr)
whitespace := ' \t\n'
expressionQueue := []
expr ?
{
every loc := upto(whitespace) do
{
term := fullTrim(tab(loc), whitespace)
if term ~== "OF" & term ~== "AN"
then
{
put(expressionQueue, term)
}
}
}
return execExpression()
end
procedure execExpression()
if *expressionQueue = 0
then write("empty") & return
node := pop(expressionQueue)
if node[1] ? any(&digits)
then return node
if node == "SUM"
then return execExpression() + execExpression()
if node == "DIF"
then return execExpression() - execExpression()
if node == "PRODUKT"
then return execExpression() * execExpression()
if node == "QUOSHUNT"
then return execExpression() / execExpression()
if node == "MOD"
then return execExpression() % execExpression()
if node == "BIGGR"
then
{
first := execExpression()
second := execExpression()
if first > second
then return first
else return second
}
if node == "SMALLR"
then
{
first := execExpression()
second := execExpression()
if first > second
then return second
else return first
}
if node == "SAEM"
then
{
if execExpression() = execExpression()
then return 1
else return 0
}
if node == "DIFFRINT"
then
{
if execExpression() = execExpression()
then return 0
else return 1
}
# Check if variable is defined / exists
value := vars[node]
if value ~== "NOOB"
then return value
else write("Error: " || node || " is undefined")
end
procedure addVar(line)
whitespace := ' \t\n'
line ?
{
move(match("I HAS A"))
varName := tab(upto(whitespace)) | tab(0)
vars[varName] := "NOOB" #TODO: implement types
if findLoc := find("ITZ")
then
{
move(5)
vars[varName] := line[&pos:*line+1]
}
}
end
procedure fullTrim(str, chars)
return reverse(trim(reverse(trim(str, chars)), chars))
end