示例#1
0
文件: Parser.cs 项目: zjloscar/Dynamo
        void forloop(out Node forloop)
        {
            Node        node;
            ForLoopNode loopNode = new ForLoopNode();
            NodeList    body     = null;

            Expect(23);
            Expect(8);
            arrayident(out node);
            loopNode.id = node;
            Expect(34);
            expr(out node);
            loopNode.expression = node;
            Expect(9);
            if (StartOf(2))
            {
                Node singleStmt = null;
                stmt(out singleStmt);
                loopNode.body.Add(singleStmt);
            }
            else if (la.kind == 32)
            {
                Get();
                stmtlist(out body);
                loopNode.body = body;
                Expect(33);
            }
            else
            {
                SynErr(57);
            }
            forloop = loopNode;
        }
示例#2
0
        public override DynValue Visit(ForLoopNode forLoopNode)
        {
            Environment.EnterScope();
            {
                foreach (var assignment in forLoopNode.Assignments)
                {
                    Visit(assignment);
                }

                try
                {
                    Environment.LoopStack.Push(new LoopFrame());
                    while (true)
                    {
                        var conditionEvaluation = Visit(forLoopNode.Condition);
                        if (!conditionEvaluation.IsTruth)
                        {
                            break;
                        }

                        var loopStackTop = Environment.LoopStack.Peek();

                        if (loopStackTop.BreakLoop)
                        {
                            break;
                        }

                        if (!loopStackTop.SkipThisIteration)
                        {
                            Environment.EnterScope();
                            {
                                ExecuteStatementList(forLoopNode.StatementList);
                            }
                            Environment.ExitScope();
                        }

                        foreach (var iterationStatement in forLoopNode.IterationStatements)
                        {
                            Visit(iterationStatement);
                        }

                        if (loopStackTop.SkipThisIteration)
                        {
                            loopStackTop.SkipThisIteration = false;
                        }
                    }
                }
                finally
                {
                    Environment.LoopStack.Pop();
                }
            }
            Environment.ExitScope();

            return(DynValue.Zero);
        }
        public void visitForLoop(ForLoopNode node)
        {
            IdentifierNode identifier   = (IdentifierNode)node.getChildren()[0];
            string         variableName = identifier.getVariableName();

            if (!variableAlreadyDeclared(variableName))
            {
                throw new SemanticException("The control variable '" + variableName + "' in for loop has not been declared.");
            }
            this.forLoopControlVariables.Push(variableName);
            node.getChildren()[2].accept(this);
            this.forLoopControlVariables.Pop();
        }
示例#4
0
        public void TranslateStartForLoopNode(ForLoopNode node)
        {
            // Translate all the pure nodes this node depends on in
            // the correct order
            TranslateDependentPureNodes(node);

            builder.AppendLine($"{GetOrCreatePinName(node.IndexPin)} = {GetPinIncomingValue(node.InitialIndexPin)};");
            builder.AppendLine($"if({GetOrCreatePinName(node.IndexPin)} < {GetPinIncomingValue(node.MaxIndexPin)})");
            builder.AppendLine("{");
            WritePushJumpStack(node.ContinuePin);
            WriteGotoOutputPin(node.LoopPin);
            builder.AppendLine("}");
        }
        //NB: This one might break, there was talk about refactoring all for loops to while loops behind the scenes
        protected override CrawlSyntaxNode VisitForLoop(ForLoopNode forLoop)
        {
            GenericScope scope = new GenericScope(new[]
            {
                new KeyValuePair <string, TypeInformation>(
                    forLoop.LoopVariable.Value,
                    new TypeInformation(
                        forLoop.Loopvariable.ActualType,
                        ProtectionLevel.NotApplicable,
                        forLoop.LoopVariable.Interval.a,
                        DeclaringScope.MethodLike)
                    ),
            });

            ForLoopNode afterVisit = (ForLoopNode)base.VisitForLoop(forLoop);

            return(afterVisit.WithScope(scope));
        }
        public override void Visit(ForLoopNode node)
        {
            SymbolTable.SetCurrentNode(node);

            SymbolTable.OpenScope(BlockType.ForLoop);
            if (node.VariableDeclaration != null)
            {
                node.VariableDeclaration.Accept(this);
            }
            node.ToValueOperation.Accept(this);

            if (node.Increment != null)
            {
                node.Increment.Accept(this);
            }
            VisitChildren(node);
            SymbolTable.CloseScope();
        }
        public void CreateForLoopMethod()
        {
            // Create method
            forLoopMethod = new Method("ForLoop")
            {
                Modifiers = MethodModifiers.Public
            };

            // Create nodes
            LiteralNode maxIndexLiteralNode = new LiteralNode(forLoopMethod, typeof(int), 10);
            ForLoopNode forLoopNode         = new ForLoopNode(forLoopMethod);

            // Connect exec nodes
            GraphUtil.ConnectExecPins(forLoopMethod.EntryNode.InitialExecutionPin, forLoopNode.ExecutionPin);
            GraphUtil.ConnectExecPins(forLoopNode.CompletedPin, forLoopMethod.ReturnNode.ReturnPin);

            // Connect node data
            GraphUtil.ConnectDataPins(maxIndexLiteralNode.ValuePin, forLoopNode.MaxIndexPin);
        }
