mirror of
https://github.com/Alvin-Zilverstand/femcode.git
synced 2026-03-06 02:56:56 +01:00
feat: Implement while loops
This commit is contained in:
5
examples/loops.fem
Normal file
5
examples/loops.fem
Normal file
@@ -0,0 +1,5 @@
|
||||
counter is 0
|
||||
Otokonoko counter < 5 Femboycore
|
||||
UwU Boy counter
|
||||
counter is counter + 1
|
||||
Periodt
|
||||
@@ -75,3 +75,7 @@ class Interpreter:
|
||||
self.visit(node.if_block)
|
||||
elif node.else_block:
|
||||
self.visit(node.else_block)
|
||||
|
||||
def visit_WhileStatement(self, node):
|
||||
while self.visit(node.condition):
|
||||
self.visit(node.body)
|
||||
|
||||
@@ -90,6 +90,9 @@ class Lexer:
|
||||
if re.match(r'\bAndrogyny\b', self.text[self.pos:]):
|
||||
self.pos += len('Androgyny')
|
||||
return Token('ANDROGYNY', 'Androgyny')
|
||||
if re.match(r'\bOtokonoko\b', self.text[self.pos:]):
|
||||
self.pos += len('Otokonoko')
|
||||
return Token('OTOKONOKO', 'Otokonoko')
|
||||
if re.match(r'\bis\b', self.text[self.pos:]):
|
||||
self.pos += 2
|
||||
return Token('ASSIGN', 'is')
|
||||
|
||||
@@ -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':
|
||||
|
||||
Reference in New Issue
Block a user