Skip to content

Commit

Permalink
ast: remove Table.fun_decls
Browse files Browse the repository at this point in the history
  • Loading branch information
serkonda7 committed Jan 23, 2025
1 parent 95be144 commit deb443b
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 27 deletions.
2 changes: 1 addition & 1 deletion lib/bait/ast/ast.bt
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub struct FunDecl {
pub is_pub bool
pub lang Language
pub name string
pub mix_name string // TMP
pub key string // TODO rework generics and possibly remove this field
pub params []Param
pub return_type Type
pub is_test bool
Expand Down
7 changes: 0 additions & 7 deletions lib/bait/ast/repr.bt
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,3 @@ pub fun (e Expr) repr() string {
InvalidExpr { 'InvalidExpr' }
}
}

pub fun (fn FunDecl) key() string {
if fn.is_method {
return '${fn.params[0].typ}.${fn.name}'
}
return fn.mix_name
}
1 change: 0 additions & 1 deletion lib/bait/ast/table.bt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package ast

pub struct Table {
pub mut fun_decls map[string]FunDecl
pub mut type_idxs map[string]Type
pub mut type_symbols []TypeSymbol
pub mut needed_equality_funs []Type
Expand Down
11 changes: 3 additions & 8 deletions lib/bait/checker/fun.bt
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,6 @@ fun (c Checker) fun_params(params []ast.Param){

if sym.kind == .fun_ {
info := sym.info as ast.FunInfo
c.table.fun_decls[p.name] = ast.FunDecl{
return_type = info.return_type
params = info.to_params()
}
c.scope.register(p.name, context.ScopeObject{
typ = p.typ
kind = .function
Expand Down Expand Up @@ -158,7 +154,6 @@ fun (mut c Checker) fun_call(mut node ast.CallExpr) ast.Type {
c.error("function `${node.full_name()}` is private", node.pos)
}

mut def := c.table.fun_decls[node.full_name()]
node.noreturn = obj.noreturn
node.return_type = obj.return_type

Expand All @@ -181,7 +176,7 @@ fun (mut c Checker) fun_call(mut node ast.CallExpr) ast.Type {

c.or_block(mut node)

c.set_conc_types(mut node, obj.generic_names, def.key())
c.set_conc_types(mut node, obj.generic_names, obj.key)

if node.name == 'error' {
node.return_type = c.cur_fun.return_type
Expand Down Expand Up @@ -331,7 +326,7 @@ fun (mut c Checker) method_call(mut node ast.CallExpr) ast.Type {

c.or_block(mut node)

c.set_conc_types(mut node, def.generic_names, def.key())
c.set_conc_types(mut node, def.generic_names, def.key)

return node.return_type
}
Expand Down Expand Up @@ -458,7 +453,7 @@ fun (mut c Checker) or_block(mut node ast.CallExpr) {

fun (mut c Checker) resolve_generics_funs() {
for mut fn in c.file.generic_funs {
gtypes := c.table.generic_fun_types[fn.key()]
gtypes := c.table.generic_fun_types[fn.key]
for concrete in gtypes {
c.cur_concrete_types = concrete
c.fun_instance(mut fn)
Expand Down
1 change: 1 addition & 0 deletions lib/bait/context/scope.bt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub struct ScopeObject{
pub params []ast.Param
pub attrs []ast.Attribute
pub generic_names []string
pub key string
}

pub enum ObjectKind {
Expand Down
2 changes: 1 addition & 1 deletion lib/bait/gen/c/fun.bt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fun (mut g Gen) anon_fun(node ast.AnonFun) {

fun (mut g Gen) fun_decl(node ast.FunDecl) {
if node.generic_names.length > 0 and g.cur_concrete_types.length == 0 {
gtypes := g.table.generic_fun_types[node.key()]
gtypes := g.table.generic_fun_types[node.key]
for conc_types in gtypes {
for i, gn in node.generic_names {
g.cur_concrete_types[gn] = conc_types[i]
Expand Down
2 changes: 1 addition & 1 deletion lib/bait/gen/js/fun.bt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import bait.ast

fun (mut g Gen) fun_decl(node ast.FunDecl) {
if node.generic_names.length > 0 and g.cur_concrete_types.length == 0 {
gtypes := g.table.generic_fun_types[node.key()]
gtypes := g.table.generic_fun_types[node.key]
for conc_types in gtypes {
for i, gn in node.generic_names {
g.cur_concrete_types[gn] = conc_types[i]
Expand Down
17 changes: 10 additions & 7 deletions lib/bait/parser/fun.bt
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,19 @@ fun (mut p Parser) fun_decl() !ast.FunDecl{

ffi := p.parse_ffi_pkg()!
lang = ffi.lang
mut mix_name := ""
mut key := ""
if lang != .bait {
mix_name = ffi.pkg + "." // TMP
key = ffi.pkg + "." // TMP
}

// normal function name
name := p.check_name()!
mix_name += name
key += name
is_test := name.starts_with('test_')
if not is_method and lang == .bait {
mix_name = p.prepend_pkg(mix_name)
if is_method {
key = '${params[0].typ}.${name}'
} else if lang == .bait {
key = p.prepend_pkg(key)
}

// Generics
Expand All @@ -73,7 +75,7 @@ fun (mut p Parser) fun_decl() !ast.FunDecl{
is_test = is_test
is_pub = is_pub
name = name
mix_name = mix_name
key = key
generic_names = generic_names
params = params
return_type = return_type
Expand All @@ -92,7 +94,6 @@ fun (mut p Parser) fun_decl() !ast.FunDecl{
}
sym.methods.push(node)
} else {
p.table.fun_decls[mix_name] = node
mut param_types := []ast.Type
for param in params {
param_types.push(param.typ)
Expand All @@ -110,6 +111,7 @@ fun (mut p Parser) fun_decl() !ast.FunDecl{
params = params
attrs = p.attributes
generic_names = generic_names
key = key
})
} else {
ffi_scope := p.sema_ctx.obtain_root_scope(ffi.pkg)
Expand All @@ -118,6 +120,7 @@ fun (mut p Parser) fun_decl() !ast.FunDecl{
is_pub = true
return_type = return_type
params = params
key = key
})
}
}
Expand Down
1 change: 0 additions & 1 deletion lib/bait/parser/stmt.bt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ fun (mut p Parser) script_mode_main() !ast.FunDecl {
name = "main"
return_type = ast.VOID_TYPE
}
p.table.fun_decls['main'] = node
node.stmts = stmts
return node
}
Expand Down

0 comments on commit deb443b

Please sign in to comment.