From 92abcfe5b9c72cf00e8d6f80463b26f47151c870 Mon Sep 17 00:00:00 2001 From: mappu Date: Fri, 10 Apr 2020 20:08:15 +1200 Subject: [PATCH] node: implement *expr.Ternary (a hoisting pass would be better) --- node.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/node.go b/node.go index 9fb2cf8..d330bf0 100644 --- a/node.go +++ b/node.go @@ -1090,6 +1090,25 @@ func (this *conversionState) convertNoFreeFloating(n_ node.Node) (string, error) return "!(" + rhs + ")", nil + case *expr.Ternary: + cond, err := this.convert(n.Condition) + if err != nil { + return "", parseErr{n, err} + } + + iftrue, err := this.convert(n.IfTrue) + if err != nil { + return "", parseErr{n, err} + } + + iffalse, err := this.convert(n.IfFalse) + if err != nil { + return "", parseErr{n, err} + } + + // FIXME this is (A) not idiomatic, and (B) doesn't work in assignment expressions + return "(func() unknown {\nif (" + cond + ") {\nreturn (" + iftrue + ")\n} else {\nreturn (" + iffalse + ")\n } })()", nil + case *expr.ArrayDimFetch: // Might be x[foo], might be x[] (i.e. append() call) // In order to make the append() transformation, we need to lookahead