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,8 +56,19 @@
"type": "BlockStatement",
"statements": [
{
"type": "ReturnStatement",
"return_value": {
"type": "ExpressionStatement",
"expr": {
"type": "CallExpression",
"function": {
"type": "IdentifierLiteral",
"value": "print"
},
"arguments": [
{
"type": "StringLiteral",
"value": "apples %i"
},
{
"type": "CallExpression",
"function": {
"type": "IdentifierLiteral",
@@ -74,6 +85,15 @@
}
]
}
]
}
},
{
"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)
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:

View File

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

View File

@@ -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__":

View File

@@ -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()
@@ -341,13 +343,6 @@ class Parser:
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)

View File

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

View File

@@ -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;
}