Skip to content

Commit 8f054fa

Browse files
authored
First version
Source code is messy, because this was a quick & dirty thing.
1 parent e073617 commit 8f054fa

File tree

1 file changed

+229
-0
lines changed

1 file changed

+229
-0
lines changed

yakjava.bas

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
2+
dim shared as string labelNames(any)
3+
dim shared as integer labelNumbers(any)
4+
dim shared as string lines(any)
5+
dim shared as string variableName(any)
6+
dim shared as integer variableValue(any)
7+
8+
function checkIfValid (nam as string) as integer
9+
if nam="if" or nam="goto" or nam="print" or nam="true" or nam="false" then
10+
return false
11+
end if
12+
for i as integer=1 to len(nam)
13+
dim as integer char=asc(mid(nam,i,1))
14+
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(nam,i,1)="_" then
15+
return false
16+
end if
17+
next
18+
return true
19+
end function
20+
21+
sub addLabel (label as string, linenum as integer)
22+
if not checkIfValid(label) then
23+
print "Error: Line "+str(linenum)+": Invalid label '"+label+"'!"
24+
system
25+
end if
26+
redim preserve labelNames (lbound(labelNames) to ubound(labelNames)+1)
27+
redim preserve labelNumbers (lbound(labelNames) to ubound(labelNames)+1)
28+
labelNames(ubound(labelNames))=label
29+
labelNumbers(ubound(labelNames))=linenum
30+
end sub
31+
32+
sub addLine (codeline as string)
33+
redim preserve lines (lbound(lines) to ubound(lines)+1)
34+
lines(ubound(lines))=codeline
35+
end sub
36+
37+
if command(1)="" then
38+
print "lukflug's YakJava interpreter Version 0.0.0"
39+
print "21.07.2020"
40+
print "Usage: yakjava <code file>"
41+
system
42+
end if
43+
44+
if open(command(1) for input as #1)<>0 then
45+
print "Fatal: Could not open file '"+command(1)+"'!"
46+
system
47+
end if
48+
49+
dim as integer linenum=0
50+
51+
while not eof(1)
52+
linenum+=1
53+
dim as string fileline,codeline
54+
input #1,fileline
55+
dim as integer multispace=false
56+
for i as integer=1 to len(fileline)
57+
if mid(fileline,i,1)=" " or mid(fileline,i,1)=chr(9) then
58+
if not multispace then
59+
codeline+=" "
60+
multispace=true
61+
end if
62+
else
63+
codeline+=mid(fileline,i,1)
64+
multispace=false
65+
end if
66+
next
67+
68+
dim as integer sep=instr(codeline,"//")
69+
if sep>0 then codeline=mid(codeline,1,sep-1)
70+
codeline=trim(codeline)
71+
sep=instr(codeline,":")
72+
if sep>0 then
73+
addLabel(trim(mid(codeline,1,sep-1)),linenum)
74+
codeline=trim(mid(codeline,sep+1))
75+
end if
76+
addLine(codeline)
77+
wend
78+
79+
close #1
80+
81+
function replace (byval original as string, oldstr as string, newstr as string) as string
82+
dim as integer position=1
83+
while true
84+
dim as integer location=instr(position,original,oldstr)
85+
if location>0 then
86+
original=mid(original,1,location-1)+newstr+mid(original,location+len(oldstr))
87+
position=location
88+
else
89+
exit while
90+
end if
91+
wend
92+
return original
93+
end function
94+
95+
function exprEval (byval expr as string) as integer
96+
for i as integer=lbound(variableName) to ubound(variableName)
97+
if variableValue(i)=true then
98+
expr=replace(expr,variableName(i),"true")
99+
else
100+
expr=replace(expr,variableName(i),"false")
101+
end if
102+
next
103+
replace(expr," ","")
104+
while true
105+
dim as integer oldlen=len(expr)
106+
' 1. OR
107+
expr=replace(expr,"true||true","true")
108+
expr=replace(expr,"true||false","true")
109+
expr=replace(expr,"false||true","true")
110+
expr=replace(expr,"false||false","false")
111+
' 2. AND
112+
expr=replace(expr,"true&&true","true")
113+
expr=replace(expr,"true&&false","false")
114+
expr=replace(expr,"false&&true","false")
115+
expr=replace(expr,"false&&false","false")
116+
' 3. XNOR
117+
expr=replace(expr,"true==true","true")
118+
expr=replace(expr,"true==false","false")
119+
expr=replace(expr,"false==true","false")
120+
expr=replace(expr,"false==false","true")
121+
' 4. XOR
122+
expr=replace(expr,"true!=true","false")
123+
expr=replace(expr,"true!=false","true")
124+
expr=replace(expr,"false!=true","true")
125+
expr=replace(expr,"false!=false","false")
126+
' 5. NOT
127+
expr=replace(expr,"!true","false")
128+
expr=replace(expr,"!false","true")
129+
'6. Parentheses
130+
expr=replace(expr,"(true)","true")
131+
expr=replace(expr,"(false)","false")
132+
' Exit condition
133+
if expr="true" then
134+
return true
135+
elseif expr="false" then
136+
return false
137+
end if
138+
if oldlen=len(expr) then exit while
139+
wend
140+
return 1
141+
end function
142+
143+
sub addVariable (variable as string, value as integer, linenum as integer)
144+
if not checkIfValid(variable) then
145+
print "Error: Line "+str(linenum)+": Invalid variable name '"+variable+"'!"
146+
system
147+
end if
148+
for i as integer=lbound(variableName) to ubound(variableName)
149+
if variableName(i)=variable then
150+
variableValue(i)=true
151+
return
152+
end if
153+
next
154+
redim preserve variableName (lbound(variableName) to ubound(variableName)+1)
155+
redim preserve variableValue (lbound(variableName) to ubound(variableName)+1)
156+
variableName(ubound(variableName))=variable
157+
variableValue(ubound(variableName))=value
158+
end sub
159+
160+
function interpretInstruction (linenum as integer, codeline as string) as integer
161+
if codeline="" then
162+
elseif mid(codeline,1,3)="if(" or mid(codeline,1,4)="if (" then
163+
dim as integer separator=0
164+
dim as integer level=0
165+
for i as integer=instr(codeline,"(") to len(codeline)
166+
if mid(codeline,i,1)="(" then
167+
level+=1
168+
elseif mid(codeline,i,1)=")" then
169+
level-=1
170+
end if
171+
if level=0 then
172+
separator=i
173+
exit for
174+
end if
175+
next i
176+
if separator=0 then
177+
print "Error: Line "+str(linenum)+": Syntax error while parsing expression!"
178+
system
179+
end if
180+
dim as integer condition=exprEval(mid(codeline,instr(codeline,"("),separator-instr(codeline,"(")+1))
181+
if condition=true then
182+
interpretInstruction(linenum,trim(mid(codeline,separator+1)))
183+
elseif condition=false then
184+
else
185+
print "Error: Line "+str(linenum)+": Syntax error while parsing expression!"
186+
system
187+
end if
188+
elseif mid(codeline,1,5)="goto " then
189+
for i as integer=lbound(labelNames) to ubound(labelNames)
190+
if labelNames(i)=mid(codeline,6) then
191+
return labelNumbers(i)
192+
end if
193+
next
194+
print "Error: Line "+str(linenum)+": Undefined label '"+mid(codeline,6)+"!"
195+
system
196+
elseif mid(codeline,1,6)="print " then
197+
dim as integer value=exprEval(mid(codeline,7))
198+
if value=true then
199+
print "true"
200+
elseif value=false then
201+
print "false"
202+
else
203+
print "Error: Line "+str(linenum)+": Syntax error while parsing expression!"
204+
system
205+
end if
206+
else
207+
dim as integer separator=instr(codeline,"=")
208+
if separator>0 then
209+
dim as integer value=exprEval(mid(codeline,separator+1))
210+
if value=true then
211+
addVariable(trim(mid(codeline,1,separator-1)),true,linenum)
212+
elseif value=false then
213+
addVariable(trim(mid(codeline,1,separator-1)),false,linenum)
214+
else
215+
print "Error: Line "+str(linenum)+": Syntax error while parsing expression!"
216+
system
217+
end if
218+
else
219+
print "Error: Line "+str(linenum)+": Syntax error!"
220+
system
221+
end if
222+
end if
223+
return linenum+1
224+
end function
225+
226+
dim as integer counter=1
227+
while counter<=ubound(lines)+1
228+
counter=interpretInstruction(counter,lines(counter-1))
229+
wend

0 commit comments

Comments
 (0)