Skip to content

Commit

Permalink
if_nested
Browse files Browse the repository at this point in the history
  • Loading branch information
medvednikov committed Aug 1, 2024
1 parent b72b183 commit e381135
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 19 deletions.
9 changes: 5 additions & 4 deletions ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,11 @@ struct CaseClause {
}

struct IfStmt {
node_type_str string @[json: '_type']
cond Expr @[json: 'Cond']
body BlockStmt @[json: 'Body']
else_ Stmt @[json: 'Else']
node_type_str string @[json: '_type']
cond Expr @[json: 'Cond']
init AssignStmt @[json: 'Init']
body BlockStmt @[json: 'Body']
else_ Stmt @[json: 'Else']
}

struct ForStmt {
Expand Down
18 changes: 11 additions & 7 deletions stmt.v
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,23 @@ fn (mut app App) block_stmt(body BlockStmt) {
app.genln('}')
}

fn (mut app App) if_stmt(i IfStmt) {
fn (mut app App) if_stmt(node IfStmt) {
if node.init.tok != '' {
app.assign_stmt(node.init, false)
}

app.gen('if ')
app.expr(i.cond)
app.block_stmt(i.body)
app.expr(node.cond)
app.block_stmt(node.body)
// else if ... {
if i.else_ is IfStmt {
if node.else_ is IfStmt {
app.genln('else')
app.if_stmt(i.else_)
app.if_stmt(node.else_)
}
// else {
else if i.else_ is BlockStmt {
else if node.else_ is BlockStmt {
app.genln('else')
app.block_stmt(i.else_)
app.block_stmt(node.else_)
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/if_nested/if_nested.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ func main() {
if z := 123; z == 0 {
println("ok")
} else {
if z := 456; z == 0 {
println(z)
if x := 456; x == 0 {
println(x)
}
}
}
6 changes: 3 additions & 3 deletions tests/if_nested/if_nested.go.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
"Lhs": [
{
"_type": "Ident",
"Name": "z"
"Name": "x"
}
],
"Tok": ":=",
Expand All @@ -106,7 +106,7 @@
"_type": "BinaryExpr",
"X": {
"_type": "Ident",
"Name": "z"
"Name": "x"
},
"Op": "==",
"Y": {
Expand All @@ -129,7 +129,7 @@
"Args": [
{
"_type": "Ident",
"Name": "z"
"Name": "x"
}
]
}
Expand Down
6 changes: 3 additions & 3 deletions tests/if_nested/if_nested.vv
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ fn main() {
if z == 0 {
println('ok')
} else {
mut z_1 := 456
if z_1 == 0 {
println(z_1)
mut x := 456
if x == 0 {
println(x)
}
}
}

0 comments on commit e381135

Please sign in to comment.