/// <summary> /// run step 123. /// </summary> /// <returns></returns> public override Expr Parse() { var startToken = _tokenIt.NextToken; var conditionToken = startToken; var assignToken = startToken; var incToken = startToken; var loopToken = startToken; var varToken = startToken; _tokenIt.ExpectIdText("repeat"); Expr varnameExpr = null; Expr startVal = null; Expr endVal = null; Expr incVal = null; Operator op = Operator.LessThanEqual; // Case 1: repeat to 10 if (_tokenIt.NextToken.Token.Text == "to") { var result = ParseTo(); startVal = Exprs.Const(new LNumber(1.0), startToken); op = result.Item1; endVal = result.Item2; incVal = result.Item3; } // Case 2: repeat 1 to 10 else if (_tokenIt.NextToken.Token.Kind == TokenKind.LiteralNumber) { var num = _tokenIt.ExpectNumber(); var result = ParseTo(); startVal = Exprs.Const(new LNumber(num), startToken); op = result.Item1; endVal = result.Item2; incVal = result.Item3; } // Case 3: repeat ndx to 10 else if (_tokenIt.NextToken.Token.Kind == TokenKind.Ident) { var variableName = _tokenIt.ExpectId(); varnameExpr = Exprs.Ident(variableName, _tokenIt.LastToken); if (_tokenIt.NextToken.Token.Type == TokenTypes.Assignment) { _tokenIt.Advance(); // Upto "to" startVal = ParseExpr(_terminatorForTo); } else { startVal = Exprs.Const(new LNumber(0), startToken); } var result = ParseTo(); op = result.Item1; endVal = result.Item2; incVal = result.Item3; } // auto-create variable name. if (varnameExpr == null) { varnameExpr = Exprs.Ident("it", _tokenIt.LastToken); } // Now setup the stmts var start = Exprs.Assign(true, varnameExpr, startVal, varToken); var condition = Exprs.Compare(varnameExpr, op, endVal, varToken); var incExp = Exprs.Unary(varnameExpr.ToQualifiedName(), incVal, 0, Operator.PlusEqual, incToken); var incStmt = Exprs.Assign(false, varnameExpr, incExp, assignToken); var loopStmt = Exprs.For(start, condition, incStmt, loopToken); ParseBlock(loopStmt as BlockExpr); return(loopStmt); }
private void AddAssignment(bool expectVar, Expr varExp, Expr valExp, List <AssignExpr> declarations, TokenData token) { var a = (AssignExpr)Exprs.Assign(expectVar, varExp, valExp, token); declarations.Add(a); }