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); } } }
public DoWhileExpression parseDoWhileExpression() { DoWhileExpression doWhileExp = new DoWhileExpression(); nextTokenIfEquals(Token.TokenValue.DO); nextTokenIfEquals(Token.TokenValue.LBRACE); //do{ }while(e); if (currentTokenEquals(Token.TokenValue.RBRACE)) { doWhileExp.body = null; nextToken(); } //do{ e }while(e); else { doWhileExp.body = parseExpression(); nextTokenIfEquals(Token.TokenValue.RBRACE); } nextTokenIfEquals(Token.TokenValue.WHILE); nextTokenIfEquals(Token.TokenValue.LPAR); if (currentTokenEquals(Token.TokenValue.RPAR)) { throw new EmptyExpression(lineNumber, colNumber); } doWhileExp.condition = parseSimpleExpression(); if (doWhileExp.condition.getExpressionType() != ObjectType.booleanType) { throw new IncompatibleTypesException(lineNumber, colNumber, ObjectType.booleanType, doWhileExp.condition.getExpressionType()); } nextTokenIfEquals(Token.TokenValue.RPAR); nextTokenIfEquals(Token.TokenValue.SEMICOLON); return doWhileExp; }