public override sealed AbstractStatement DeepCopyStatement() { ForLoop copy = new ForLoop() { PreExpression = this.PreExpression, Expression = this.Expression, PostExpression = this.PostExpression }; foreach (AbstractStatement statement in Statements) { copy.AddStatement(statement.DeepCopyStatement()); } return copy; }
public void ForLoop_Expression() { var loop = new ForLoop(); CommandHelper.TestUndoableCommand( () => Assert.IsNull(loop.Expression), () => loop.Expression = "i < 5", () => Assert.AreEqual("i < 5", loop.Expression) ); }
public void CompositeStatement_AddAction() { var loop = new WhileLoop(); var stmt = new ForLoop(); CommandHelper.TestUndoableCommand( () => { Assert.AreEqual(0, loop.Statements.Count); Assert.AreEqual(-1, loop.IndexOf(stmt)); }, () => loop.AddActionCommand.Execute(stmt), () => { Assert.AreEqual(1, loop.Statements.Count); Assert.AreEqual(0, loop.IndexOf(stmt)); } ); }
private void CreateStatement(ParseTreeNode statementNode, Event evt, CompositeStatement compositeStatement = null) { AbstractStatement statement = null; if (statementNode.Term == grammar.Action) { statement = createAction(statementNode); } else if (statementNode.Term == grammar.Condition) { ConditionGroup conditionGroup = new ConditionGroup(); conditionGroup.If.Expression = getStrVal(statementNode.ChildNodes.First(child => child.Term == grammar.Expr)); foreach (ParseTreeNode child in grammar.GetOfType(statementNode, grammar.Actions)) CreateStatement(child, evt, conditionGroup.If); foreach (ParseTreeNode elseNode in grammar.GetOfType(statementNode, grammar.Else)) { string expr = getStrVal(elseNode.ChildNodes.FirstOrDefault(child => child.Term == grammar.Expr)); AbstractCondition cond; if (null == expr) { conditionGroup.Else = new BasicCondition(); cond = conditionGroup.Else; } else { cond = new ExpressionCondition() { Expression = expr }; conditionGroup.AddStatement(cond); } foreach (ParseTreeNode child in grammar.GetOfType(elseNode, grammar.Actions)) CreateStatement(child, evt, cond); } statement = conditionGroup; } else if (statementNode.Term == grammar.Assignment) { Assignment assignment = new Assignment(); assignment.Key = getStrVal(statementNode.ChildNodes[0]); assignment.Value = getStrVal(statementNode.ChildNodes.Last()); assignment.Operator = statementNode.ChildNodes.Count == 2 ? AssignmentOperator.Assign : AbstractAssignment.AssignmentValues[grammar.OpLookup[statementNode.ChildNodes[1].Term]]; statement = assignment; } else { CompositeStatement loop = null; switch (statementNode.ChildNodes.Count) { case 2: WhileLoop wl = new WhileLoop(); wl.Expression = getStrVal(statementNode.ChildNodes[0]); statement = loop = wl; break; case 3: ForLoop fl = new ForLoop(); fl.Expression = getStrVal(statementNode.ChildNodes[0]); //First assignment, expression, assignment, actions fl.PostExpression = getStrVal(statementNode.ChildNodes[1]); statement = loop = fl; break; case 4: ForLoop forLoop = new ForLoop(); forLoop.Expression = getStrVal(statementNode.ChildNodes[1]); //First assignment, expression, assignment, actions forLoop.PostExpression = getStrVal(statementNode.ChildNodes[2]); forLoop.PreExpression = getStrVal(statementNode.ChildNodes[0]); statement = loop = forLoop; break; } foreach (ParseTreeNode child in grammar.GetOfType(statementNode, grammar.Actions)) { // TODO: This is a hack to skip the Actions found in the first line of the for() statement. if (!statementNode.ChildNodes.Contains(child)) { CreateStatement(child, evt, loop); } } } if (compositeStatement == null) evt.AddStatement(statement); else compositeStatement.AddStatement(statement); }
public void AbstractStatement_InsertBefore() { var evt = new Event(Workspace.Instance.GetPlugin(typeof(TriggerOccursEvent).FullName)); var loop = new WhileLoop(); evt.AddStatement(loop); var stmt = new ForLoop(); CommandHelper.TestUndoableCommand( () => { Assert.AreEqual(1, evt.Statements.Count); Assert.AreEqual(0, evt.IndexOf(loop)); Assert.AreEqual(-1, evt.IndexOf(stmt)); }, () => loop.InsertBeforeCommand.Execute(stmt), () => { Assert.AreEqual(2, evt.Statements.Count); Assert.AreEqual(1, evt.IndexOf(loop)); Assert.AreEqual(0, evt.IndexOf(stmt)); } ); }