v2: checker: [] and in

pull/3785/head
Alexander Medvednikov 2020-02-19 21:13:42 +01:00
parent e30bd04579
commit 92e3e48bce
2 changed files with 11 additions and 2 deletions

View File

@ -93,11 +93,15 @@ pub fn (c mut Checker) infix_expr(infix_expr ast.InfixExpr) table.Type {
if !c.table.check(right_type, left_type) {
left := c.table.get_type_symbol(left_type)
right := c.table.get_type_symbol(right_type)
// array << elm
// `array << elm`
// the expressions have different types (array_x and x)
if left.kind == .array && infix_expr.op == .left_shift {
return table.void_type
}
// `elm in array`
if right.kind == .array && infix_expr.op == .key_in {
return table.bool_type
}
// if !c.table.check(&infix_expr.right_type, &infix_expr.right_type) {
// c.error('infix expr: cannot use `$infix_expr.right_type.name` as `$infix_expr.left_type.name`', infix_expr.pos)
c.error('infix expr: cannot use `$right.name` (right) as `$left.name`', infix_expr.pos)
@ -378,7 +382,7 @@ pub fn (c mut Checker) expr(node ast.Expr) table.Type {
return it.typ
}
ast.None {
return table.none_type
return table.none_type
}
else {}
}

View File

@ -395,6 +395,11 @@ pub fn (t &Table) check(got, expected Type) bool {
if exp_type_sym.name == 'array' || got_type_sym.name == 'array' {
return true
}
// TODO
// accept [] when an expected type is an array
if got_type_sym.kind == .array && got_type_sym.name == 'array_void' && exp_type_sym.kind == .array {
return true
}
if got_idx != exp_idx {
// && got.typ.name != expected.typ.name*/
return false