Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refac: improve handling of FunDecl and CallExpr #295

Merged
merged 12 commits into from
Jan 23, 2025
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ All notable changes are documented in this file.
- Properly warn if FFI is used in a file intended for a different backend
- Improve various error messages and reduce noise
- Functions can no longer be shadowed by variables
- fix: change `unnecessary mut` to be a warning

### Other Changes
- `static` visibility is now controlled by `pub` keyword
- lexer: Disallow floats with trailing decimal point
- fix(c): Prevent c error if blank identifier is used

### Compiler internals
- refac: improve handling of FunDecl and CallExpr
- refac(gen): move generation of test main function into transformer


Expand Down
6 changes: 3 additions & 3 deletions 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 All @@ -112,7 +112,7 @@ pub struct Param {
}

pub fun (fn FunDecl) is_main() bool {
return not fn.is_method and fn.mix_name == 'main'
return not fn.is_method and fn.name == 'main'
}

pub struct LoopControlStmt {
Expand Down Expand Up @@ -199,7 +199,7 @@ pub struct BoolLiteral {

pub struct CallExpr {
global lang Language
pub pkg string
global pkg string
global name string
global mix_name string // TMP
global return_type Type
Expand Down
9 changes: 1 addition & 8 deletions lib/bait/ast/repr.bt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fun (e Expr) repr() string {
AsCast { e.expr.repr() + ' as XX' }
BlankIdent { '_' }
BoolLiteral { e.str() }
CallExpr{ e.mix_name + '()'}
CallExpr{ e.full_name() + '()'}
CharLiteral { e.val }
ComptimeVar { e.kind.str() }
EnumVal{ e.val }
Expand All @@ -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.mix_name}'
}
return fn.mix_name
}
3 changes: 1 addition & 2 deletions 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 Expand Up @@ -176,7 +175,7 @@ pub fun (t Table) get_method(sym TypeSymbol, name string) FunDecl {
mut s := sym
for true {
for m in s.methods {
if m.mix_name == name {
if m.name == name {
return m
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/bait/ast/types.bt
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub fun (sym TypeSymbol) find_field(name string, t Table) StructField {

pub fun (sym TypeSymbol) has_method(name string) bool {
for m in sym.methods {
if m.mix_name == name {
if m.name == name {
return true
}
}
Expand Down
6 changes: 3 additions & 3 deletions lib/bait/checker/attribute.bt
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ fun (c Checker) attr_export(attr ast.Attribute) {
c.export_names.push(attr.value)
}

fun (c Checker) check_fun_attrs_on_call(call ast.CallExpr, def ast.FunDecl) {
fun (c Checker) check_fun_attrs_on_call(call ast.CallExpr, attrs []ast.Attribute) {
mut is_deprecated := false
mut depr_attr := ast.Attribute{}
mut depr_date_attr := ast.Attribute{}

for attr in def.attrs {
for attr in attrs {
if attr.name == 'deprecated_after' {
depr_date_attr = attr
is_deprecated = true
Expand All @@ -118,7 +118,7 @@ fun (c Checker) check_fun_attrs_on_call(call ast.CallExpr, def ast.FunDecl) {
}

if is_deprecated {
mut depr_message := 'function "${call.mix_name}" '
mut depr_message := 'function "${call.full_name()}" '
if depr_date_attr.name.length > 0 {
depr_message += 'will be deprecated after ${depr_date_attr.value}'
} else {
Expand Down
Loading
Loading