parser/cgen: typeof test fixes - match & fixed array order

pull/4179/head
joe-conigliaro 2020-04-02 02:13:21 +11:00
parent bd8d51fc95
commit 6764c7dd5c
No known key found for this signature in database
GPG Key ID: C12F7136C08206F1
2 changed files with 29 additions and 14 deletions

View File

@ -176,14 +176,6 @@ pub fn (g mut Gen) write_typedef_types() {
styp := typ.name.replace('.', '__')
g.definitions.writeln('typedef array $styp;')
}
.array_fixed {
styp := typ.name.replace('.', '__')
// array_fixed_char_300 => char x[300]
mut fixed := styp[12..]
len := styp.after('_')
fixed = fixed[..fixed.len - len.len - 1]
g.definitions.writeln('typedef $fixed $styp [$len];')
}
.map {
styp := typ.name.replace('.', '__')
g.definitions.writeln('typedef map $styp;')
@ -956,7 +948,8 @@ fn (g mut Gen) expr(node ast.Expr) {
// &Foo(0) => ((Foo*)0)
g.out.go_back(1)
}
if it.typ == table.string_type_idx {
sym := g.table.get_type_symbol(it.typ)
if sym.kind == .string {
// `tos(str, len)`, `tos2(str)`
if it.has_arg {
g.write('tos(')
@ -965,8 +958,8 @@ 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 {
expr_sym := g.table.get_type_symbol(it.expr_type)
if expr_sym.kind == .array {
// if we are casting an array, we need to add `.data`
g.write('.data')
}
@ -977,6 +970,9 @@ fn (g mut Gen) expr(node ast.Expr) {
}
g.write(')')
}
else if sym.kind == .sum_type {
g.expr_with_cast(it.expr, it.expr_type, it.typ)
}
else {
// styp := g.table.type_to_str(it.typ)
styp := g.typ(it.typ)
@ -1153,6 +1149,11 @@ fn (g mut Gen) typeof_expr(node ast.TypeOf) {
g.expr(node.expr)
g.write(').typ ))')
}
else if sym.kind == .array_fixed {
fixed_info := sym.info as table.ArrayFixed
elem_sym := g.table.get_type_symbol(fixed_info.elem_type)
g.write('tos3("[$fixed_info.size]${elem_sym.name}")')
}
else {
g.write('tos3("${sym.name}")')
}
@ -2089,6 +2090,16 @@ fn (g mut Gen) write_types(types []table.TypeSymbol) {
void* obj;
int typ;
} $name;')
}
table.ArrayFixed {
// .array_fixed {
styp := typ.name.replace('.', '__')
// array_fixed_char_300 => char x[300]
mut fixed := styp[12..]
len := styp.after('_')
fixed = fixed[..fixed.len - len.len - 1]
g.definitions.writeln('typedef $fixed $styp [$len];')
// }
}
else {}
}
@ -2108,11 +2119,15 @@ fn (g &Gen) sort_structs(types []table.TypeSymbol) []table.TypeSymbol {
// create list of deps
mut field_deps := []string
match t.info {
table.ArrayFixed {
dep := g.table.get_type_symbol(it.elem_type).name
if dep in type_names {
field_deps << dep
}
}
table.Struct {
info := t.info as table.Struct
for field in info.fields {
// Need to handle fixed size arrays as well (`[10]Point`)
// ft := if field.typ.starts_with('[') { field.typ.all_after(']') } else { field.typ }
dep := g.table.get_type_symbol(field.typ).name
// skip if not in types list or already in deps
if !(dep in type_names) || dep in field_deps || table.type_is_ptr(field.typ) {

View File

@ -1784,7 +1784,7 @@ fn (p mut Parser) match_expr() ast.MatchExpr {
p.next()
}
// Sum type match
else if p.tok.kind == .name && (p.tok.lit[0].is_capital() || p.peek_tok.kind == .dot) {
else if p.tok.kind == .name && (p.tok.lit in table.builtin_type_names || p.tok.lit[0].is_capital() || p.peek_tok.kind == .dot) {
// if sym.kind == .sum_type {
// p.warn('is sum')
// TODO `exprs << ast.Type{...}`