diff --git a/bsc/codegen/__init__.py b/bsc/codegen/__init__.py index 62051b6..f8114b1 100644 --- a/bsc/codegen/__init__.py +++ b/bsc/codegen/__init__.py @@ -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 =========================================== diff --git a/bsc/codegen/lua_ast.py b/bsc/codegen/lua_ast.py index 179505d..6a46831 100644 --- a/bsc/codegen/lua_ast.py +++ b/bsc/codegen/lua_ast.py @@ -33,13 +33,13 @@ 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: @@ -47,10 +47,10 @@ 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 = []): diff --git a/bsc/sema.py b/bsc/sema.py index 973c6d2..eeeb1d5 100644 --- a/bsc/sema.py +++ b/bsc/sema.py @@ -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 ==================================