cgen: sum type cast & map str() receiver

pull/4093/head
Joe Conigliaro 2020-03-21 23:57:11 +11:00
parent 0f1371e1c9
commit f7042e9038
4 changed files with 37 additions and 14 deletions

View File

@ -333,7 +333,7 @@ pub fn (c mut Checker) method_call_expr(method_call_expr mut ast.MethodCallExpr)
} }
// TODO: str methods // TODO: str methods
if typ_sym.kind in [.map] && name == 'str' { if typ_sym.kind in [.map] && name == 'str' {
method_call_expr.receiver_type = typ method_call_expr.receiver_type = table.new_type(c.table.type_idxs['map_string'])
method_call_expr.return_type = table.string_type method_call_expr.return_type = table.string_type
return table.string_type return table.string_type
} }
@ -997,9 +997,16 @@ pub fn (c mut Checker) index_expr(node mut ast.IndexExpr) table.Type {
} }
value_type := c.table.value_type(typ) value_type := c.table.value_type(typ)
if value_type != table.void_type { if value_type != table.void_type {
if c.is_amp {
return table.type_to_ptr(value_type)
}
return value_type return value_type
} }
} }
// TODO: handle these globally, not individually
if c.is_amp {
return table.type_to_ptr(typ)
}
return typ return typ
} }

View File

@ -1525,8 +1525,11 @@ fn (g mut Gen) call_args(args []ast.CallArg) {
} }
if arg.expected_type != 0 { if arg.expected_type != 0 {
g.ref_or_deref_arg(arg) g.ref_or_deref_arg(arg)
g.expr_with_cast(arg.expr, arg.typ, arg.expected_type)
} }
else {
g.expr(arg.expr) g.expr(arg.expr)
}
if i != args.len - 1 { if i != args.len - 1 {
g.write(', ') g.write(', ')
} }
@ -1670,7 +1673,9 @@ void* obj;
int typ; int typ;
} $name;') } $name;')
} }
else {} else {
g.typedefs.writeln('#define _type_idx_$name $i')
}
} }
} }
} }
@ -1730,15 +1735,22 @@ fn (g mut Gen) string_inter_literal(node ast.StringInterLiteral) {
if i >= node.exprs.len { if i >= node.exprs.len {
continue continue
} }
match node.expr_types[i] { // TODO: fix match, sum type false positive
table.string_type { // match node.expr_types[i] {
// table.string_type {
// g.write('%.*s')
// }
// table.int_type {
// g.write('%d')
// }
// else {}
// }
if node.expr_types[i] == table.string_type {
g.write('%.*s') g.write('%.*s')
} }
table.int_type { else if node.expr_types[i] == table.int_type {
g.write('%d') g.write('%d')
} }
else {}
}
} }
g.write('", ') g.write('", ')
// Build args // Build args

View File

@ -72,8 +72,8 @@ int main(int argc, char** argv) {
user.name = tos3("bob"); user.name = tos3("bob");
Option_int n = get_opt(); Option_int n = get_opt();
int a = /*opt*/(*(int*)n.data) + 3; int a = /*opt*/(*(int*)n.data) + 3;
handle_expr((IfExpr){ handle_expr(/* sum type cast */ (Expr) {.obj = memdup(&(IfExpr[]) {(IfExpr){
0}); 0}}, sizeof(IfExpr)), .typ = 25});
return 0; return 0;
} }

View File

@ -1685,6 +1685,7 @@ fn (p mut Parser) global_decl() ast.GlobalDecl {
fn (p mut Parser) match_expr() ast.MatchExpr { fn (p mut Parser) match_expr() ast.MatchExpr {
p.check(.key_match) p.check(.key_match)
pos := p.tok.position()
is_mut := p.tok.kind == .key_mut is_mut := p.tok.kind == .key_mut
mut is_sum_type := false mut is_sum_type := false
if is_mut { if is_mut {
@ -1695,6 +1696,7 @@ fn (p mut Parser) match_expr() ast.MatchExpr {
mut branches := []ast.MatchBranch mut branches := []ast.MatchBranch
for { for {
mut exprs := []ast.Expr mut exprs := []ast.Expr
branch_pos := p.tok.position()
p.open_scope() p.open_scope()
// final else // final else
if p.tok.kind == .key_else { if p.tok.kind == .key_else {
@ -1739,6 +1741,7 @@ fn (p mut Parser) match_expr() ast.MatchExpr {
branches << ast.MatchBranch{ branches << ast.MatchBranch{
exprs: exprs exprs: exprs
stmts: stmts stmts: stmts
pos: branch_pos
} }
p.close_scope() p.close_scope()
if p.tok.kind == .rcbr { if p.tok.kind == .rcbr {
@ -1750,6 +1753,7 @@ fn (p mut Parser) match_expr() ast.MatchExpr {
branches: branches branches: branches
cond: cond cond: cond
is_sum_type: is_sum_type is_sum_type: is_sum_type
pos: pos
} }
} }