IT-Berater Forum

IT-Consultant: Charles Pegge => OxygenBasic Examples => Topic started by: Zlatko Vid on February 15, 2024, 03:44:00 PM

Title: Let's Write an Interpreter (in 168 Lines of Python)
Post by: Zlatko Vid on February 15, 2024, 03:44:00 PM
i will try translate this one to o2 code :

Title: Re: Let's Write an Interpreter (in 168 Lines of Python)
Post by: Zlatko Vid on February 15, 2024, 08:27:21 PM
Arrghhhh
I finally get it ..to work
python as usual pissed me off ..i forget that is whitespace sensitive  ::)
ahh i never liked python but this guy really made good interpreter
and translation should not be very hard 170 lines of code  :)
ok here is how look in PyScripter IDE
Title: Re: Let's Write an Interpreter (in 168 Lines of Python)
Post by: Zlatko Vid on February 15, 2024, 08:36:07 PM
something like this:

'min.py in oxygen

'try: f = open('demo.txt', 'r') # open source file
'except: print("ERROR: Can't find source file \'" + sys.argv[1] + "\'."); exit(1)
'source = f.read() + '\0'; f.close()


sys f = getfile(filename, source)
Title: Re: Let's Write an Interpreter (in 168 Lines of Python)
Post by: Charles Pegge on February 15, 2024, 10:05:54 PM
The metalanguage in Oxygen is a kind of interpreter. but it only works at compile-time. It takes up about 1300 lines of the total o2 line count of around 30000 lines. ~ 4% of the total source code.
Title: Re: Let's Write an Interpreter (in 168 Lines of Python)
Post by: Zlatko Vid on February 16, 2024, 08:13:09 AM
WOW..good to know Charles  :)
Title: Re: Let's Write an Interpreter (in 168 Lines of Python)
Post by: Zlatko Vid on February 16, 2024, 03:04:50 PM
minpy.o2bas TEST 1

source code test.txt loaded..
Title: Re: Let's Write an Interpreter (in 168 Lines of Python)
Post by: Zlatko Vid on February 19, 2024, 07:42:23 PM
any help?

Code Select
def TakeString(word):            # returns whether a certain string could be taken starting at pc
    global pc; copypc = pc
    for c in word:
        if Take() != c: pc = copypc; return False
    return True

is this valid in Basic:

Code Select
'...
sub Take() as string ' takes away and returns the current character
c = Look()  pc = pc + 1 : return c
end sub
'...
sub TakeString(string _word) as int        ' returns whether a certain string could be taken starting at pc
    'global pc;
    int copypc = pc
    for c = 1 to LEN(_word)
        if Take() <> c : pc = copypc : return False : end if
        return True
    next c
end sub
Title: Re: Let's Write an Interpreter (in 168 Lines of Python)
Post by: Zlatko Vid on February 19, 2024, 07:58:11 PM
maybe is this proper :

sub Take() as string
      string c ' takes away and returns the current character
c = Look() : pc = pc + 1 : return c
end sub
'...
sub TakeString(string _word) as int ' returns whether a certain string could be taken starting at pc
int n : string c
int copypc = pc
for n = 1 to LEN(_word)
         c = mid(_word,n,1)
if Take() <> c : pc = copypc : return False : end if
     return True
    next n
end sub
Title: Re: Let's Write an Interpreter (in 168 Lines of Python)
Post by: Zlatko Vid on February 19, 2024, 08:05:21 PM
If some of you interested or want compile...
 program compile OKAY!
..one loop is commented in line : 52
because require Block() procedure/ function

'min.py in oxygen
$filename "minpy.exe"
include "rtl32.inc"
#lookahead

'global vars...
int pc : string variable[]  'program counteridentifier -> (type, value) lookup table
'get program
string source[] , fname
fname = "test.txt"
source = GetFile fname
print source

'...
sub Look() as string ' returns the current character while skipping over comments
'global pc ' comments are entered by # and exited by \n or \0
if source[pc] = "#"
while source[pc] <> chr(10) and source[pc] <> chr(0): pc = pc + 1 : wend ' scan over comments here
     end if
return source[pc]
end sub
'...
sub Take() as string
      string c ' takes away and returns the current character
c = Look() : pc = pc + 1 : return c
end sub
'...
sub TakeString(string _word) as int ' returns whether a certain string could be taken starting at pc
int n : string c
int copypc = pc
for n = 1 to LEN(_word)
         c = mid(_word,n,1)
if Take() <> c : pc = copypc : return False : end if
     return True
    next n
end sub






'....
sub NextC() as string ' returns the next non-whitespace character
    while Look() = " " or Look() = chr(9) or Look() = chr(10) or Look() = chr(13) : Take()
     return Look()
    wend
end sub
'...
sub Program()
int act = True
'while NextC() <> chr(0) : Block(act) : wend
end sub

'def Error(text):




'start program...
Program()