Initial commit: Basic interpreter structure

This commit is contained in:
Alvin
2025-07-22 15:56:40 +02:00
commit 39789bd96a
9 changed files with 128 additions and 0 deletions

36
src/parser.py Normal file
View File

@@ -0,0 +1,36 @@
class AST:
pass
class Print(AST):
def __init__(self, value):
self.value = value
class Parser:
def __init__(self, tokens):
self.tokens = tokens
self.pos = 0
def get_next_token(self):
if self.pos < len(self.tokens):
token = self.tokens[self.pos]
self.pos += 1
return token
return None
def parse(self):
statements = []
while True:
token = self.get_next_token()
if token is None or token.type == 'EOF':
break
if token.type == 'PRINT':
next_token = self.get_next_token()
if next_token.type == 'STRING':
statements.append(Print(next_token.value))
else:
raise Exception("Expected a string after 'UwU Boy'")
else:
raise Exception(f"Unexpected token: {token.type}")
return statements