From 549f650b54997ecf052d45ad8b6ea7d19163cd14 Mon Sep 17 00:00:00 2001 From: SpookyDervish <78246495+SpookyDervish@users.noreply.github.com> Date: Wed, 15 Oct 2025 16:23:51 +1100 Subject: [PATCH] IT WORKS --- debug/ast.json | 34 +++++++++++++++++++++++++++------- lexer.py | 2 ++ lexer_token.py | 1 + main.py | 4 ++-- plasma_parser.py | 22 +++++++++++++--------- tests/helloWorld.pla | 6 ++++-- tests/test.pla | 8 ++------ 7 files changed, 51 insertions(+), 26 deletions(-) diff --git a/debug/ast.json b/debug/ast.json index 501dde3..7091861 100644 --- a/debug/ast.json +++ b/debug/ast.json @@ -56,24 +56,44 @@ "type": "BlockStatement", "statements": [ { - "type": "ReturnStatement", - "return_value": { + "type": "ExpressionStatement", + "expr": { "type": "CallExpression", "function": { "type": "IdentifierLiteral", - "value": "add" + "value": "print" }, "arguments": [ { - "type": "IntegerLiteral", - "value": 1 + "type": "StringLiteral", + "value": "apples %i" }, { - "type": "IntegerLiteral", - "value": 2 + "type": "CallExpression", + "function": { + "type": "IdentifierLiteral", + "value": "add" + }, + "arguments": [ + { + "type": "IntegerLiteral", + "value": 1 + }, + { + "type": "IntegerLiteral", + "value": 2 + } + ] } ] } + }, + { + "type": "ReturnStatement", + "return_value": { + "type": "IntegerLiteral", + "value": 0 + } } ] } diff --git a/lexer.py b/lexer.py index 0043cf2..950e4ca 100644 --- a/lexer.py +++ b/lexer.py @@ -146,6 +146,8 @@ class Lexer: tok = self.__new_token(TokenType.COLON, self.current_char) case ",": tok = self.__new_token(TokenType.COMMA, self.current_char) + case "$": + tok = self.__new_token(TokenType.DOLLARSIGN, self.current_char) case '"': tok = self.__new_token(TokenType.STRING, self.__read_string()) case None: diff --git a/lexer_token.py b/lexer_token.py index 57bbe2d..4c1a0ff 100644 --- a/lexer_token.py +++ b/lexer_token.py @@ -42,6 +42,7 @@ class TokenType(Enum): COLON = "COLON" SEMICOLON = "SEMICOLON" COMMA = "COMMA" + DOLLARSIGN = "DOLLARSIGN" # Keywords RETURN = "RETURN" diff --git a/main.py b/main.py index b287b44..aaec4f1 100644 --- a/main.py +++ b/main.py @@ -10,9 +10,9 @@ import llvmlite.binding as llvm from ctypes import CFUNCTYPE, c_int, c_float LEXER_DEBUG: bool = False -PARSER_DEBUG: bool = True +PARSER_DEBUG: bool = False COMPILER_DEBUG: bool = False -RUN_CODE: bool = False +RUN_CODE: bool = True if __name__ == "__main__": diff --git a/plasma_parser.py b/plasma_parser.py index 7e548e5..f87481d 100644 --- a/plasma_parser.py +++ b/plasma_parser.py @@ -33,7 +33,7 @@ PRECEDENCES: dict[TokenType, PrecedenceType] = { TokenType.GT: PrecedenceType.P_LESSGREATER, TokenType.LT_EQ: PrecedenceType.P_LESSGREATER, TokenType.GT_EQ: PrecedenceType.P_LESSGREATER, - TokenType.LPAREN: PrecedenceType.P_CALL + TokenType.DOLLARSIGN: PrecedenceType.P_CALL } class Parser: @@ -56,6 +56,8 @@ class Parser: TokenType.FALSE: self.__parse_boolean, TokenType.STRING: self.__parse_string_literal, + + TokenType.DOLLARSIGN: self.__parse_call_expression } self.infix_parse_functions: dict[Token, Callable] = { # 5 + 5 TokenType.PLUS: self.__parse_infix_expression, @@ -70,7 +72,7 @@ class Parser: TokenType.GT: self.__parse_infix_expression, TokenType.LT_EQ: self.__parse_infix_expression, TokenType.GT_EQ: self.__parse_infix_expression, - TokenType.LPAREN: self.__parse_call_expression + } self.__next_token() @@ -340,13 +342,6 @@ class Parser: return None return expr - - def __parse_call_expression(self, function: Expression) -> CallExpression: - expr: CallExpression = CallExpression(function=function) - - expr.arguments = self.__parse_expression_list(TokenType.RPAREN) - - return expr def __parse_expression_list(self, end: TokenType) -> list[Expression]: e_list: list[Expression] = [] @@ -372,6 +367,15 @@ class Parser: # endregion # region Prefix Methods + def __parse_call_expression(self) -> CallExpression: + self.__next_token() + expr: CallExpression = CallExpression(function=self.__parse_expression(PrecedenceType.P_LOWEST)) + self.__next_token() + + expr.arguments = self.__parse_expression_list(TokenType.RPAREN) + + return expr + def __parse_identifier(self) -> IdentifierLiteral: return IdentifierLiteral(value=self.current_token.literal) diff --git a/tests/helloWorld.pla b/tests/helloWorld.pla index a32e625..3ffcb1c 100644 --- a/tests/helloWorld.pla +++ b/tests/helloWorld.pla @@ -1,2 +1,4 @@ -depend "io.pla" -print("Hello, World!"); \ No newline at end of file +main = Func(): Int { + $print("Hello, World!"); + return 0; +} \ No newline at end of file diff --git a/tests/test.pla b/tests/test.pla index ded45d4..3ffcb1c 100644 --- a/tests/test.pla +++ b/tests/test.pla @@ -1,8 +1,4 @@ -add = Func(a: Int, b: Int): Int { - return a + b; -} - main = Func(): Int { - add(1,2); - return x; + $print("Hello, World!"); + return 0; } \ No newline at end of file