while loops work!

This commit is contained in:
SpookyDervish
2025-10-15 18:44:15 +11:00
parent 549f650b54
commit 9f6fff9977
7 changed files with 155 additions and 86 deletions

View File

@@ -2,6 +2,7 @@ from llvmlite import ir
from AST import Node, NodeType, Program, Expression
from AST import ExpressionStatement, AssignmentStatement, BlockStatement, ReturnStatement, FunctionStatement, ReassignStatement, IfStatement
from AST import WhileStatement
from AST import InfixExpression, CallExpression
from AST import IntegerLiteral, FloatLiteral, IdentifierLiteral, BooleanLiteral, StringLiteral
from AST import FunctionParameter
@@ -84,6 +85,8 @@ class Compiler:
self.__visit_reassign_statement(node)
case NodeType.IfStatement:
self.__visit_if_statement(node)
case NodeType.WhileStatement:
self.__visit_while_statement(node)
# Expressions
case NodeType.InfixExpression:
@@ -211,6 +214,30 @@ class Compiler:
with otherwise:
self.compile(alternative)
def __visit_while_statement(self, node: WhileStatement) -> None:
condition: Expression = node.condition
body: BlockStatement = node.body
test, _ = self.__resolve_value(condition)
while_loop_entry = self.builder.append_basic_block(f"while_loop_entry_{self.__increment_counter()}")
while_loop_otherwise = self.builder.append_basic_block(f"while_loop_otherwise_{self.counter}")
# Creating a condition branch
# condition
# / \
# true false
# / \
# / \
# if block else block
self.builder.cbranch(test, while_loop_entry, while_loop_otherwise)
self.builder.position_at_start(while_loop_entry)
self.compile(body)
test, _ = self.__resolve_value(condition)
self.builder.cbranch(test, while_loop_entry, while_loop_otherwise)
self.builder.position_at_start(while_loop_otherwise)
# endregion
# region Expressions