stmt/if: implement if/elif/else
This commit is contained in:
parent
8533e72047
commit
628e4c1781
47
node.go
47
node.go
@ -466,6 +466,53 @@ func (this *conversionState) convert(n_ node.Node) (string, error) {
|
||||
|
||||
return "fmt.Print(" + quoted + ")\n", nil // newline - standalone statement
|
||||
|
||||
case *stmt.If:
|
||||
cond, err := this.convert(n.Cond)
|
||||
if err != nil {
|
||||
return "", parseErr{n, err}
|
||||
}
|
||||
|
||||
body, err := this.convert(convertToStmtList(n.Stmt))
|
||||
if err != nil {
|
||||
return "", parseErr{n, err}
|
||||
}
|
||||
|
||||
ret := "if " + cond + body
|
||||
for _, elif := range n.ElseIf {
|
||||
elif, ok := elif.(*stmt.ElseIf)
|
||||
if !ok {
|
||||
return "", parseErr{n, fmt.Errorf("expected stmt.ElseIf")}
|
||||
}
|
||||
|
||||
cond, err := this.convert(elif.Cond)
|
||||
if err != nil {
|
||||
return "", parseErr{n, err}
|
||||
}
|
||||
|
||||
body, err := this.convert(convertToStmtList(elif.Stmt))
|
||||
if err != nil {
|
||||
return "", parseErr{n, err}
|
||||
}
|
||||
|
||||
ret += " else if " + cond + body
|
||||
}
|
||||
|
||||
if n.Else != nil {
|
||||
els, ok := n.Else.(*stmt.Else)
|
||||
if !ok {
|
||||
return "", parseErr{n, fmt.Errorf("expected stmt.Else")}
|
||||
}
|
||||
|
||||
body, err := this.convert(convertToStmtList(els.Stmt))
|
||||
if err != nil {
|
||||
return "", parseErr{n, err}
|
||||
}
|
||||
|
||||
ret += " else " + body
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
|
||||
case *stmt.Nop:
|
||||
return "", nil
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user