mirror of
https://github.com/Alvin-Zilverstand/femcode.git
synced 2026-03-07 13:24:44 +01:00
feat: Implement while loops
This commit is contained in:
@@ -48,6 +48,11 @@ class IfStatement(AST):
|
||||
self.if_block = if_block
|
||||
self.else_block = else_block
|
||||
|
||||
class WhileStatement(AST):
|
||||
def __init__(self, condition, body):
|
||||
self.condition = condition
|
||||
self.body = body
|
||||
|
||||
class Parser:
|
||||
def __init__(self, tokens):
|
||||
self.tokens = tokens
|
||||
@@ -83,6 +88,9 @@ class Parser:
|
||||
if token.type == 'FEMBOY_FEMININE':
|
||||
return self.parse_if_statement()
|
||||
|
||||
if token.type == 'OTOKONOKO':
|
||||
return self.parse_while_statement()
|
||||
|
||||
raise Exception(f"Invalid statement starting with token {token.type}")
|
||||
|
||||
def parse_print_statement(self):
|
||||
@@ -135,6 +143,25 @@ class Parser:
|
||||
|
||||
return IfStatement(condition, if_block, else_block)
|
||||
|
||||
def parse_while_statement(self):
|
||||
self.get_next_token() # Consume OTOKONOKO
|
||||
|
||||
condition = self.expression()
|
||||
|
||||
if self.peek_next_token().type != 'FEMBOYCORE':
|
||||
raise Exception("Expected 'Femboycore' to start while loop body")
|
||||
self.get_next_token() # Consume FEMBOYCORE
|
||||
|
||||
body_statements = []
|
||||
while self.peek_next_token().type != 'PERIODT':
|
||||
if self.peek_next_token().type == 'EOF':
|
||||
raise Exception("Unterminated while loop: Expected 'Periodt'")
|
||||
body_statements.append(self.parse_statement())
|
||||
self.get_next_token() # Consume PERIODT
|
||||
body = Block(body_statements)
|
||||
|
||||
return WhileStatement(condition, body)
|
||||
|
||||
def factor(self):
|
||||
token = self.get_next_token()
|
||||
if token.type == 'INTEGER':
|
||||
@@ -167,4 +194,4 @@ class Parser:
|
||||
right_node = self.expression() # Recursively parse right side of comparison
|
||||
node = Comparison(left=node, op=op_token, right=right_node)
|
||||
|
||||
return node
|
||||
return node
|
||||
Reference in New Issue
Block a user