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(ast.TypeSymbol): clarify dirty field usage by mix_ prefix #293

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/bait/ast/ast.bt
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,8 @@ pub struct InvalidStmt {
}

pub struct Import {
pub name string // Full import name
pub alias string // Custom alias or the part after the last dot
pub name string // Full name, e.g. `crypto.sha1`
pub alias string // Last name part or custom alias, e.g. `sha1`
pub lang Language
pub pos token.Pos
}
Expand Down
18 changes: 9 additions & 9 deletions lib/bait/ast/table.bt
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ pub fun (mut t Table) register_concrete(key string, concrete []Type) bool {

pub fun (t Table) type_name(typ Type) string {
amps := '&'.repeat(typ.get_nr_amp())
return amps + t.get_sym(typ).name
return amps + t.get_sym(typ).mix_name
}

pub fun (t Table) register_sym(sym TypeSymbol) Type {
idx := t.get_idx(sym.name)
idx := t.get_idx(sym.mix_name)
if idx > 0 as Type {
cur_sym := t.get_sym(idx)
if cur_sym.kind == .placeholder {
Expand All @@ -96,21 +96,21 @@ pub fun (t Table) register_sym(sym TypeSymbol) Type {

new_idx := t.type_symbols.length as Type
t.type_symbols.push(sym)
t.type_idxs[sym.name] = new_idx
t.type_idxs[sym.mix_name] = new_idx
return new_idx
}

pub fun (t Table) find_or_register_array(elem_type Type) Type {
elem_sym := t.get_sym(elem_type)
name := '[]' + elem_sym.name
name := '[]' + elem_sym.mix_name
idx := t.get_idx(name)
if idx > 0 as Type {
return idx
}

return t.register_sym(TypeSymbol{
kind = .array
name = name
mix_name = name
pkg = elem_sym.pkg
parent = ARRAY_TYPE
info = ArrayInfo{
Expand All @@ -122,14 +122,14 @@ pub fun (t Table) find_or_register_array(elem_type Type) Type {
pub fun (t Table) find_or_register_map(key_type Type, val_type Type) Type {
key_sym := t.get_sym(key_type)
val_sym := t.get_sym(val_type)
name := 'map[${key_sym.name}]${val_sym.name}'
name := 'map[${key_sym.mix_name}]${val_sym.mix_name}'
idx := t.get_idx(name)
if idx > 0 as Type {
return idx
}
return t.register_sym(TypeSymbol{
kind = .map
name = name
mix_name = name
parent = MAP_TYPE
info = MapInfo{
key_type = key_type
Expand All @@ -148,7 +148,7 @@ pub fun (t Table) find_or_register_fun(param_types []Type, return_type Type, ali

return t.register_sym(TypeSymbol{
kind = .fun_
name = name
mix_name = name
info = FunInfo{
is_alias = alias
param_types = param_types
Expand All @@ -166,7 +166,7 @@ pub fun (t Table) find_type_or_add_placeholder(name string, pkg string) Type {

return t.register_sym(TypeSymbol{
kind = .placeholder
name = name
mix_name = name
pkg = pkg
})
}
Expand Down
23 changes: 12 additions & 11 deletions lib/bait/ast/types.bt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub enum TypeKind {

pub struct TypeSymbol{
pub mut name string
pub mut mix_name string // TMP
pub mut kind TypeKind
pub mut methods []FunDecl
pub mut parent Type
Expand Down Expand Up @@ -113,9 +114,9 @@ pub fun (sym TypeSymbol) has_method(name string) bool {

pub fun (t Table) register_builtins(){
// IMPORTANT: Order of registration must match the order of the constants above
_ = t.register_sym(TypeSymbol{ name = '_' }) // PLACEHOLDER_TYPE
_ = t.register_sym(TypeSymbol{ name = '_err' }) // ERROR_TYPE
_ = t.register_sym(TypeSymbol{ name = 'void' })
_ = t.register_sym(TypeSymbol{ mix_name = '_' }) // PLACEHOLDER_TYPE
_ = t.register_sym(TypeSymbol{ mix_name = '_err' }) // ERROR_TYPE
_ = t.register_sym(TypeSymbol{ mix_name = 'void' })
_ = t.register_num('i8')
_ = t.register_num('i16')
_ = t.register_num('i32')
Expand All @@ -124,27 +125,27 @@ pub fun (t Table) register_builtins(){
_ = t.register_num('u16')
_ = t.register_num('u32')
_ = t.register_num('u64')
_ = t.register_sym(TypeSymbol{ name = 'f32' })
_ = t.register_sym(TypeSymbol{ name = 'f64' })
_ = t.register_sym(TypeSymbol{ name = 'bool' })
_ = t.register_sym(TypeSymbol{ mix_name = 'f32' })
_ = t.register_sym(TypeSymbol{ mix_name = 'f64' })
_ = t.register_sym(TypeSymbol{ mix_name = 'bool' })
_ = t.register_sym(TypeSymbol{
name = 'string'
mix_name = 'string'
kind = .string
})
_ = t.register_sym(TypeSymbol{
name = 'Array'
mix_name = 'Array'
kind = .array
})
_ = t.register_sym(TypeSymbol{
name = 'Map'
mix_name = 'Map'
kind = .map
})
_ = t.register_sym(TypeSymbol{ name = 'any'})
_ = t.register_sym(TypeSymbol{ mix_name = 'any'})
}

pub fun (t Table) register_num(name string) {
_ = t.register_sym(TypeSymbol{
name = name
mix_name = name
kind = .number
is_pub = true
})
Expand Down
2 changes: 1 addition & 1 deletion lib/bait/checker/assign.bt
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ fun (c Checker) is_field_mutable(left ast.SelectorExpr) bool {
sym := c.table.get_sym(left.expr_type)
field := sym.find_field(left.field_name, c.table)
if (not field.is_mut or sym.pkg != c.pkg) and not field.is_global {
c.error('field `${sym.name}.${left.field_name}` is immutable', left.pos)
c.error('field `${sym.mix_name}.${left.field_name}` is immutable', left.pos)
return false
}
return true
Expand Down
2 changes: 1 addition & 1 deletion lib/bait/checker/attribute.bt
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fun (c Checker) attr_overload(node ast.FunDecl, attr ast.Attribute) {
rec_sym := c.table.get_sym(node.params[0].typ)
if rec_sym.overloads.contains(attr.value) {
// TODO print positions of all overloads
c.error('"${attr.value}" was overloaded twice for type "${rec_sym.name}"', attr.pos)
c.error('"${attr.value}" was overloaded twice for type "${rec_sym.mix_name}"', attr.pos)
return
}

Expand Down
18 changes: 9 additions & 9 deletions lib/bait/checker/expr.bt
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,19 @@ fun (mut c Checker) enum_val(mut node ast.EnumVal) ast.Type {
return ast.ERROR_TYPE
}
if sym.kind != .enum_ {
c.error('expected type is not an enum, got ${sym.name}', node.pos)
c.error('expected type is not an enum, got ${sym.mix_name}', node.pos)
return ast.ERROR_TYPE
}
if not sym.is_pub and sym.name.contains('.') and sym.pkg != c.pkg {
c.error('enum ${sym.name} is private', node.pos)
if not sym.is_pub and sym.mix_name.contains('.') and sym.pkg != c.pkg {
c.error('enum ${sym.mix_name} is private', node.pos)
return ast.ERROR_TYPE
}
info := sym.info as ast.EnumInfo
if not info.vals.contains(node.val) {
c.error('enum ${sym.name} has no value ${node.val}', node.pos)
c.error('enum ${sym.mix_name} has no value ${node.val}', node.pos)
return ast.ERROR_TYPE
}
node.name = sym.name
node.name = sym.mix_name
return node.typ
}

Expand Down Expand Up @@ -312,27 +312,27 @@ fun (mut c Checker) selector_expr(mut node ast.SelectorExpr) ast.Type {
if [ast.TypeKind.struct_, .interface_, .array, .string, .map].contains(sym.kind) {
field := sym.find_field(node.field_name, c.table)
if field.name.length == 0 {
c.error('${sym.name} has no field ${node.field_name}', node.pos)
c.error('${sym.mix_name} has no field ${node.field_name}', node.pos)
return ast.ERROR_TYPE
}

if not field.is_pub and sym.pkg != c.pkg {
c.error('field ${sym.name}.${node.field_name} is private', node.pos)
c.error('field ${sym.mix_name}.${node.field_name} is private', node.pos)
}

fsym := c.table.get_sym(field.typ)
if fsym.kind == .generic {
gen_info := gen_sym.info as ast.GenericInst
info := sym.info as ast.StructInfo
return gen_info.concrete_types[info.generic_names.index(fsym.name)]
return gen_info.concrete_types[info.generic_names.index(fsym.mix_name)]
}

return field.typ
}

if sym.kind == .sum_type {
// TODO check if field is present on all variants (with same type)
c.error('cast to the variant before accessing field of sumtype ${sym.name}', node.pos)
c.error('cast to the variant before accessing field of sumtype ${sym.mix_name}', node.pos)
return ast.VOID_TYPE
}

Expand Down
6 changes: 3 additions & 3 deletions lib/bait/checker/fun.bt
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ fun (mut c Checker) method_call(mut node ast.CallExpr) ast.Type {
return node.return_type
}

c.error('method ${node.mix_name} not found on type ${left_sym.name}', node.pos)
c.error('method ${node.mix_name} not found on type ${left_sym.mix_name}', node.pos)
return ast.ERROR_TYPE
}

Expand Down Expand Up @@ -334,7 +334,7 @@ fun (mut c Checker) set_conc_types(mut node ast.CallExpr, mut def ast.FunDecl) {

ret_sym := c.table.get_sym(node.return_type)
if ret_sym.kind == .generic {
idx := def.generic_names.index(ret_sym.name)
idx := def.generic_names.index(ret_sym.mix_name)
node.return_type = node.concrete_types[idx]
}
}
Expand All @@ -349,7 +349,7 @@ fun (mut c Checker) call_args(def ast.FunDecl, mut node ast.CallExpr, poffset i3
psym := c.table.get_sym(param_type)

if should_resolve_generics and psym.kind == .generic {
gi := def.generic_names.index(psym.name)
gi := def.generic_names.index(psym.mix_name)
if gi < node.concrete_types.length {
param_type = node.concrete_types[gi]
} else if gi < c.cur_concrete_types.length {
Expand Down
2 changes: 1 addition & 1 deletion lib/bait/checker/stmt.bt
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ fun (mut c Checker) for_in_loop(mut node ast.ForInLoop) {
} else if sym.kind == .string {
node.val_type = ast.U8_TYPE
} else {
c.error('cannot iterate over ${sym.name}', node.pos)
c.error('cannot iterate over ${sym.mix_name}', node.pos)
}

if node.idxvar is ast.Ident {
Expand Down
8 changes: 4 additions & 4 deletions lib/bait/checker/struct.bt
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,19 @@ fun (mut c Checker) struct_init(mut node ast.StructInit) ast.Type {
c.error('undefined struct ${node.name}', node.pos)
return ast.ERROR_TYPE
}
if not sym.is_pub and sym.name.contains('.') and sym.pkg != c.pkg {
c.error('struct ${sym.name} is private', node.pos)
if not sym.is_pub and sym.mix_name.contains('.') and sym.pkg != c.pkg {
c.error('struct ${sym.mix_name} is private', node.pos)
return ast.ERROR_TYPE
}

c.check_init_field_values(node, sym.info as ast.StructInfo)


node.name = sym.name
node.name = sym.mix_name
for field in node.fields {
def := sym.find_field(field.name, c.table)
if def.name.length == 0 {
c.error('struct ${sym.name} has no field ${field.name}', node.pos)
c.error('struct ${sym.mix_name} has no field ${field.name}', node.pos)
// Stay in the loop to also check the field expression
}

Expand Down
8 changes: 4 additions & 4 deletions lib/bait/checker/type.bt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fun (c Checker) check_types(got ast.Type, expected ast.Type) bool {
}

if exp_sym.kind == .array and got_sym.kind == .array {
if exp_sym.name == 'Array' or got_sym.name == 'Array' {
if exp_sym.mix_name == 'Array' or got_sym.mix_name == 'Array' {
return true
}
got_info := got_sym.info as ast.ArrayInfo
Expand Down Expand Up @@ -81,8 +81,8 @@ fun (c Checker) check_types(got ast.Type, expected ast.Type) bool {
}

fun (c Checker) does_type_exist(sym ast.TypeSymbol, pos token.Pos) bool {
if (sym.kind == .placeholder and sym.name != '_') or (sym.kind == .generic and not (c.cur_generic_names.contains(sym.name))) {
c.error('unknown type ${sym.name}', pos)
if (sym.kind == .placeholder and sym.mix_name != '_') or (sym.kind == .generic and not (c.cur_generic_names.contains(sym.mix_name))) {
c.error('unknown type ${sym.mix_name}', pos)
return false
}

Expand Down Expand Up @@ -134,6 +134,6 @@ fun (c Checker) does_type_exist(sym ast.TypeSymbol, pos token.Pos) bool {
return true
}

c.error('type ${sym.name} is private', pos)
c.error('type ${sym.mix_name} is private', pos)
return false
}
10 changes: 5 additions & 5 deletions lib/bait/gen/c/auto_str.bt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const LB := if os.platform() == 'windows' { '\\r\\n' } else { '\\n' }
fun (mut g Gen) get_str_fun(typ ast.Type) string {
g.table.needed_str_funs.push(typ)
sym := g.table.get_sym(typ)
return c_esc('${sym.name}_str')
return c_esc('${sym.mix_name}_str')
}

// TODO transformer?
Expand All @@ -27,7 +27,7 @@ fun (mut g Gen) generate_str_fun(typ ast.Type) {

g.generated_str_funs.push(typ)

sname := c_esc(sym.name)
sname := c_esc(sym.mix_name)
fname := sname + '_str'

if sym.kind == .struct_ {
Expand All @@ -40,14 +40,14 @@ fun (mut g Gen) generate_str_fun(typ ast.Type) {
string space = string_repeat(from_c_string(" "), indent * 2);
strings__Builder b = strings__new_builder(100);
strings__Builder_write(&b, space);
strings__Builder_write(&b, from_c_string("${sym.name}{"));\n'
strings__Builder_write(&b, from_c_string("${sym.mix_name}{"));\n'
if info.fields.length > 0 {
g.auto_funs_out += '\tstrings__Builder_write(&b, from_c_string("${LB}"));\n'
}
for field in info.fields {
if typ == field.typ {
g.auto_funs_out += '\tstrings__Builder_write(&b, space);
strings__Builder_write(&b, from_c_string(" ${field.name} = ${sym.name}{...}${LB}"));\n'
strings__Builder_write(&b, from_c_string(" ${field.name} = ${sym.mix_name}{...}${LB}"));\n'
continue
}

Expand All @@ -69,6 +69,6 @@ fun (mut g Gen) generate_str_fun(typ ast.Type) {

// TODO other types

errors.generic_error('cannot convert ${sym.name} to string')
errors.generic_error('cannot convert ${sym.mix_name} to string')
exit(1)
}
2 changes: 1 addition & 1 deletion lib/bait/gen/c/cgen.bt
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ fun (mut g Gen) c_main() {
fun (g Gen) get_concrete_name(name string, concrete_types []ast.Type) string {
mut full_name := name
for t in concrete_types {
full_name += '_' + g.table.get_sym(t).name
full_name += '_' + g.table.get_sym(t).mix_name
}
return c_esc(full_name)
}
Expand Down
2 changes: 1 addition & 1 deletion lib/bait/gen/c/eq_funs.bt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ fun (g Gen) equality_fun(typ ast.Type) string {
return ''
}

errors.generic_error('cannot generate equality function for type ${sym.name}')
errors.generic_error('cannot generate equality function for type ${sym.mix_name}')
exit(1)
}
Loading
Loading