From 6b8c5a55bd0537168063eb80eb4645a8ce6fad2c Mon Sep 17 00:00:00 2001 From: "Adam D. Ruppe" Date: Sun, 6 Sep 2020 22:38:53 -0400 Subject: [PATCH] this code is so bad. fixed ternary... OR HAVE I? --- script.d | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/script.d b/script.d index cd0bde0..4a6b00e 100644 --- a/script.d +++ b/script.d @@ -49,6 +49,9 @@ your existing knowledge from Javascript will carry over, making it hopefully easy to use right out of the box. See the [#examples] to quickly get the feel of the script language as well as the interop. + I haven't benchmarked it, but I expect it is pretty slow. My goal is to see what is possible for easy interoperability + with dynamic functionality and D rather than speed. + $(TIP A goal of this language is to blur the line between D and script, but @@ -79,6 +82,7 @@ OVERVIEW $(LIST + * Can subclass D objects in script. See [http://dpldocs.info/this-week-in-d/Blog.Posted_2020_04_27.html#subclasses-in-script * easy interop with D thanks to arsd.jsvar. When interpreting, pass a var object to use as globals. This object also contains the global state when interpretation is done. * mostly familiar syntax, hybrid of D and Javascript @@ -2564,6 +2568,7 @@ Expression parsePart(MyTokenStreamHere)(ref MyTokenStreamHere tokens) { //tokens.popFront(); auto parenthetical = new ParentheticalExpression(parseExpression(tokens)); tokens.requireNextToken(ScriptToken.Type.symbol, ")"); + return parenthetical; case "[": // array literal @@ -2775,6 +2780,7 @@ Expression parseAddend(MyTokenStreamHere)(ref MyTokenStreamHere tokens) { case ",": // possible FIXME these are passed on to the next thing case ";": case ":": // idk + case "?": return e1; case "|>": @@ -2793,15 +2799,6 @@ Expression parseAddend(MyTokenStreamHere)(ref MyTokenStreamHere tokens) { tokens.popFront(); e1 = new BinaryExpression(peek.str, e1, parseExpression(tokens)); break; - case "?": // is this the right precedence? - auto e = new TernaryExpression(); - e.condition = e1; - tokens.requireNextToken(ScriptToken.Type.symbol, "?"); - e.ifTrue = parseExpression(tokens); - tokens.requireNextToken(ScriptToken.Type.symbol, ":"); - e.ifFalse = parseExpression(tokens); - e1 = e; - break; case "~": // FIXME: make sure this has the right associativity @@ -3217,8 +3214,19 @@ Expression parseExpression(MyTokenStreamHere)(ref MyTokenStreamHere tokens, bool //throw new ScriptCompileException("Parse error, missing finally or catch after try", tryToken.lineNumber); ret = e; - } else + } else { ret = parseAddend(tokens); + } + + if(!tokens.empty && tokens.peekNextToken(ScriptToken.Type.symbol, "?")) { + auto e = new TernaryExpression(); + e.condition = ret; + tokens.requireNextToken(ScriptToken.Type.symbol, "?"); + e.ifTrue = parseExpression(tokens); + tokens.requireNextToken(ScriptToken.Type.symbol, ":"); + e.ifFalse = parseExpression(tokens); + ret = e; + } } else { //assert(0); // return null; @@ -3423,6 +3431,7 @@ Expression parseStatement(MyTokenStreamHere)(ref MyTokenStreamHere tokens, strin case "continue": case "break": case "return": + return parseExpression(tokens); // unary prefix operators case "!":