protected override void EmitForExpression(ForExpression expression, bool isStatement = false) { Emit(expression.Initialization); AppendLine(); AppendTabs(); EmitWhileExpression(expression.Condition, expression.Body.Append(expression.Afterthought)); }
void PrintExpression(ForExpression e, int d) { SayLn("ForExpression("); Indent(d + 1); PrintDeclaration(e.VarDec, d + 1); SayLn(","); PrintExpression(e.High, d + 1); SayLn(","); PrintExpression(e.Body, d + 1); Say(")"); }
ExpressionType TranslateExpression(ForExpression e) { Env.ValueEnvironment.BeginScope(); ExpressionType init = TranslateExpression(e.VarDec.Init); if (!init.Type.CoerceTo(Types.Type._int)) { Error.Report(e.VarDec.Pos, "Loop variable must be integer"); } Access access = Level.AllocLocal(e.VarDec.Escape); Env.ValueEnvironment[e.VarDec.Name] = new LoopVariableEntry(access, init.Type); ExpressionType high = TranslateExpression(e.High); CheckInteger(e.High.Pos, high); Env.LoopEnvironment.EnterLoop(); ExpressionType body = TranslateExpression(e.Body); if (!body.Type.CoerceTo(Types.Type._void)) { Error.Report(e.Body.Pos, "For expression must return a 'void'"); } Exp exp = Translate.TranslateForExp(Level, access, init.Exp, high.Exp, body.Exp, Env.LoopEnvironment.Done()); Env.LoopEnvironment.ExitLoop(); Env.ValueEnvironment.EndScope(); return(new ExpressionType(exp, Types.Type._void)); }
public int Visit(ForExpression expression) { _writer.Write("for ("); if (expression.Initializer != null) { expression.Initializer.Statements[0].Accept(this); } _writer.Write("; "); if (expression.Condition != null) { expression.Condition.Accept(this); } _writer.Write("; "); if (expression.Increment != null) { var incrementExprs = expression.Increment.Statements; for (var i = 0; i < incrementExprs.Count; i++) { incrementExprs[i].Accept(this); if (i < incrementExprs.Count - 1) { _writer.Write(", "); } } } _writer.WriteLine(")"); expression.Block.Accept(this); return(0); }
public ForExpression Visit(ForExpression @for) { codeWriter.Write("for (", currentIndent); insideExpressionCount++; Visit(@for.Initiator); insideExpressionCount--; codeWriter.Write("; "); insideExpressionCount++; Visit(@for.Test); insideExpressionCount--; codeWriter.Write("; "); insideExpressionCount++; Visit(@for.Incrementor); insideExpressionCount--; codeWriter.Write(") {"); codeWriter.NewLine(); currentIndent++; Visit(@for.Body); currentIndent--; codeWriter.Write("}", currentIndent); codeWriter.NewLine(); return(null); }
public ForInstruction(ScopePrototype scopePrototype, ForExpression forExpression) : base(scopePrototype) { ForExpression = forExpression; Block = new Block(scopePrototype, new List <string> { forExpression.ElementName }, scopePrototype.FunctionName); }
public string Visit(ForExpression @for) { var codeWriter = new XzaarCodeWriter(); codeWriter.Write("for (", currentIndent); insideExpressionCount++; codeWriter.Write(Visit(@for.Initiator)); insideExpressionCount--; codeWriter.Write("; "); insideExpressionCount++; codeWriter.Write(Visit(@for.Test)); insideExpressionCount--; codeWriter.Write("; "); insideExpressionCount++; codeWriter.Write(Visit(@for.Incrementor)); insideExpressionCount--; codeWriter.Write(") {"); codeWriter.NewLine(); currentIndent++; codeWriter.Write(Visit(@for.Body)); currentIndent--; codeWriter.Write("}", currentIndent); codeWriter.NewLine(); return(codeWriter.ToString()); }
protected virtual Expression VisitFor(ForExpression node) { Visit(node.Test); Visit(node.Step); Visit(node.Init); Visit(node.Body); return(node); }
public object Visit(ForExpression node) { if (node.Initiator != null) { Visit(node.Initiator); } return(null); }
public virtual T Visit(ForExpression expression) { expression.Initializer.Accept(this); expression.Condition.Accept(this); expression.Block.Accept(this); expression.Increment.Accept(this); return(default(T)); }
public override Expression Visit(ForExpression expression) { PushScope(); var result = base.Visit(expression); PopScope(); return(result); }
public void TestAppendString() { var expr = new ForExpression(new VariableDefinitionExpression("i"), new VariableExpression("dict")); var builder = new StringBuilder(); expr.AppendString(builder); Assert.That(builder.ToString(), Is.EqualTo("for i in dict")); // NOTE: does not output Expressions block }
ForExpression ILexicalScope <ForExpression, Action <ParameterExpression> > .Build(Action <ParameterExpression> scope) { var result = new ForExpression(initialization, ContinueLabel, BreakLabel, condition); scope(result.LoopVar); AddStatement(Expression.Label(ContinueLabel)); iteration(result.LoopVar); result.Body = Build(); return(result); }
private ForExpression Parse(string input) { var tokenizer = new PositionalTokenizer(Tokenizer.CreateTokenizer(input)); tokenizer.Match("for"); var expr = ForExpression.Parse(tokenizer); Assert.That(expr, Is.InstanceOf <ForExpression>()); return((ForExpression)expr); }
private bool EvaluateLoop(ForExpression forExpression, InterpreterScope scope) { ExpressionBase range; if (!forExpression.Range.ReplaceVariables(scope, out range)) { Error = range as ParseErrorExpression; return(false); } var iterableExpression = range as IIterableExpression; if (iterableExpression != null) { var iterator = forExpression.IteratorName; var iteratorScope = new InterpreterScope(scope); var iteratorVariable = new VariableExpression(iterator.Name); foreach (var entry in iterableExpression.IterableExpressions()) { iteratorScope.Context = new AssignmentExpression(iteratorVariable, entry); ExpressionBase key; if (!entry.ReplaceVariables(iteratorScope, out key)) { Error = key as ParseErrorExpression; return(false); } var loopScope = new InterpreterScope(scope); loopScope.DefineVariable(iterator, key); if (!Evaluate(forExpression.Expressions, loopScope)) { return(false); } if (loopScope.IsComplete) { if (loopScope.ReturnValue != null) { scope.ReturnValue = loopScope.ReturnValue; scope.IsComplete = true; } break; } } return(true); } Error = new ParseErrorExpression("Cannot iterate over " + forExpression.Range.ToString(), forExpression.Range); return(false); }
public virtual Expression Visit(ForExpression expression) { return(new ForExpression( expression.Token, expression.Initializer != null ? (BlockExpression)expression.Initializer.Accept(this) : null, expression.Condition != null ? expression.Condition.Accept(this) : null, expression.Increment != null ? (BlockExpression)expression.Increment.Accept(this) : null, (ScopeExpression)expression.Block.Accept(this)) { EndToken = expression.EndToken }); }
private ForInstruction PopulateForInstruction(ScopePrototype scope, ForExpression forExpression) { // check if the value to be iterated over exists in the scope if (!scope.Variables.Contains(forExpression.Collection.VariableName)) { throw new SemanticsException($"Variable {forExpression.Collection.VariableName} has not been declared in the scope of operation for in function {scope.FunctionName}."); } var forInstruction = new ForInstruction(scope, forExpression); PopulateInstructions(forExpression.Instructions, forInstruction.Block.NestedBlocks, forInstruction.Block.ScopePrototype); return(forInstruction); }
private AphidObject InterpretForExpression(ForExpression expression) { EnterChildScope(); var init = InterpretExpression(expression.Initialization); while ((InterpretExpression(expression.Condition) as AphidObject).GetBool()) { EnterChildScope(); Interpret(expression.Body, false); InterpretExpression(expression.Afterthought); if (LeaveChildScope(true) || _isBreaking) { _isBreaking = false; break; } } LeaveChildScope(true); return(null); }
private ForExpression ParseForExpression() { var result = new ForExpression(); _scanner.ReadNextToken(); ExpectTokenType(TokenType.ParenthesisOpen); _scanner.ReadNextToken(); ExpectTokenType(TokenType.Identifier); result.ElementName = _scanner.Token.Value; _scanner.ReadNextToken(); ExpectTokenType(TokenType.In); _scanner.ReadNextToken(); result.Collection = ParseValueOf(); ExpectTokenType(TokenType.ParenthesisClose); _scanner.ReadNextToken(); ExpectTokenType(TokenType.PointyBracketClose); result.Instructions = ParseInstructionsTillTagClose(); ExpectSequenceOfTokens(new TokenType[] { TokenType.TagClose, TokenType.For, TokenType.PointyBracketClose }); return(result); }
void PrintExpression(ForExpression e, int d) { SayLn("ForExpression("); Indent(d + 1); PrintDeclaration(e.VarDec, d + 1); SayLn(","); PrintExpression(e.High, d + 1); SayLn(","); PrintExpression(e.Body, d + 1); Say(")"); }
public object Visit(ForExpression node) { return(null); }
public void Visit(ForExpression forExpr) { int lowerBound = 0; int upperBound = 0; object iteratee = null; if (forExpr.Iterator != null) { if (forExpr.Iterator.VariableLower != null) { var lowerVariable = _variables.FirstOrDefault(v => v.Name == forExpr.Iterator.VariableLower); if (lowerVariable == null) { ErrorManager.CreateError("Cannot iterate over non-existant list!", ErrorType.Error, forExpr.Iterator.Line, forExpr.Iterator.Column); return; } // TODO (anthony) : add checking to make sure the variable is actually a list, when lists are actually implemented. if (!(lowerVariable.Value is int)) { ErrorManager.CreateError("Lower bound of for expression is not a number!", ErrorType.Error, forExpr.Iterator.Line, forExpr.Iterator.Column); return; } lowerBound = (int)lowerVariable.Value; } if (forExpr.Iterator.VariableUpper != null) { var upperVariable = _variables.FirstOrDefault(v => v.Name == forExpr.Iterator.VariableUpper); if (upperVariable == null) { ErrorManager.CreateError("Cannot iterate over non-existant list!", ErrorType.Error, forExpr.Iterator.Line, forExpr.Iterator.Column); return; } // TODO (anthony) : add checking to make sure the variable is actually a list, when lists are actually implemented. if (!(upperVariable.Value is int)) { ErrorManager.CreateError("Upper bound of for expression is not a number!", ErrorType.Error, forExpr.Iterator.Line, forExpr.Iterator.Column); return; } upperBound = (int)upperVariable.Value; } if (forExpr.Iterator.LowerBound != null) { lowerBound = (int)forExpr.Iterator.LowerBound; } if (forExpr.Iterator.UpperBound != null) { upperBound = (int)forExpr.Iterator.UpperBound; } } else if (forExpr.Iterator == null && forExpr.ListName != null) { var variable = _variables.FirstOrDefault(v => v.Name == forExpr.ListName); if (variable.Value == null) { ErrorManager.CreateError("Cannot iterate over non-null value!", ErrorType.Error, forExpr.Iterator.Line, forExpr.Iterator.Column); return; } if (variable.Type == SailType.STR) { iteratee = (string)variable.Value; } } if (iteratee == null) { _variables.Add(new SailObject("it", lowerBound, SailType.INT)); var it = _variables.FirstOrDefault(v => v.Name == "it"); for (float iter = lowerBound; iter <= upperBound; iter++) { it.Value = iter; Visit(forExpr.Block); } } else if (iteratee is string) { _variables.Add(new SailObject("it", iteratee, SailType.STR)); var it = _variables.FirstOrDefault(v => v.Name == "it"); foreach (char c in (string)iteratee) { it.Value = c.ToString(); Visit(forExpr.Block); } } }
[DebuggerStepThrough] protected virtual void EmitForExpression(ForExpression expression, bool isStatement = false) { throw new NotImplementedException(); }
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 object Visit(ForExpression @for) { // throw new System.NotImplementedException(); }