Skip to content

Commit

Permalink
codegen: generate while stmt
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS committed Jul 5, 2024
1 parent bb82230 commit 60985fb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
7 changes: 7 additions & 0 deletions bsc/codegen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ def gen_stmt(self, stmt):
self.gen_expr(stmt.expr)
elif isinstance(stmt, ConstDecl):
self.gen_const_decl(stmt)
elif isinstance(stmt, WhileStmt):
while_stmt = LuaWhile(self.gen_expr(stmt.cond))
old_block = self.cur_block
self.cur_block = LuaBlock()
self.gen_stmts(stmt.stmts)
self.cur_block = old_block
self.cur_block.add_stmt(while_stmt)

## == Expressions ===========================================

Expand Down
12 changes: 6 additions & 6 deletions bsc/codegen/lua_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,24 @@ def __init__(self, name, args, is_static = False):
# Statements

class LuaWhile:
def __init__(self, cond, stmts):
def __init__(self, cond, stmts = []):
self.cond = cond
self.stmts = stmts
self.stmts = stmts.copy()

class LuaRepeat:
def __init__(self, stmts, cond):
self.stmts = stmts
def __init__(self, cond, stmts = []):
self.stmts = stmts.copy()
self.cond = cond

class LuaIf:
def __init__(self, branches):
self.branches = branches

class LuaIfBranch:
def __init__(self, cond, is_else, stmts):
def __init__(self, cond, is_else, stmts = []):
self.cond = cond
self.is_else = is_else
self.stmts = stmts
self.stmts = stmts.copy()

class LuaBlock:
def __init__(self, stmts = []):
Expand Down
5 changes: 5 additions & 0 deletions bsc/sema.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ def check_stmt(self, stmt):
self.check_const_decl(stmt)
elif isinstance(stmt, VarDecl):
self.check_var_decl(stmt)
elif isinstance(stmt, WhileStmt):
if not self.first_pass:
if self.check_expr(stmt.cond) != self.ctx.bool_type:
report.error("non-boolean `while` condition", stmt.cond.pos)
self.check_stmts(stmt.stmts)

## === Expressions ==================================

Expand Down

0 comments on commit 60985fb

Please sign in to comment.