/// <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 }); } } } }
/// <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); }