mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
gen: add in
for map and string to cgen
This commit is contained in:
parent
28309da1f1
commit
3e70e5f2f1
@ -134,9 +134,12 @@ pub fn (c mut Checker) infix_expr(infix_expr mut ast.InfixExpr) table.Type {
|
|||||||
c.expected_type = left_type
|
c.expected_type = left_type
|
||||||
right_type := c.expr(infix_expr.right)
|
right_type := c.expr(infix_expr.right)
|
||||||
infix_expr.right_type = right_type
|
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) {
|
if !c.table.check(right_type, left_type) {
|
||||||
left := c.table.get_type_symbol(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)
|
// the expressions have different types (array_x and x)
|
||||||
if left.kind == .array && infix_expr.op == .left_shift {
|
if left.kind == .array && infix_expr.op == .left_shift {
|
||||||
|
@ -930,12 +930,29 @@ fn (g mut Gen) infix_expr(node ast.InfixExpr) {
|
|||||||
g.write(')')
|
g.write(')')
|
||||||
}
|
}
|
||||||
else if node.op == .key_in {
|
else if node.op == .key_in {
|
||||||
styp := g.typ(node.left_type)
|
right_sym := g.table.get_type_symbol(node.right_type)
|
||||||
g.write('_IN($styp, ')
|
if right_sym.kind == .array {
|
||||||
g.expr(node.left)
|
styp := g.typ(node.left_type)
|
||||||
g.write(', ')
|
g.write('_IN($styp, ')
|
||||||
g.expr(node.right)
|
g.expr(node.left)
|
||||||
g.write(')')
|
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
|
// arr << val
|
||||||
else if node.op == .left_shift && g.table.get_type_symbol(node.left_type).kind == .array {
|
else if node.op == .left_shift && g.table.get_type_symbol(node.left_type).kind == .array {
|
||||||
|
Loading…
Reference in New Issue
Block a user