cgen: simplify for in range
parent
0f251e9ede
commit
d60233b618
|
@ -715,15 +715,12 @@ fn (mut g Gen) write_defer_stmts() {
|
|||
fn (mut g Gen) for_in(it ast.ForInStmt) {
|
||||
if it.is_range {
|
||||
// `for x in 1..10 {`
|
||||
i := g.new_tmp_var()
|
||||
i := if it.val_var == '_' { g.new_tmp_var() } else { c_name(it.val_var) }
|
||||
g.write('for (int $i = ')
|
||||
g.expr(it.cond)
|
||||
g.write('; $i < ')
|
||||
g.expr(it.high)
|
||||
g.writeln('; $i++) {')
|
||||
if it.val_var != '_' {
|
||||
g.writeln('\tint ${c_name(it.val_var)} = $i;')
|
||||
}
|
||||
g.stmts(it.stmts)
|
||||
g.writeln('}')
|
||||
} else if it.kind == .array {
|
||||
|
@ -1303,14 +1300,14 @@ fn (mut g Gen) expr(node ast.Expr) {
|
|||
mut styp := it.type_name
|
||||
if it.type_name == '' {
|
||||
styp = g.typ(it.typ)
|
||||
} else {
|
||||
sym := g.table.get_type_symbol(it.typ)
|
||||
if sym.kind == .struct_ {
|
||||
info := sym.info as table.Struct
|
||||
if !info.is_typedef {
|
||||
styp = 'struct ' + styp
|
||||
}
|
||||
}
|
||||
} else {
|
||||
sym := g.table.get_type_symbol(it.typ)
|
||||
if sym.kind == .struct_ {
|
||||
info := sym.info as table.Struct
|
||||
if !info.is_typedef {
|
||||
styp = 'struct ' + styp
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
if styp.starts_with('C__') {
|
||||
|
|
|
@ -18,6 +18,14 @@ fn test_for_char_in_range() {
|
|||
assert sum == 6
|
||||
}
|
||||
|
||||
fn test_for_blank_in_range() {
|
||||
mut sum := 0
|
||||
for _ in 1 .. 3 {
|
||||
sum++
|
||||
}
|
||||
assert sum == 2
|
||||
}
|
||||
|
||||
fn test_for_char_complex() {
|
||||
mut sum := 0
|
||||
for char := 0; char < nums.len; char++ {
|
||||
|
|
Loading…
Reference in New Issue