M2000 Compiler Task: Difference between revisions

Content added Content deleted
(Created page with "One module compile, including lexical analyzer, syntax analyzer, code generator and virtual machine interpreter. The VM running using lambda functions, which include in an inv...")
 
mNo edit summary
Line 644: Line 644:
end if
end if
}
}
const nl$=chr$(13)+chr$(10)
\\ module to print a string to console using codes, 13, 10, 9
Module printsrv (a$) {
for i=1 to len(a$)
select case chrcode(Mid$(a$,i,1))
case 13
cursor 0
case 10
cursor 0 : Print
case 9
cursor ((pos+tab) div tab)*tab
else case
{
m=pos :if pos>=width then Print : m=pos
Print Mid$(a$,i,1);
if m<=width then cursor m+1
}
end select
next i
}
const nl$=chr$(13)+chr$(10)
\\ we can set starting value to any number n where 0<=n<=232
\\ we can set starting value to any number n where 0<=n<=232
enum op { halt_=232, add_, sub_, mul_, div_, mod_, not_, neg_, and_, or_, lt_,
enum op { halt_=232, add_, sub_, mul_, div_, mod_, not_, neg_, and_, or_, lt_,
Line 693: Line 674:
Append func, ne_:=lambda->{Return stack_, sp+1:=uint(if(eval(stack_, sp+1)<>eval(stack_, sp)->-1, 0)):sp++}
Append func, ne_:=lambda->{Return stack_, sp+1:=uint(if(eval(stack_, sp+1)<>eval(stack_, sp)->-1, 0)):sp++}
Append func, eq_:=lambda->{Return stack_, sp+1:=uint(if(eval(stack_, sp+1)=eval(stack_, sp)->-1, 0)):sp++}
Append func, eq_:=lambda->{Return stack_, sp+1:=uint(if(eval(stack_, sp+1)=eval(stack_, sp)->-1, 0)):sp++}
Append func, prts_:=lambda->{printsrv strings$(eval(stack_,sp)):sp++}
Append func, prts_:=lambda->{Print #-2, strings$(eval(stack_,sp));:sp++}
Append func, prti_:=lambda->{printsrv str$(sint(eval(stack_,sp)),0):sp++}
Append func, prti_:=lambda->{Print #-2, str$(sint(eval(stack_,sp)),0);:sp++}
Append func, prtc_:=lambda->{printsrv chrcode$(eval(stack_,sp)):sp++}
Append func, prtc_:=lambda->{Print #-2, chrcode$(eval(stack_,sp));:sp++}
Rem : Form 120, 60 ' change console width X height to run Ascii Mandlebrot examlpe
Rem : Form 120, 60 ' change console width X height to run Ascii Mandlebrot examlpe
Report "Virtual Assembly Code:"+{
Report "Virtual Assembly Code:"+{