Beispiel #1
0
        /// <summary>
        /// Execute
        /// </summary>
        public object VisitIf(IfExpr expr)
        {
            // Case 1: If is true
            var result = expr.Condition.Evaluate(this) as LObject;
            bool execIf = EvalHelper.IsTrue(result);
            object returnVal = LObjects.Null;
            if (execIf)
            {

                if (expr.Statements != null && expr.Statements.Count > 0)
                {
                    foreach (var stmt in expr.Statements)
                    {
                        returnVal = stmt.Evaluate(this);
                    }
                }
            }
            // Case 2: Else available to execute
            else if (expr.Else != null)
            {
                returnVal = expr.Else.Evaluate(this);
            }
            return returnVal;
        }
Beispiel #2
0
        public Expr OnParseIf()
        {
            var tokenIt = this._parser.TokenIt;
            var initiatorToken = tokenIt.NextToken;
            var expr = new IfExpr();
            // <codeIf>
            tokenIt.Expect(Tokens.If);

            // Parse the if
            this.ParseConditionalBlock(expr);
            tokenIt.AdvancePastNewLines();

            // Handle "else if" and/or else
            if (tokenIt.NextToken.Token == Tokens.Else)
            {
                // tokenIt.NextToken = "else"
                tokenIt.Advance();
                tokenIt.AdvancePastNewLines();

                // What's after else?
                // 1. "if"      = else if statement
                // 2. "{"       = multi  line else
                // 3. "nothing" = single line else
                // Peek 2nd token for else if.
                var token = tokenIt.NextToken;
                if (tokenIt.NextToken.Token == Tokens.If)
                {
                    expr.Else = OnParseIf() as BlockExpr;
                }
                else // Multi-line or single line else
                {
                    var elseStmt = new BlockExpr();
                    this._parser.ParseBlock(elseStmt);
                    this._parser.SetupContext(elseStmt, token);
                    expr.Else = elseStmt;
                }
            }
            // </codeIf>
            this._parser.SetupContext(expr, initiatorToken);
            return expr;
        }