diff --git a/AST.py b/AST.py new file mode 100644 index 0000000..df5979c --- /dev/null +++ b/AST.py @@ -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": [] + } \ No newline at end of file diff --git a/parser.py b/plasma_parser.py similarity index 80% rename from parser.py rename to plasma_parser.py index a3a4849..8c228a7 100644 --- a/parser.py +++ b/plasma_parser.py @@ -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 \ No newline at end of file + # endregion + + def parse_program(self) -> None: + pass \ No newline at end of file