FOR STATEMENTS WORK!!!!!

This commit is contained in:
SpookyDervish
2025-10-16 17:35:00 +11:00
parent 600bebb9b2
commit 1d6c3db5e4
4 changed files with 25 additions and 42 deletions

View File

@@ -14,29 +14,29 @@
"type": "BlockStatement", "type": "BlockStatement",
"statements": [ "statements": [
{ {
"type": "AssignmentStatement", "type": "ForStatement",
"name": { "var_declaration": {
"type": "IdentifierLiteral", "type": "AssignmentStatement",
"value": "a" "name": {
"type": "IdentifierLiteral",
"value": "x"
},
"value": {
"type": "IntegerLiteral",
"value": 1
},
"value_type": "Int"
}, },
"value": {
"type": "IntegerLiteral",
"value": 0
},
"value_type": "Int"
},
{
"type": "WhileStatement",
"condition": { "condition": {
"type": "InfixExpression", "type": "InfixExpression",
"left_node": { "left_node": {
"type": "IdentifierLiteral", "type": "IdentifierLiteral",
"value": "a" "value": "x"
}, },
"operator": "<", "operator": "<=",
"right_node": { "right_node": {
"type": "IntegerLiteral", "type": "IntegerLiteral",
"value": 10 "value": 20
} }
}, },
"body": { "body": {
@@ -53,33 +53,14 @@
"arguments": [ "arguments": [
{ {
"type": "StringLiteral", "type": "StringLiteral",
"value": "a = %i\\n" "value": "i = %i\\n"
}, },
{ {
"type": "IdentifierLiteral", "type": "IdentifierLiteral",
"value": "a" "value": "x"
} }
] ]
} }
},
{
"type": "ReassignStatement",
"ident": {
"type": "IdentifierLiteral",
"value": "a"
},
"right_value": {
"type": "InfixExpression",
"left_node": {
"type": "IdentifierLiteral",
"value": "a"
},
"operator": "+",
"right_node": {
"type": "IntegerLiteral",
"value": 1
}
}
} }
] ]
} }
@@ -88,7 +69,7 @@
"type": "ReturnStatement", "type": "ReturnStatement",
"return_value": { "return_value": {
"type": "IdentifierLiteral", "type": "IdentifierLiteral",
"value": "a" "value": "x"
} }
} }
] ]

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

@@ -202,6 +202,7 @@ class Parser:
assign_stmt.ident = stmt.name assign_stmt.ident = stmt.name
assign_stmt.right_value = self.__parse_expression(PrecedenceType.P_LOWEST) assign_stmt.right_value = self.__parse_expression(PrecedenceType.P_LOWEST)
while not self.__current_token_is(TokenType.SEMICOLON) and not self.__current_token_is(TokenType.EOF): while not self.__current_token_is(TokenType.SEMICOLON) and not self.__current_token_is(TokenType.EOF):
self.__next_token() self.__next_token()
@@ -355,7 +356,7 @@ class Parser:
stmt.action = self.__parse_assignment_statement() stmt.action = self.__parse_assignment_statement()
print(stmt.action.json())
self.__next_token() self.__next_token()
@@ -376,6 +377,7 @@ class Parser:
left_expr: Expression = prefix_func() left_expr: Expression = prefix_func()
while not self.__peek_token_is(TokenType.SEMICOLON) and precedence.value < self.__peek_precedence().value: while not self.__peek_token_is(TokenType.SEMICOLON) and precedence.value < self.__peek_precedence().value:
infix_func: Callable | None = self.infix_parse_functions.get(self.peek_token.type) infix_func: Callable | None = self.infix_parse_functions.get(self.peek_token.type)
if infix_func is None: if infix_func is None:
return left_expr return left_expr

View File

@@ -1,6 +1,6 @@
main = Func(): Int { main = Func(): Int {
for (x: Int = 1; x <= 20; x = x + 1) { for (x: Int = 1; x <= 20; x = x + 1;) {
print("i = %i\n", x) $print("i = %i\n", x)
} }
return x; return x;