public override Statement VisitForRange(RheaParser.ForRangeContext context) { var range = new Range { Start = new ExpressionBuilder(parentBlock).Visit(context.range().start), End = new ExpressionBuilder(parentBlock).Visit(context.range().end) }; var newForRange = new ForRange { ParentBlock = parentBlock, ParentScope = parentScope, Range = range, Iterator = new VariableDeclaration { Name = context.name().GetText(), ParentBlock = parentBlock, Type = range.InferredType } }; var newBlock = new Block { ParentBlock = parentBlock, ParentScope = newForRange }; newBlock.Statements = context.block()._statements.Select(s => new StatementBuilder(newBlock, newForRange.Block).Visit(s)); newForRange.Block = newBlock; return(newForRange); }
public void Save_Workflow() { var path = $@"..\..\..\Activities\XAMLs\{nameof(Save_Workflow)}.xaml"; var expected = File.ReadAllText(path); var wf = new SequentialWorkflow { Title = "Test" }; wf.Activities.Add(new Delay { Timeout = 300 }); var forRange = new ForRange { Start = 3, Count = 10 }; forRange.Activities.Add(new Click { Position = new Point(0, 123) }); forRange.Activities.Add(new KeyStroke { Timeout = 500, Key = Keys.Control | Keys.A }); wf.Activities.Add(forRange); Assert.AreEqual(expected, XamlServices.Save(wf)); var o = (SequentialWorkflow)XamlServices.Parse(expected); }
public bool Visit(ForRange node) { node.Start.Accept(this); node.Finish.Accept(this); return(true); }
public void GetPi_Methods() { var context = new WorkflowContext(); context.Variables.Add(new Variable <double> { VariableName = "sum", Value = 1.0 }); context.Variables.Add(new Variable <double> { VariableName = "p", Value = 1.0 }); var wf = new SequentialWorkflow(); var forRange = new ForRange { Start = 3, Count = 50, Step = 2 }; forRange.Activities.Add(new InvokeMethod { Type = typeof(SequentialWorkflowTest), MethodName = "NextP" }); forRange.Activities.Add(new InvokeMethod { Type = typeof(SequentialWorkflowTest), MethodName = "AddTerm" }); wf.Activities.Add(forRange); wf.Activities.Add(new InvokeMethod { Type = typeof(SequentialWorkflowTest), MethodName = "Sqrt12" }); wf.Execute(context); AssertNearlyEqual(Math.PI, context.Variables.Get <double>("pi")); }
public void GetPi_Expression() { var context = new WorkflowContext(); context.Variables.Add(new Variable <double> { VariableName = "sum", Value = 1.0 }); context.Variables.Add(new Variable <double> { VariableName = "p", Value = 1.0 }); var wf = new SequentialWorkflow(); var forRange = new ForRange { Start = 3, Count = 50, Step = 2 }; forRange.Activities.Add(new Expression { Text = "p *= -3" }); forRange.Activities.Add(new Expression { Text = "sum += 1 / (i * p)" }); wf.Activities.Add(forRange); wf.Activities.Add(new Expression { Text = "pi = Math.Sqrt(12) * sum" }); wf.Execute(context); AssertNearlyEqual(Math.PI, context.Variables.Get <double>("pi")); }
public void GetSqrt_Expression() { var context = new WorkflowContext(); context.Variables.Add(new Variable <double> { VariableName = "a", Value = 5.0 }); var wf = new SequentialWorkflow(); wf.Activities.Add(new Expression { Text = "x = a" }); var forRange = new ForRange { Count = 100 }; forRange.Activities.Add(new Expression { Text = "xi = (x + a / x) / 2" }); var ifReturn = new If { Condition = "x == xi" }; ifReturn.Activities.Add(new Return()); forRange.Activities.Add(ifReturn); forRange.Activities.Add(new Expression { Text = "x = xi" }); wf.Activities.Add(forRange); wf.Execute(context); AssertNearlyEqual(Math.Sqrt(5.0), context.Variables.Get <double>("x")); }
public AstPrinterNode Visit(ForRange node) { var printer = new AstPrinterNode(node.ToString()); printer.AddChild(node.Start.Accept(this)); printer.AddChild(node.Finish.Accept(this)); return(printer); }
/// <summary> /// Executes a for loop in which iterations may run in parallel. /// </summary> /// <param name="fromInclusive">The start index, inclusive.</param> /// <param name="toExclusive">The end index, exclusive.</param> /// <param name="body">The delegate that is invoked once per iteration.</param> public static void For(int fromInclusive, int toExclusive, Action <int> body) { Thread[] threads = new Thread[NumThread]; for (int i = 0; i < NumThread; i++) { threads[i] = new Thread(arg => { ForRange range = (ForRange)arg !; for (int j = range.Start; j < range.End; j++) { body(j); } }); } for (int i = 0; i < NumThread; i++) { threads[i].Start(new ForRange(fromInclusive, toExclusive, i)); } for (int i = 0; i < NumThread; i++) { threads[i].Join(); } }
public ForLoop(string identifier, ForRange forRange, Body body) { Identifier = identifier; ForRange = forRange; Body = body; }