From 3e70e5f2f1e3224a3bf8d087af26456b5e512f96 Mon Sep 17 00:00:00 2001 From: Joe Conigliaro Date: Thu, 19 Mar 2020 18:32:42 +1100 Subject: [PATCH] gen: add `in` for map and string to cgen --- vlib/v/checker/checker.v | 5 ++++- vlib/v/gen/cgen.v | 29 +++++++++++++++++++++++------ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 605f243f92..7d5531aae3 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -134,9 +134,12 @@ pub fn (c mut Checker) infix_expr(infix_expr mut ast.InfixExpr) table.Type { c.expected_type = left_type right_type := c.expr(infix_expr.right) infix_expr.right_type = right_type + right := c.table.get_type_symbol(right_type) + if infix_expr.op == .key_in && !(right.kind in [.array, .map, .string]) { + c.error('infix expr: `in` can only be used with array/map/string.', infix_expr.pos) + } 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` // the expressions have different types (array_x and x) if left.kind == .array && infix_expr.op == .left_shift { diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index b31f197bc5..ba83a56f63 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -930,12 +930,29 @@ fn (g mut Gen) infix_expr(node ast.InfixExpr) { g.write(')') } else if node.op == .key_in { - styp := g.typ(node.left_type) - g.write('_IN($styp, ') - g.expr(node.left) - g.write(', ') - g.expr(node.right) - g.write(')') + right_sym := g.table.get_type_symbol(node.right_type) + if right_sym.kind == .array { + styp := g.typ(node.left_type) + g.write('_IN($styp, ') + g.expr(node.left) + g.write(', ') + g.expr(node.right) + g.write(')') + } + else if right_sym.kind == .map { + g.write('_IN_MAP(') + g.expr(node.left) + g.write(', ') + g.expr(node.right) + g.write(')') + } + else if right_sym.kind == .string { + g.write('string_contains(') + g.expr(node.right) + g.write(', ') + g.expr(node.left) + g.write(')') + } } // arr << val else if node.op == .left_shift && g.table.get_type_symbol(node.left_type).kind == .array {