diff --git a/bsc/codegen/__init__.py b/bsc/codegen/__init__.py index 4ee9b4d..62051b6 100644 --- a/bsc/codegen/__init__.py +++ b/bsc/codegen/__init__.py @@ -14,10 +14,20 @@ def __init__(self, ctx): self.modules = [] self.cur_file = None + self.cur_sym = None + self.sym_stack = [] + self.cur_module = None self.cur_fn = None self.cur_block = None + def switch_cur_sym(self, new_cur_sym = None): + if new_cur_sym == None: + self.cur_sym = self.sym_stack.pop() + else: + self.sym_stack.append(self.cur_sym) + self.cur_sym = new_cur_sym + def gen_files(self, source_files): for file in source_files: self.gen_file(file) @@ -26,6 +36,7 @@ def gen_files(self, source_files): def gen_file(self, file): self.cur_file = file + self.switch_cur_sym(self.cur_file.mod_sym) self.cur_module = LuaModule(file.mod_sym.name) self.cur_block = self.cur_module.block self.gen_decls(file.decls) @@ -45,6 +56,7 @@ def gen_file(self, file): self.cur_module.stmts = self.cur_block self.modules.append(self.cur_module) self.cur_block = LuaBlock() + self.switch_cur_sym() def gen_decls(self, decls): for decl in decls: @@ -62,6 +74,7 @@ def gen_decl(self, decl): def gen_mod_decl(self, decl): if decl.is_inline: + self.switch_cur_sym(decl.sym) self.cur_block.add_comment(f"inline module `{decl.sym.qualname()}`") self.cur_block.add_stmt(LuaAssignment([LuaIdent(decl.name)], [])) old_block = self.cur_block @@ -83,6 +96,7 @@ def gen_mod_decl(self, decl): mod_decls = self.cur_block self.cur_block = old_block self.cur_block.add_stmt(mod_decls) + self.switch_cur_sym() else: self.cur_block.add_comment(f"extern module `{decl.sym.qualname()}`") self.cur_block.add_stmt( @@ -105,7 +119,9 @@ def gen_enum_decl(self, decl): self.cur_block.add_stmt( LuaAssignment([LuaIdent(decl.sym.name)], [LuaTable(fields)], False) ) + self.switch_cur_sym(decl.sym) self.gen_decls(decl.decls) + self.switch_cur_sym() def gen_fn_decl(self, decl): if not decl.has_body: return @@ -114,7 +130,8 @@ def gen_fn_decl(self, decl): for arg in decl.args: args.append(LuaIdent(arg.name)) luafn = LuaFunction( - decl.sym.codegen_qualname(), args, is_static = decl.sym.is_static() + decl.sym.cg_method_qualname(), args, + is_static = decl.sym.is_static() ) for arg in decl.args: if arg.default_value != None: @@ -226,6 +243,10 @@ def gen_expr(self, expr): return LuaIdent(expr.name) if expr.sym != None: return LuaIdent(expr.sym.name) return LuaIdent(expr.name) + elif isinstance(expr, PathExpr): + if expr.left_sym == self.cur_sym: + return LuaIdent(expr.name) + return LuaSelector(self.gen_expr(expr.left), expr.name) elif isinstance(expr, BlockExpr): old_block = self.cur_block block = LuaBlock() diff --git a/bsc/sym.py b/bsc/sym.py index 0cbe77b..23cb87b 100644 --- a/bsc/sym.py +++ b/bsc/sym.py @@ -62,7 +62,7 @@ def qualname(self, sep = "::"): return f"{self.parent.qualname(sep)}{sep}{self.name}" return self.name - def codegen_qualname(self): + def cg_method_qualname(self): if isinstance(self.parent, TypeSym): return f"{self.parent.name}.{self.name}" return self.name diff --git a/tests/main.bs b/tests/main.bs index 8b1ddcd..3a9421e 100644 --- a/tests/main.bs +++ b/tests/main.bs @@ -1,13 +1,15 @@ mod syntax; mod consts { - const dec_num = 616; + pub const dec_num = 616; const hex_num = 0xFFFF; const bin_num = 0b10101; const octal_num = 0o666; const float_num = 0.5e4; - fn main() {} + fn main() { + const inner = dec_num; + } } const f = 1.0 / 2.0;