Exemplo n.º 1
0
        public void evalExpression(Expression exp)
        {
            if (exp == null)
            {
                return;
            }

            if (exp is InstructionExpression)
            {
                evalInstruction((InstructionExpression)exp);

                return;
            }
            else if (exp is SequenceExpression)
            {
                SequenceExpression seqExp = (SequenceExpression)exp, seqTmp;
                Expression expTmp;
                int index = insertIndex;

                seqTmp = new SequenceExpression(new List<Expression>(seqExp.exps));

                seqs.Add(seqExp);
                runningSeqs.Add(seqTmp);
                index = runningSeqs.Count - 1;

                while (seqTmp.exps.Count > 0)
                {
                    expTmp = seqTmp.exps.ElementAt(0);
                    evalExpression(expTmp);

                    seqTmp.exps.RemoveAt(0);

                    if (!shouldEvalSeq)
                    {
                        if (seqTmp.exps.Count == 0)
                        {
                            runningSeqs.RemoveAt(index);
                        }

                        return;
                    }
                }

                runningSeqs.RemoveAt(runningSeqs.Count - 1);

                removeDeclaredVariables(seqs.ElementAt(seqs.Count - 1).declaredVariables());
                seqs.RemoveAt(seqs.Count - 1);

                return;
            }
            else if (exp is IfExpression)
            {
                ifExp = (IfExpression)exp;

                o1 = evalSimpleExpression(ifExp.condition);

                if ((bool)o1.value)
                {
                    evalExpression(ifExp.body);

                    return;
                }
                if (ifExp.elseIfList != null)
                {
                    foreach (ElseIfExpression elseIfExp in ifExp.elseIfList)
                    {
                        o1 = evalSimpleExpression(elseIfExp.condition);

                        if ((bool)o1.value)
                        {
                            evalExpression(elseIfExp.corps);

                            return;
                        }
                    }
                }
                if (ifExp.elseBlock != null)
                {
                    evalExpression(ifExp.elseBlock.body);

                    return;
                }

                return;
            }
            else if (exp is SwitchExpression)
            {
                switchExp = (SwitchExpression)exp;

                o1 = evalSimpleExpression(switchExp.exp);

                foreach (SwitchCaseExpression sce in switchExp.cases)
                {
                    if (sce is CaseExpression)
                    {
                        if (o1.type == ObjectType.stringType
                            && isCatched((string)o1.value, sce, false))
                        {
                            return;
                        }
                        else if (o1.type == ObjectType.intType
                            && isCatched((int)o1.value, sce, false))
                        {
                            return;
                        }

                    }
                    else if (sce is DefaultExpression)
                    {
                        evalExpression(((DefaultExpression)sce).body);
                        return;
                    }
                }

                return;
            }
            else if (exp is WhileExpression)
            {
                whileExp = (WhileExpression)exp;

                while (true)
                {
                    o1 = evalSimpleExpression(whileExp.condition);

                    if (!(bool)o1.value)
                    {
                        return;
                    }

                    evalExpression(whileExp.body);
                }
            }
            else if (exp is DoWhileExpression)
            {
                doWhileExp = (DoWhileExpression)exp;

                do
                {
                    evalExpression(doWhileExp.body);

                    o1 = evalSimpleExpression(doWhileExp.condition);

                    if (!(bool)o1.value)
                    {
                        return;
                    }

                } while (true);
            }
            else if (exp is ForExpression)
            {
                forExp = (ForExpression)exp;

                evalExpression(forExp.init);

                while (true)
                {
                    o1 = evalSimpleExpression(forExp.condition);

                    if (!(bool)o1.value)
                    {
                        return;
                    }

                    evalExpression(forExp.body);
                    evalExpression(forExp.maj);
                }
            }
        }
Exemplo n.º 2
0
        public SwitchExpression parseSwitchExpression()
        {
            SwitchExpression switchExp = new SwitchExpression();

            nextTokenIfEquals(Token.TokenValue.SWITCH);
            nextTokenIfEquals(Token.TokenValue.LPAR);

            if (currentTokenEquals(Token.TokenValue.RPAR))
            {
                throw new EmptyExpression(lineNumber, colNumber);
            }

            switchExp.exp = parseSimpleExpression();

            nextTokenIfEquals(Token.TokenValue.RPAR);
            nextTokenIfEquals(Token.TokenValue.LBRACE);

            while (currentTokenEquals(Token.TokenValue.CASE))
            {
                if (switchExp.hasDefaultCase)
                {
                    throw new CaseException(lineNumber, colNumber);
                }

                switchExp.addCase(parseCase());
            }

            if (currentTokenEquals(Token.TokenValue.DEFAULT))
            {
                if (switchExp.hasDefaultCase)
                {
                    throw new DefaultCaseException(lineNumber, colNumber);
                }

                switchExp.addCase(parseCase());
            }

            nextTokenIfEquals(Token.TokenValue.RBRACE);

            return switchExp;
        }