Skip to content

Commit

Permalink
codegen: generate ast.PathExpr
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS committed Jul 4, 2024
1 parent 030f02a commit 633d8b7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
23 changes: 22 additions & 1 deletion bsc/codegen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion bsc/sym.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions tests/main.bs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 633d8b7

Please sign in to comment.