more work on plasma parser

This commit is contained in:
2025-10-13 13:09:48 +11:00
parent d4aa448712
commit 40d695729f
2 changed files with 60 additions and 1 deletions

44
AST.py Normal file
View File

@@ -0,0 +1,44 @@
from abc import ABC, abstractmethod
from enum import Enum
class NodeType(Enum):
Program = "Program"
# Statements
ExpressionStatement = "ExpressionStatement"
# Expressions
InfixExpression = "InfixExpression"
# Literals
IntegerLiteral = "IntegerLiteral"
FloatLiteral = "FloatLiteral"
class Node:
@abstractmethod
def type(self) -> NodeType:
pass
@abstractmethod
def json(self) -> dict:
pass
class Statement(Node):
pass
class Expression(Node):
pass
class Program(Node):
def __init__(self) -> None:
self.statements: list[Statement] = []
def type(self) -> NodeType:
return NodeType.Program
def json(self) -> dict:
return {
"type": self.type().value,
"statements": []
}

View File

@@ -54,9 +54,24 @@ class Parser:
self.__peek_error(tt)
return False
def __current_precedence(self) -> PrecedenceType:
prec = PRECEDENCES.get(self.current_token.type)
if prec is None:
return PrecedenceType.P_LOWEST
return prec
def __peek_precedence(self) -> PrecedenceType:
prec = PRECEDENCES.get(self.peek_token.type)
if prec is None:
return PrecedenceType.P_LOWEST
return prec
def __peek_error(self, tt: TokenType):
self.errors.append(f"Expected next token to be {tt}, got {self.peek_token.type} instead.")
def __no_prefix_parse_function_error(self, tt: TokenType):
self.errors.append(f"No Prefix Parse Function for {tt} found.")
# endregion
# endregion
def parse_program(self) -> None:
pass