cgen: fix [] => string casts and for in

pull/3994/head
Alexander Medvednikov 2020-03-11 19:00:51 +01:00
parent 4470252913
commit aada19f574
5 changed files with 34 additions and 29 deletions

View File

@ -128,7 +128,6 @@ pub:
pub struct Arg {
pub:
name string
is_mut bool
typ table.Type
@ -535,9 +534,11 @@ pub:
pub struct CastExpr {
pub:
typ table.Type
expr Expr // `buf`
arg Expr // `n` in `string(buf, n)`
typ table.Type // `string`
mut:
expr_type table.Type // `byteptr`
has_arg bool
}

View File

@ -536,7 +536,10 @@ fn (c mut Checker) stmt(node ast.Stmt) {
c.stmt(stmt)
}
}
// ast.ForInStmt {}
ast.ForInStmt {
c.expr(it.cond)
c.expr(it.high)
}
// ast.GlobalDecl {}
// ast.HashStmt {}
ast.Import {}
@ -584,7 +587,7 @@ pub fn (c mut Checker) expr(node ast.Expr) table.Type {
return table.bool_type
}
ast.CastExpr {
c.expr(it.expr)
it.expr_type = c.expr(it.expr)
if it.has_arg {
c.expr(it.arg)
}

View File

@ -494,7 +494,7 @@ fn (g mut Gen) expr(node ast.Expr) {
g.out.go_back(1)
}
if it.typ == table.string_type_idx {
// tos(str, len), tos2(str)
// `tos(str, len)`, `tos2(str)`
if it.has_arg {
g.write('tos(')
}
@ -502,7 +502,13 @@ fn (g mut Gen) expr(node ast.Expr) {
g.write('tos2(')
}
g.expr(it.expr)
sym := g.table.get_type_symbol(it.expr_type)
if sym.kind == .array {
// if we are casting an array, we need to add `.data`
g.write('.data')
}
if it.has_arg {
// len argument
g.write(', ')
g.expr(it.arg)
}

View File

@ -355,8 +355,7 @@ pub fn (k Kind) str() string {
'enum'
}
else {
'unknown'
}
'unknown'}
}
return k_str
}
@ -434,7 +433,6 @@ pub fn (table &Table) type_to_str(t Type) string {
res += ')'
return res
}
mut res := sym.name
if sym.kind == .array {
res = res.replace('array_', '[]')

View File

@ -294,7 +294,9 @@ pub fn (t &Table) array_fixed_name(elem_type Type, size int, nr_dims int) string
pub fn (t &Table) map_name(key_type Type, value_type Type) string {
key_type_sym := t.get_type_symbol(key_type)
value_type_sym := t.get_type_symbol(value_type)
return 'map_${key_type_sym.name}_${value_type_sym.name}' + if type_is_ptr(value_type) { '_ptr' } else { '' }
suffix := if type_is_ptr(value_type) { '_ptr' } else { '' }
return 'map_${key_type_sym.name}_${value_type_sym.name}' + suffix
// return 'map_${value_type_sym.name}' + suffix
}
pub fn (t mut Table) find_or_register_map(key_type, value_type Type) int {
@ -380,12 +382,7 @@ pub fn (t mut Table) find_or_register_multi_return(mr_typs []Type) int {
}
pub fn (t mut Table) find_or_register_fn_type(f Fn) int {
name := if f.name.len > 0 {
f.name
}
else {
'anon_$f.signature()'
}
name := if f.name.len > 0 { f.name } else { 'anon_$f.signature()' }
return t.register_type_symbol(TypeSymbol{
kind: .function
name: name