Let's Write an Interpreter (in 168 Lines of Python)

Started by Zlatko Vid, February 15, 2024, 03:44:00 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Zlatko Vid


Zlatko Vid

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

Zlatko Vid

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)

Charles Pegge

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.

Zlatko Vid


Zlatko Vid

minpy.o2bas TEST 1

source code test.txt loaded..

Zlatko Vid

#6
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

Zlatko Vid

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

Zlatko Vid

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()