This commit is contained in:
SpookyDervish
2025-10-15 16:23:51 +11:00
parent a75dcb933e
commit 549f650b54
7 changed files with 51 additions and 26 deletions

View File

@@ -56,24 +56,44 @@
"type": "BlockStatement", "type": "BlockStatement",
"statements": [ "statements": [
{ {
"type": "ReturnStatement", "type": "ExpressionStatement",
"return_value": { "expr": {
"type": "CallExpression", "type": "CallExpression",
"function": { "function": {
"type": "IdentifierLiteral", "type": "IdentifierLiteral",
"value": "add" "value": "print"
}, },
"arguments": [ "arguments": [
{ {
"type": "IntegerLiteral", "type": "StringLiteral",
"value": 1 "value": "apples %i"
}, },
{ {
"type": "IntegerLiteral", "type": "CallExpression",
"value": 2 "function": {
"type": "IdentifierLiteral",
"value": "add"
},
"arguments": [
{
"type": "IntegerLiteral",
"value": 1
},
{
"type": "IntegerLiteral",
"value": 2
}
]
} }
] ]
} }
},
{
"type": "ReturnStatement",
"return_value": {
"type": "IntegerLiteral",
"value": 0
}
} }
] ]
} }

View File

@@ -146,6 +146,8 @@ class Lexer:
tok = self.__new_token(TokenType.COLON, self.current_char) tok = self.__new_token(TokenType.COLON, self.current_char)
case ",": case ",":
tok = self.__new_token(TokenType.COMMA, self.current_char) tok = self.__new_token(TokenType.COMMA, self.current_char)
case "$":
tok = self.__new_token(TokenType.DOLLARSIGN, self.current_char)
case '"': case '"':
tok = self.__new_token(TokenType.STRING, self.__read_string()) tok = self.__new_token(TokenType.STRING, self.__read_string())
case None: case None:

View File

@@ -42,6 +42,7 @@ class TokenType(Enum):
COLON = "COLON" COLON = "COLON"
SEMICOLON = "SEMICOLON" SEMICOLON = "SEMICOLON"
COMMA = "COMMA" COMMA = "COMMA"
DOLLARSIGN = "DOLLARSIGN"
# Keywords # Keywords
RETURN = "RETURN" RETURN = "RETURN"

View File

@@ -10,9 +10,9 @@ import llvmlite.binding as llvm
from ctypes import CFUNCTYPE, c_int, c_float from ctypes import CFUNCTYPE, c_int, c_float
LEXER_DEBUG: bool = False LEXER_DEBUG: bool = False
PARSER_DEBUG: bool = True PARSER_DEBUG: bool = False
COMPILER_DEBUG: bool = False COMPILER_DEBUG: bool = False
RUN_CODE: bool = False RUN_CODE: bool = True
if __name__ == "__main__": if __name__ == "__main__":

View File

@@ -33,7 +33,7 @@ PRECEDENCES: dict[TokenType, PrecedenceType] = {
TokenType.GT: PrecedenceType.P_LESSGREATER, TokenType.GT: PrecedenceType.P_LESSGREATER,
TokenType.LT_EQ: PrecedenceType.P_LESSGREATER, TokenType.LT_EQ: PrecedenceType.P_LESSGREATER,
TokenType.GT_EQ: PrecedenceType.P_LESSGREATER, TokenType.GT_EQ: PrecedenceType.P_LESSGREATER,
TokenType.LPAREN: PrecedenceType.P_CALL TokenType.DOLLARSIGN: PrecedenceType.P_CALL
} }
class Parser: class Parser:
@@ -56,6 +56,8 @@ class Parser:
TokenType.FALSE: self.__parse_boolean, TokenType.FALSE: self.__parse_boolean,
TokenType.STRING: self.__parse_string_literal, TokenType.STRING: self.__parse_string_literal,
TokenType.DOLLARSIGN: self.__parse_call_expression
} }
self.infix_parse_functions: dict[Token, Callable] = { # 5 + 5 self.infix_parse_functions: dict[Token, Callable] = { # 5 + 5
TokenType.PLUS: self.__parse_infix_expression, TokenType.PLUS: self.__parse_infix_expression,
@@ -70,7 +72,7 @@ class Parser:
TokenType.GT: self.__parse_infix_expression, TokenType.GT: self.__parse_infix_expression,
TokenType.LT_EQ: self.__parse_infix_expression, TokenType.LT_EQ: self.__parse_infix_expression,
TokenType.GT_EQ: self.__parse_infix_expression, TokenType.GT_EQ: self.__parse_infix_expression,
TokenType.LPAREN: self.__parse_call_expression
} }
self.__next_token() self.__next_token()
@@ -340,13 +342,6 @@ class Parser:
return None return None
return expr 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]: def __parse_expression_list(self, end: TokenType) -> list[Expression]:
e_list: list[Expression] = [] e_list: list[Expression] = []
@@ -372,6 +367,15 @@ class Parser:
# endregion # endregion
# region Prefix Methods # 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: def __parse_identifier(self) -> IdentifierLiteral:
return IdentifierLiteral(value=self.current_token.literal) return IdentifierLiteral(value=self.current_token.literal)

View File

@@ -1,2 +1,4 @@
depend "io.pla" main = Func(): Int {
print("Hello, World!"); $print("Hello, World!");
return 0;
}

View File

@@ -1,8 +1,4 @@
add = Func(a: Int, b: Int): Int {
return a + b;
}
main = Func(): Int { main = Func(): Int {
add(1,2); $print("Hello, World!");
return x; return 0;
} }