-
Notifications
You must be signed in to change notification settings - Fork 1
/
yakjava.bas
238 lines (222 loc) · 8.01 KB
/
yakjava.bas
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
#include "util.bas"
dim shared as string labelNames(any)
dim shared as integer labelNumbers(any)
dim shared as string lines(any)
dim shared as string variableName(any)
dim shared as integer variableValue(any)
sub addLabel (label as string, linenum as integer)
if not checkIfValid(label) then
print "Error: Line "+str(linenum)+": Invalid label '"+label+"'!"
system
end if
redim preserve labelNames (lbound(labelNames) to ubound(labelNames)+1)
redim preserve labelNumbers (lbound(labelNames) to ubound(labelNames)+1)
labelNames(ubound(labelNames))=label
labelNumbers(ubound(labelNames))=linenum
end sub
sub addLine (codeline as string)
redim preserve lines (lbound(lines) to ubound(lines)+1)
lines(ubound(lines))=codeline
end sub
if command(1)="" then
print "lukflug's YakJava Interpreter Version 0.0.1"
print "22.07.2020"
print "Usage: yakjava <code file>"
system
end if
if open(command(1) for input as #1)<>0 then
print "Fatal: Could not open file '"+command(1)+"'!"
system
end if
dim as integer linenum=0
while not eof(1)
linenum+=1
dim as string fileline,codeline
input #1,fileline
dim as integer multispace=false
for i as integer=1 to len(fileline)
if mid(fileline,i,1)=" " or mid(fileline,i,1)=chr(9) then
if not multispace then
codeline+=" "
multispace=true
end if
else
codeline+=mid(fileline,i,1)
multispace=false
end if
next
dim as integer sep=instr(codeline,"//")
if sep>0 then codeline=mid(codeline,1,sep-1)
codeline=trim(codeline)
sep=instr(codeline,":")
if sep>0 then
addLabel(trim(mid(codeline,1,sep-1)),linenum)
codeline=trim(mid(codeline,sep+1))
end if
if mid(codeline,len(codeline),1)<>";" and codeline<>"" then
print "Error: Missing semicolon at line "+str(linenum)
system
end if
if codeline<>"" then
addLine(mid(codeline,1,len(codeline)-1))
else
addLine("")
end if
wend
close #1
function getValue (part as string, linenum as integer) as string
if part="input" then
while true
print "(y/n) ";
dim as integer key=getkey
if key=asc("y") or key=asc("Y") then
print "y"
return "true"
elseif key=asc("n") or key=asc("N") then
print "n"
return "false"
end if
wend
elseif part="true" or part="false" or part="" then
return part
else
dim found as integer=false
for i as integer=lbound(variableName) to ubound(variableName)
if variableName(i)=part then
if variableValue(i) then return "true"
return "false"
end if
next
end if
print "Error: Line "+str(linenum)+": Variable '"+part+"' not defined!"
system
end function
function exprEval (byval rawexpr as string, linenum as integer) as integer
dim as integer lastChar=0
dim as string expr=""
replace(rawexpr," ","")
for i as integer=1 to len(rawexpr)
dim as integer char=asc(mid(rawexpr,i,1))
if not (char>=asc("A") and char<=asc("Z")) and not (char>=asc("a") and char<=asc("z")) and not (char>=asc("0") and char<=asc("9")) and not mid(rawexpr,i,1)="_" then
expr+=getValue(mid(rawexpr,lastChar+1,i-lastChar-1),linenum)
lastChar=i
expr+=mid(rawexpr,i,1)
end if
next
expr+=getValue(mid(rawexpr,lastChar+1),linenum)
while true
dim as integer oldlen=len(expr)
' 1. OR
expr=replace(expr,"true||true","true")
expr=replace(expr,"true||false","true")
expr=replace(expr,"false||true","true")
expr=replace(expr,"false||false","false")
' 2. AND
expr=replace(expr,"true&&true","true")
expr=replace(expr,"true&&false","false")
expr=replace(expr,"false&&true","false")
expr=replace(expr,"false&&false","false")
' 3. XNOR
expr=replace(expr,"true==true","true")
expr=replace(expr,"true==false","false")
expr=replace(expr,"false==true","false")
expr=replace(expr,"false==false","true")
' 4. XOR
expr=replace(expr,"true!=true","false")
expr=replace(expr,"true!=false","true")
expr=replace(expr,"false!=true","true")
expr=replace(expr,"false!=false","false")
' 5. NOT
expr=replace(expr,"!true","false")
expr=replace(expr,"!false","true")
'6. Parentheses
expr=replace(expr,"(true)","true")
expr=replace(expr,"(false)","false")
' Exit condition
if expr="true" then
return true
elseif expr="false" then
return false
end if
if oldlen=len(expr) then exit while
wend
print "Error: Line "+str(linenum)+": Syntax error while parsing expression!"
system
end function
sub addVariable (variable as string, value as integer, linenum as integer)
if not checkIfValid(variable) then
print "Error: Line "+str(linenum)+": Invalid variable name '"+variable+"'!"
system
end if
for i as integer=lbound(variableName) to ubound(variableName)
if variableName(i)=variable then
variableValue(i)=true
return
end if
next
redim preserve variableName (lbound(variableName) to ubound(variableName)+1)
redim preserve variableValue (lbound(variableName) to ubound(variableName)+1)
variableName(ubound(variableName))=variable
variableValue(ubound(variableName))=value
end sub
function interpretInstruction (linenum as integer, codeline as string) as integer
if codeline="" then
elseif mid(codeline,1,3)="if(" or mid(codeline,1,4)="if (" then
dim as integer separator=0
dim as integer level=0
for i as integer=instr(codeline,"(") to len(codeline)
if mid(codeline,i,1)="(" then
level+=1
elseif mid(codeline,i,1)=")" then
level-=1
end if
if level=0 then
separator=i
exit for
end if
next i
if separator=0 then
print "Error: Line "+str(linenum)+": Syntax error while parsing expression!"
system
end if
dim as integer condition=exprEval(mid(codeline,instr(codeline,"("),separator-instr(codeline,"(")+1),linenum)
if condition=true then
interpretInstruction(linenum,trim(mid(codeline,separator+1)))
elseif condition=false then
else
end if
elseif mid(codeline,1,5)="goto " then
for i as integer=lbound(labelNames) to ubound(labelNames)
if labelNames(i)=mid(codeline,6) then
return labelNumbers(i)
end if
next
print "Error: Line "+str(linenum)+": Undefined label '"+mid(codeline,6)+"!"
system
elseif mid(codeline,1,6)="print(" or mid(codeline,1,7)="print (" then
dim as integer value=exprEval(mid(codeline,instr(codeline,"(")),linenum)
if value then
print "true"
else
print "false"
end if
else
dim as integer separator=instr(codeline,"=")
if separator>0 then
dim as integer value=exprEval(mid(codeline,separator+1),linenum)
if value then
addVariable(trim(mid(codeline,1,separator-1)),true,linenum)
else
addVariable(trim(mid(codeline,1,separator-1)),false,linenum)
end if
else
print "Error: Line "+str(linenum)+": Syntax error!"
system
end if
end if
return linenum+1
end function
dim as integer counter=1
while counter<=ubound(lines)+1
counter=interpretInstruction(counter,lines(counter-1))
wend