示例#8
0
        public override ImperativeNode VisitForLoopNode(ForLoopNode node)
        {
            var newLoopVar = node.LoopVariable.Accept(this);

            if (node.LoopVariable != newLoopVar)
            {
                node.LoopVariable = newLoopVar;
            }

            var newExpr = node.Expression.Accept(this);

            if (node.Expression != newExpr)
            {
                node.Expression = newExpr;
            }

            node.Body = VisitNodeList(node.Body);
            return(node);
        }
        public void CreateForLoopMethod()
        {
            // Create method
            forLoopMethod = new Method("ForLoop")
            {
                Visibility = MemberVisibility.Public
            };

            // Create nodes
            LiteralNode maxIndexLiteralNode = LiteralNode.WithValue(forLoopMethod, 10);
            ForLoopNode forLoopNode         = new ForLoopNode(forLoopMethod);

            // Connect exec nodes
            GraphUtil.ConnectExecPins(forLoopMethod.EntryNode.InitialExecutionPin, forLoopNode.ExecutionPin);
            GraphUtil.ConnectExecPins(forLoopNode.CompletedPin, forLoopMethod.ReturnNodes.First().ReturnPin);

            // Connect node data
            GraphUtil.ConnectDataPins(maxIndexLiteralNode.ValuePin, forLoopNode.MaxIndexPin);
        }
        public void visitForLoop(ForLoopNode forLoopNode)
        {
            IdentifierNode    identifier      = (IdentifierNode)forLoopNode.getChildren()[0];
            string            controlVariable = identifier.getVariableName();
            RangeOperatorNode rangeNode       = (RangeOperatorNode)forLoopNode.getChildren()[1];
            StatementListNode forStatements   = (StatementListNode)forLoopNode.getChildren()[2];

            rangeNode.getChildren()[0].accept(this);
            int begin = popInt();

            rangeNode.getChildren()[1].accept(this);
            int end = popInt();

            for (int i = begin; i <= end; i++)
            {
                this.symbolTable.updateVariable(controlVariable, i);
                foreach (INode child in forStatements.getChildren())
                {
                    child.accept(this);
                }
            }
        }
        public void visitForLoop(ForLoopNode node)
        {
            RangeOperatorNode rangeOperatorNode = (RangeOperatorNode)node.getChildren()[1];

            this.typeStack = new Stack <MiniPLTokenType>();
            INode leftExpression = rangeOperatorNode.getChildren()[0];

            this.typeStack.Push(MiniPLTokenType.TYPE_IDENTIFIER_INTEGER);
            accessChildren(leftExpression);
            if (this.typeStack.Pop() != MiniPLTokenType.TYPE_IDENTIFIER_INTEGER)
            {
                throw new SemanticException("Expected an integer. Wrong type in the left hand side of range operator.");
            }
            INode rightExpression = rangeOperatorNode.getChildren()[1];

            this.typeStack.Clear();
            this.typeStack.Push(MiniPLTokenType.TYPE_IDENTIFIER_INTEGER);
            accessChildren(rightExpression);
            if (this.typeStack.Pop() != MiniPLTokenType.TYPE_IDENTIFIER_INTEGER)
            {
                throw new SemanticException("Expected an integer. Wrong type in the right hand side of range operator.");
            }
        }
示例#12
0
 public abstract DynValue Visit(ForLoopNode forLoopNode);
