IT WORKS
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
2
lexer.py
2
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:
|
||||
|
||||
@@ -42,6 +42,7 @@ class TokenType(Enum):
|
||||
COLON = "COLON"
|
||||
SEMICOLON = "SEMICOLON"
|
||||
COMMA = "COMMA"
|
||||
DOLLARSIGN = "DOLLARSIGN"
|
||||
|
||||
# Keywords
|
||||
RETURN = "RETURN"
|
||||
|
||||
4
main.py
4
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__":
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -1,2 +1,4 @@
|
||||
depend "io.pla"
|
||||
print("Hello, World!");
|
||||
main = Func(): Int {
|
||||
$print("Hello, World!");
|
||||
return 0;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user