2019-10-13 15:37:43 +02:00
|
|
|
module compiler
|
2019-10-06 03:27:29 +02:00
|
|
|
|
|
|
|
// `a in [1,2,3]` => `a == 1 || a == 2 || a == 3`
|
|
|
|
// avoid allocation
|
|
|
|
// `typ` is the type of `a`
|
|
|
|
// `ph` is for string_eq()
|
|
|
|
fn (p mut Parser) in_optimization(typ string, ph int) {
|
|
|
|
p.check(.lsbr)
|
2019-12-07 01:05:19 +01:00
|
|
|
if p.tok == .rsbr {
|
|
|
|
p.error('`x in []` is always false')
|
|
|
|
}
|
2019-10-06 03:27:29 +02:00
|
|
|
mut i := 0
|
|
|
|
// Get `a` expr value (can be a string literal, not a variable)
|
2019-10-27 08:03:15 +01:00
|
|
|
expr := p.cgen.cur_line[ph..]
|
2019-10-06 03:27:29 +02:00
|
|
|
is_str := typ == 'string'
|
|
|
|
//println('!! $p.expr_var.name => $name ($typ)')
|
|
|
|
for p.tok != .rsbr && p.tok != .eof {
|
|
|
|
if i > 0 {
|
|
|
|
if is_str {
|
|
|
|
p.gen(' || string_eq($expr, ')
|
|
|
|
} else {
|
|
|
|
p.gen(' || $expr == ')
|
|
|
|
}
|
2019-11-27 06:48:04 +01:00
|
|
|
}
|
2019-10-06 03:27:29 +02:00
|
|
|
if i == 0 {
|
|
|
|
if is_str {
|
2019-11-27 06:48:04 +01:00
|
|
|
p.cgen.set_placeholder(ph, ' (string_eq(')
|
2019-10-06 03:27:29 +02:00
|
|
|
p.gen(', ')
|
|
|
|
} else {
|
2019-11-27 06:48:04 +01:00
|
|
|
p.cgen.set_placeholder(ph, ' (')
|
2019-10-06 03:27:29 +02:00
|
|
|
p.gen(' ==')
|
2019-11-27 06:48:04 +01:00
|
|
|
}
|
|
|
|
}
|
2019-10-06 03:27:29 +02:00
|
|
|
p.check_types(p.bool_expression(), typ)
|
|
|
|
if is_str {
|
|
|
|
p.gen(')')
|
2019-11-27 06:48:04 +01:00
|
|
|
}
|
2019-10-06 03:27:29 +02:00
|
|
|
if p.tok != .rsbr {
|
|
|
|
p.check(.comma)
|
2019-12-18 05:52:14 +01:00
|
|
|
p.fspace()
|
2019-10-06 03:27:29 +02:00
|
|
|
}
|
|
|
|
i++
|
2019-11-27 06:48:04 +01:00
|
|
|
}
|
|
|
|
p.gen(')')
|
2019-10-06 03:27:29 +02:00
|
|
|
p.check(.rsbr)
|
|
|
|
}
|