示例#13
0
        public ForLoopNode(ForLoopNode rhs) : base(rhs)
        {
            Body = new List<ImperativeNode>();
            foreach (ImperativeNode iNode in rhs.Body)
            {
                ImperativeNode newNode = NodeUtils.Clone(iNode);
                Body.Add(newNode);
            }
            LoopVariable = NodeUtils.Clone(rhs.LoopVariable);
            Expression = NodeUtils.Clone(rhs.Expression);

            KwForLine = rhs.KwForLine;
            KwForCol = rhs.KwForCol;
            KwInLine = rhs.KwInLine;
            KwInCol = rhs.KwInCol;
        }
示例#14
0
 public AddressNode VisitForLoop(ForLoopNode forLoop)
 {
     throw new System.NotImplementedException();
 }
示例#15
0
 public virtual void VisitForLoopNode(ForLoopNode node)
 {
     DefaultVisit(node);
 }
示例#16
0
        public ForLoopNode(ForLoopNode rhs) : base(rhs)
        {
            body = new List<ImperativeNode>();
            foreach (ImperativeNode iNode in rhs.body)
            {
                ImperativeNode newNode = ProtoCore.Utils.NodeUtils.Clone(iNode);
                body.Add(newNode);
            }
            loopVar = ProtoCore.Utils.NodeUtils.Clone(rhs.loopVar);
            expression = ProtoCore.Utils.NodeUtils.Clone(rhs.expression);

            KwForLine = rhs.KwForLine;
            KwForCol = rhs.KwForCol;
            KwInLine = rhs.KwInLine;
            KwInCol = rhs.KwInCol;
        }
 public virtual bool VisitForLoopNode(ForLoopNode node)
 {
     return(DefaultVisit(node));
 }
        public override DynValue Visit(ForLoopNode forLoopNode)
        {
            _ = Visit(forLoopNode.Assignment).Number;

            DynValue step = new DynValue(1);

            var targetValue = Visit(forLoopNode.TargetValue);

            if (forLoopNode.Step != null)
            {
                step = Visit(forLoopNode.Step);
            }

            var isLocalScope = false;

            var assignmentNode = (forLoopNode.Assignment as AssignmentNode);
            var iteratorName   = assignmentNode.Variable.Name;

            if (assignmentNode.LocalScope)
            {
                isLocalScope = true;
            }

            try
            {
                LoopStack.Push(new LoopStackItem());
                while (true)
                {
                    ExecuteStatementList(forLoopNode.StatementList).GetAwaiter().GetResult();

                    double iterator;
                    if (isLocalScope)
                    {
                        var callStackTop = CallStack.Peek();
                        iterator = callStackTop.LocalVariableScope[iteratorName].Number;
                    }
                    else
                    {
                        iterator = Environment.Globals[iteratorName].Number;
                    }

                    if (step.Number < 0)
                    {
                        if (iterator < targetValue.Number)
                        {
                            break;
                        }
                        else
                        {
                            iterator -= step.Number;
                        }
                    }
                    else
                    {
                        if (iterator >= targetValue.Number)
                        {
                            break;
                        }
                        else
                        {
                            iterator += step.Number;
                        }
                    }

                    if (isLocalScope)
                    {
                        // no throw on localvar token, so
                        // guaranteed we're inside a function

                        var callStackTop = CallStack.Peek();
                        callStackTop.LocalVariableScope[iteratorName] = new DynValue(iterator);
                    }
                    else
                    {
                        Environment.Globals[iteratorName] = new DynValue(iterator);
                    }

                    var loopStackTop = LoopStack.Peek();

                    if (loopStackTop.BreakLoop)
                    {
                        break;
                    }

                    if (loopStackTop.SkipThisIteration)
                    {
                        loopStackTop.SkipThisIteration = false;
                        continue;
                    }
                }
            }
            catch
            {
                throw;
            }
            finally
            {
                LoopStack.Pop();
            }

            return(DynValue.Zero);
        }
 public string VisitForLoop(ForLoopNode forLoop)
 {
     throw new NotImplementedException();
 }
示例#20
0
 public virtual T VisitForLoop(ForLoopNode forLoop)
 {
     throw new NotImplementedException();
 }
示例#21
0
 public virtual XzaarExpression Visit(ForLoopNode loop)
 {
     return(null);
 }
示例#22
0
 public virtual ForLoopNode Visit(ForLoopNode loop)
 {
     return(loop);
 }