public void ParallelEnumerator_Next_WithValidData_Enumerates() { int[] data1 = new[] { 1, 2, 3, 4, 5 }; int[] data2 = new[] { 6, 7, 8 }; int[] data3 = new[] { 9, 10, 11, 12 }; var enumerator = new ParallelEnumerator <int>(data1, data2, data3); Assert.That(enumerator.Next(), Is.True); Assert.That(enumerator.Current, Is.EquivalentTo(new[] { 1, 6, 9 })); Assert.That(enumerator.Next(), Is.True); Assert.That(enumerator.Current, Is.EquivalentTo(new[] { 2, 7, 10 })); Assert.That(enumerator.Next(), Is.True); Assert.That(enumerator.Current, Is.EquivalentTo(new[] { 3, 8, 11 })); Assert.That(enumerator.Next(), Is.True); Assert.That(enumerator.Current, Is.EquivalentTo(new[] { 4, 0, 12 })); Assert.That(enumerator.Next(), Is.True); Assert.That(enumerator.Current, Is.EquivalentTo(new[] { 5, 0, 0 })); Assert.That(enumerator.Next(), Is.False); }
private static ArbitraryNumber SubstractPositives(ArbitraryNumber a, ArbitraryNumber b) { var num1 = new ArbitraryNumber(a); var num2 = new ArbitraryNumber(b); AlignFractionalDigits(num1, num2); var result = new ArbitraryNumber(); var enumerator = new ParallelEnumerator <int>(num1.Digits.AsEnumerable().Reverse(), num2.Digits.AsEnumerable().Reverse()); int carryOver = 0; while (enumerator.Next()) { int val1 = enumerator.Current[0]; int val2 = enumerator.Current[1]; int sub = val1 - val2 - carryOver; int val = 0; if (sub < 0) { val = 10 + sub; carryOver = 1; } else { val = sub; carryOver = 0; } result.Digits.Insert(0, val); } if (carryOver > 0) { result.Digits.Insert(0, carryOver); result.IsNegative = true; } result.DecimalsCount = num1.DecimalsCount; Trim(result); return(result); }
/// <summary> /// Gets an enumeration of declarations that the node declares. /// </summary> /// <returns>An enumeration of declarations.</returns> public IEnumerable<Declaration> GetDeclarations() { if (VariableList != null && ExpressionList != null) { // Iterate through the two linked lists in parallel and infer the type from the expression. var enumerator = new ParallelEnumerator<Node, Node>(VariableList, ExpressionList); while (enumerator.MoveNext()) { if (enumerator.CurrentFirst is Identifier) { var identifier = (Identifier)enumerator.CurrentFirst; DeclarationType declarationType = DeclarationType.Variable; string type = null; if (enumerator.CurrentSecond is Function) { declarationType = DeclarationType.Function; } if (enumerator.CurrentSecond is TableConstructor) { declarationType = DeclarationType.Table; type = ((TableConstructor)enumerator.CurrentSecond).Name; } if (enumerator.CurrentSecond is Literal) { type = ((Literal)enumerator.CurrentSecond).Type.ToString(); } yield return new Declaration { DeclarationType = declarationType, Name = identifier.Name, IsLocal = this.IsLocal, Type = type }; } } } }
/// <summary> /// Gets an enumeration of declarations that the node declares. /// </summary> /// <returns>An enumeration of declarations.</returns> public IEnumerable <Declaration> GetDeclarations() { if (VariableList != null && ExpressionList != null) { // Iterate through the two linked lists in parallel and infer the type from the expression. var enumerator = new ParallelEnumerator <Node, Node>(VariableList, ExpressionList); while (enumerator.MoveNext()) { if (enumerator.CurrentFirst is Identifier) { var identifier = (Identifier)enumerator.CurrentFirst; DeclarationType declarationType = DeclarationType.Variable; string type = null; if (enumerator.CurrentSecond is Function) { declarationType = DeclarationType.Function; } if (enumerator.CurrentSecond is TableConstructor) { declarationType = DeclarationType.Table; type = ((TableConstructor)enumerator.CurrentSecond).Name; } if (enumerator.CurrentSecond is Literal) { type = ((Literal)enumerator.CurrentSecond).Type.ToString(); } yield return(new Declaration { DeclarationType = declarationType, Name = identifier.Name, IsLocal = this.IsLocal, Type = type }); } } } }
private static ArbitraryNumber AddPositives(ArbitraryNumber a, ArbitraryNumber b) { var num1 = new ArbitraryNumber(a); var num2 = new ArbitraryNumber(b); AlignFractionalDigits(num1, num2); var result = new ArbitraryNumber(); var enumerator = new ParallelEnumerator <int>(num1.Digits.AsEnumerable().Reverse(), num2.Digits.AsEnumerable().Reverse()); int carryOver = 0; while (enumerator.Next()) { int val1 = enumerator.Current[0]; int val2 = enumerator.Current[1]; int added = val1 + val2 + carryOver; int val = added % 10; carryOver = (added - val) / 10; result.Digits.Insert(0, val); } if (carryOver > 0) { result.Digits.Insert(0, carryOver); } result.DecimalsCount = num1.DecimalsCount; Trim(result); return(result); }
/// <summary> /// Recurse and translate in Assignment Node. /// </summary> /// <param name="assignmentNode">Instance of Assignment Node.</param> /// <param name="parentElement">Parent of CodeElement.</param> private IEnumerable<LuaCodeVariable> RecurseAssignmentNode(CodeElement parentElement, Assignment assignmentNode) { var variables = new List<LuaCodeVariable>(); if (assignmentNode.ExpressionList == null) { assignmentNode.VariableList.ForEach( child => { if (child is Identifier) { var identifier = (Identifier) child; var element = LuaCodeElementFactory.CreateLuaCodeElement<Identifier> (dte, identifier.Name, parentElement, identifier); AddElementToParent(parentElement, element); } else { Trace.WriteLine("ERROR IN VariableList. " + child); } }); } else { var enumerator = new ParallelEnumerator<Node, Node> (assignmentNode.VariableList, assignmentNode.ExpressionList); while (enumerator.MoveNext()) { if (enumerator.CurrentFirst is Identifier) { var identifier = (Identifier) enumerator.CurrentFirst; if (enumerator.CurrentSecond is FunctionCall) { LuaCodeVariable variable = LuaCodeElementFactory.CreateVariable (dte, parentElement, identifier.Name, LuaType.Unknown, assignmentNode.IsLocal, new Variable(PrepareLocation(identifier.Location, enumerator.CurrentSecond.Location)) {Identifier = identifier}); var functionCall = enumerator.CurrentSecond as FunctionCall; RecurseStatement(variable, functionCall); variable.InitExpression = variable.Children.Item(1); variable.Type = new LuaCodeTypeRef(dte, LuaDeclaredType.Unknown); variables.Add(variable); } else if (enumerator.CurrentSecond is TableConstructor) { var constructor = enumerator.CurrentSecond as TableConstructor; var variable = CreateTable(constructor, parentElement, identifier, assignmentNode.IsLocal); variables.Add(variable); } else if (enumerator.CurrentSecond is Literal) { var literal = enumerator.CurrentSecond as Literal; var variable = LuaCodeElementFactory.CreateVariable (dte, parentElement, identifier.Name, literal.Type, assignmentNode.IsLocal, new Variable(PrepareLocation(identifier.Location, enumerator.CurrentSecond.Location)) {Identifier = identifier}); variable.InitExpression = literal.Value; variable.Type = new LuaCodeTypeRef(dte, LuaDeclaredType.Find(literal.Type.ToString())); variables.Add(variable); } else if (enumerator.CurrentSecond is Variable) { var variableNode = enumerator.CurrentSecond as Variable; var variable = LuaCodeElementFactory.CreateVariable (dte, parentElement, identifier.Name, LuaType.Table, assignmentNode.IsLocal, new Variable(PrepareLocation(identifier.Location, enumerator.CurrentSecond.Location)) {Identifier = identifier}); //variable.GetChildNodes().ForEach(child => RecurseStatement(luaVariable, child)); RecurseStatement(variable, variableNode.Identifier); RecurseStatement(variable, variableNode.PrefixExpression); RecurseStatement(variable, variableNode.Expression); variables.Add(variable); } else if (enumerator.CurrentSecond is Function) { var function = enumerator.CurrentSecond as Function; var variable = LuaCodeElementFactory.CreateVariable (dte, parentElement, identifier.Name, LuaType.Unknown, assignmentNode.IsLocal, new Variable(PrepareLocation(identifier.Location, enumerator.CurrentSecond.Location)) {Identifier = identifier}); RecurseFunctionNode(function, variable); variables.Add(variable); } else { var variable = LuaCodeElementFactory.CreateVariable (dte, parentElement, identifier.Name, LuaType.Unknown, assignmentNode.IsLocal, new Variable(PrepareLocation(identifier.Location, enumerator.CurrentSecond.Location)) {Identifier = identifier}); RecurseStatement(variable, enumerator.CurrentSecond); variables.Add(variable); //Trace.WriteLine("ERROR IN ASSIGNMENT. " + enumerator.CurrentSecond); } } else if (enumerator.CurrentFirst is Variable) { var variableNode = enumerator.CurrentFirst as Variable; string variableName = String.Empty; if (variableNode.PrefixExpression is Identifier) { variableName = variableNode.PrefixExpression == null ? String.Empty : ((Identifier) variableNode.PrefixExpression).Name; } var variable = LuaCodeElementFactory.CreateVariable (dte, parentElement, variableName, LuaType.Table, assignmentNode.IsLocal, variableNode); RecurseStatement(variable, variableNode.Identifier); RecurseStatement(variable, variableNode.PrefixExpression); RecurseStatement(variable, variableNode.Expression); if (enumerator.CurrentSecond is Function) { RecurseFunctionNode(enumerator.CurrentSecond as Function, variable); } else { RecurseStatement(variable, enumerator.CurrentSecond); } variables.Add(variable); } else { Trace.WriteLine("ERROR IN FIRST ASSIGNMENT. " + enumerator.CurrentFirst); } } } return variables; }
/// <summary> /// Recurse and translate in Assignment Node. /// </summary> /// <param name="assignmentNode">Instance of Assignment Node.</param> /// <param name="parentElement">Parent of CodeElement.</param> private IEnumerable <LuaCodeVariable> RecurseAssignmentNode(CodeElement parentElement, Assignment assignmentNode) { var variables = new List <LuaCodeVariable>(); if (assignmentNode.ExpressionList == null) { assignmentNode.VariableList.ForEach( child => { if (child is Identifier) { var identifier = (Identifier)child; var element = LuaCodeElementFactory.CreateLuaCodeElement <Identifier> (dte, identifier.Name, parentElement, identifier); AddElementToParent(parentElement, element); } else { Trace.WriteLine("ERROR IN VariableList. " + child); } }); } else { var enumerator = new ParallelEnumerator <Node, Node> (assignmentNode.VariableList, assignmentNode.ExpressionList); while (enumerator.MoveNext()) { if (enumerator.CurrentFirst is Identifier) { var identifier = (Identifier)enumerator.CurrentFirst; if (enumerator.CurrentSecond is FunctionCall) { LuaCodeVariable variable = LuaCodeElementFactory.CreateVariable (dte, parentElement, identifier.Name, LuaType.Unknown, assignmentNode.IsLocal, new Variable(PrepareLocation(identifier.Location, enumerator.CurrentSecond.Location)) { Identifier = identifier }); var functionCall = enumerator.CurrentSecond as FunctionCall; RecurseStatement(variable, functionCall); variable.InitExpression = variable.Children.Item(1); variable.Type = new LuaCodeTypeRef(dte, LuaDeclaredType.Unknown); variables.Add(variable); } else if (enumerator.CurrentSecond is TableConstructor) { var constructor = enumerator.CurrentSecond as TableConstructor; var variable = CreateTable(constructor, parentElement, identifier, assignmentNode.IsLocal); variables.Add(variable); } else if (enumerator.CurrentSecond is Literal) { var literal = enumerator.CurrentSecond as Literal; var variable = LuaCodeElementFactory.CreateVariable (dte, parentElement, identifier.Name, literal.Type, assignmentNode.IsLocal, new Variable(PrepareLocation(identifier.Location, enumerator.CurrentSecond.Location)) { Identifier = identifier }); variable.InitExpression = literal.Value; variable.Type = new LuaCodeTypeRef(dte, LuaDeclaredType.Find(literal.Type.ToString())); variables.Add(variable); } else if (enumerator.CurrentSecond is Variable) { var variableNode = enumerator.CurrentSecond as Variable; var variable = LuaCodeElementFactory.CreateVariable (dte, parentElement, identifier.Name, LuaType.Table, assignmentNode.IsLocal, new Variable(PrepareLocation(identifier.Location, enumerator.CurrentSecond.Location)) { Identifier = identifier }); //variable.GetChildNodes().ForEach(child => RecurseStatement(luaVariable, child)); RecurseStatement(variable, variableNode.Identifier); RecurseStatement(variable, variableNode.PrefixExpression); RecurseStatement(variable, variableNode.Expression); variables.Add(variable); } else if (enumerator.CurrentSecond is Function) { var function = enumerator.CurrentSecond as Function; var variable = LuaCodeElementFactory.CreateVariable (dte, parentElement, identifier.Name, LuaType.Unknown, assignmentNode.IsLocal, new Variable(PrepareLocation(identifier.Location, enumerator.CurrentSecond.Location)) { Identifier = identifier }); RecurseFunctionNode(function, variable); variables.Add(variable); } else { var variable = LuaCodeElementFactory.CreateVariable (dte, parentElement, identifier.Name, LuaType.Unknown, assignmentNode.IsLocal, new Variable(PrepareLocation(identifier.Location, enumerator.CurrentSecond.Location)) { Identifier = identifier }); RecurseStatement(variable, enumerator.CurrentSecond); variables.Add(variable); //Trace.WriteLine("ERROR IN ASSIGNMENT. " + enumerator.CurrentSecond); } } else if (enumerator.CurrentFirst is Variable) { var variableNode = enumerator.CurrentFirst as Variable; string variableName = String.Empty; if (variableNode.PrefixExpression is Identifier) { variableName = variableNode.PrefixExpression == null ? String.Empty : ((Identifier)variableNode.PrefixExpression).Name; } var variable = LuaCodeElementFactory.CreateVariable (dte, parentElement, variableName, LuaType.Table, assignmentNode.IsLocal, variableNode); RecurseStatement(variable, variableNode.Identifier); RecurseStatement(variable, variableNode.PrefixExpression); RecurseStatement(variable, variableNode.Expression); if (enumerator.CurrentSecond is Function) { RecurseFunctionNode(enumerator.CurrentSecond as Function, variable); } else { RecurseStatement(variable, enumerator.CurrentSecond); } variables.Add(variable); } else { Trace.WriteLine("ERROR IN FIRST ASSIGNMENT. " + enumerator.CurrentFirst); } } } return(variables); }