Example #1
0
        public IExpression Parse(Parser parser, Token token)
        {
            var condition = parser.ParseExpression(1);

            var ifBlock = parser.ParseExpression(1);

            if (ifBlock == null)
            {
                ErrorManager.CreateError("If statement is missing a body! ({ ... })", ErrorType.Error, token.Line, token.Column);
                return(null);
            }

            var elseIfBlocks = new List <ElseIfExpression>();

            if (parser.TokenStream.Peek().Type == TokenType.ELSEIF)
            {
                while (parser.TokenStream.Peek().Type == TokenType.ELSEIF)
                {
                    var elseIfExpr = parser.ParseExpression(1);

                    elseIfBlocks.Add((ElseIfExpression)elseIfExpr);
                }
            }

            ElseExpression elseExpr = null;

            if (parser.TokenStream.Peek().Type == TokenType.ELSE)
            {
                elseExpr = parser.ParseExpression(1) as ElseExpression;
            }

            return(new IfExpression(token.Line, token.Column, (BlockExpression)ifBlock, condition, elseIfBlocks, elseExpr));
        }
Example #2
0
        private ElseExpression ParseElseExpression()
        {
            var result = new ElseExpression();

            _scanner.ReadNextToken();
            ExpectTokenType(TokenType.PointyBracketClose);
            result.Instructions = ParseInstructionsTillTagClose();
            ExpectSequenceOfTokens(new TokenType[] { TokenType.TagClose, TokenType.Else, TokenType.PointyBracketClose });
            return(result);
        }
 public override int GetHashCode()
 {
     unchecked
     {
         var hashCode = 10 + Condition.GetHashCode();
         hashCode = (hashCode * 397) ^ ThenExpression.GetHashCode();
         hashCode = (hashCode * 397) ^ ElseExpression.GetHashCode();
         return(hashCode);
     }
 }
Example #4
0
        public static Result Run(ConditionalBlockExpression block, Scope scope)
        {
            foreach (BaseIfExpression if_expr in block.Ifs)
            {
                Result if_condition_result = Interpreters.Execute(if_expr.Condition, scope);

                if (if_condition_result.IsTrue())
                {
                    Result if_body_result = Interpreters.Execute(if_expr.Body, scope);
                    return(if_body_result);
                }
            }

            ElseExpression else_expr = block.Else;

            if (else_expr != null)
            {
                Result else_body_result = Interpreters.Execute(else_expr.Body, scope);
                return(else_body_result);
            }

            return(Result.NULL);
        }
Example #5
0
        private static ConditionalBlockExpression ToConditionalBlockExpression(IfStmt e)
        {
            ImmutableArray <BaseIfExpression> if_expr = ImmutableArray <BaseIfExpression> .Empty;
            ElseExpression else_expr = null;

            foreach (var c in e.Conditions)
            {
                if (c.Condition == null)
                {
                    if (else_expr == null)
                    {
                        else_expr = ToElseExpression(c);
                    }
                    else
                    {
                        throw new ArgumentOutOfRangeException($"Multiple else expressions ???");
                    }
                }
                else
                {
                    if (if_expr.Length == 0)
                    {
                        if_expr = if_expr.Add(ToIfExpression(c));
                    }
                    else
                    {
                        if_expr = if_expr.Add(ToElseIfExpression(c));
                    }
                }
            }

            return(new ConditionalBlockExpression(
                       if_expr,
                       else_expr
                       ));
        }
        private IfInstruction PopulateIfCondition(ScopePrototype scope, IfExpression ifExpression, ElseExpression elseExpression)
        {
            var ifInstruction = new IfInstruction(scope, ifExpression);

            PopulateInstructions(ifExpression.Instructions, ifInstruction.IfBlock, scope);
            if (elseExpression != null)
            {
                PopulateInstructions(elseExpression.Instructions, ifInstruction.ElseBlock, scope);
            }
            return(ifInstruction);
        }
Example #7
0
 public ConditionalBlockExpression(ImmutableArray <BaseIfExpression> if_exprs, ElseExpression else_expr)
 {
     Ifs  = if_exprs;
     Else = else_expr;
 }