From 628e4c1781eb74c3ac211c7acf593a2cb4e41bc6 Mon Sep 17 00:00:00 2001 From: mappu Date: Sun, 5 Apr 2020 19:41:02 +1200 Subject: [PATCH] stmt/if: implement if/elif/else --- node.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/node.go b/node.go index 65e4fb5..3bdfb43 100644 --- a/node.go +++ b/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