/// <summary> /// Objects to type ref. /// </summary> /// <param name="type">The type.</param> /// <returns></returns> protected CodeTypeRef ObjectToTypeRef(object type) { if (null == type) { throw new ArgumentNullException("type"); } var ctr = type as CodeTypeRef; if (ctr != null) { return(ctr); } if (type is int) { type = (LuaType)(int)type; } if (type is LuaDeclaredType) { return(new LuaCodeTypeRef(DTE, type as LuaDeclaredType)); } var stringType = type as string; if (stringType != null) { return(new LuaCodeTypeRef(DTE, LuaDeclaredType.Find(stringType))); } throw new InvalidOperationException(String.Format("Unknown type to get type from: {0} ({1})", type.GetType(), type)); }
/// <summary> /// Creates variables for fileds in a specified LuaCodeVariableTable. /// </summary> /// <param name="field">Field enumeration.</param> /// <param name="parentElement">LuaCodeVariableTable as parent element.</param> private void RecurseFieldInTable(LuaCodeVariableTable parentElement, Field field) { LuaCodeVariable element = LuaCodeElementFactory.CreateVariable (dte, parentElement, String.Empty, LuaType.Unknown, vsCMAccess.vsCMAccessPrivate, new Variable(field.Location) { Identifier = field.Identifier }); if (field.Identifier != null) { element.Name = field.Identifier.Name; } if (field.LeftExpression is Literal) { var literal = field.LeftExpression as Literal; if (String.IsNullOrEmpty(element.Name)) { element.Name = literal.Value; } //element.Type = new LuaCodeTypeRef(dte, LuaDeclaredType.Find(literal.Type.ToString())); } if (field.Expression is Literal) { var literal = field.Expression as Literal; element.InitExpression = ((Literal)field.Expression).Value; element.Type = new LuaCodeTypeRef(dte, LuaDeclaredType.Find(literal.Type.ToString())); } else { RecurseStatement(element, field.Expression); } parentElement.AddInitVariable(element); }
/// <summary> /// Adds a function to the top-level element. /// </summary> /// <param name="objectName">Name of the object.</param> /// <param name="returnType">Type of the return.</param> /// <param name="function">The function.</param> /// <returns></returns> public CodeFunction AddFunction(string objectName, LuaDeclaredType returnType, FunctionDeclaration function) { CodeFunction codeFunction = RootElement.AddFunction(objectName, vsCMFunction.vsCMFunctionFunction, returnType, Missing.Value, vsCMAccess.vsCMAccessPublic, function); return(codeFunction); }
/// <summary> /// Creates the lua code function. /// </summary> /// <param name="dte">The DTE.</param> /// <param name="parent">The parent.</param> /// <param name="name">The name.</param> /// <param name="returnType">Type of the return.</param> /// <param name="access">The access.</param> /// <param name="kind">The kind.</param> /// <param name="function">The function.</param> /// <returns></returns> public static LuaCodeFunction CreateLuaCodeFunction( DTE dte, CodeElement parent, string name, LuaType returnType, vsCMAccess access, vsCMFunction kind, FunctionDeclaration function) { var result = new LuaCodeFunction(dte, parent, name, kind, new LuaCodeTypeRef(dte, LuaDeclaredType.Find(returnType.ToString())), access, function); return(result); }
/// <summary> /// Adds fields to the table object. /// </summary> /// <param name="variableName">Field variable name.</param> /// <param name="type">Field variable type.</param> /// <param name="variable">Optional. Field variable Node object.</param> /// <returns></returns> public CodeVariable AddInitVariable(string variableName, LuaType type, Variable variable) { var result = new LuaCodeVariable(DTE, parent, variableName, new LuaCodeTypeRef(DTE, LuaDeclaredType.Find(type.ToString())), vsCMAccess.vsCMAccessPublic, variable); AddInitVariable(result); return(result); }
/// <summary> /// Creates the variable. /// </summary> /// <param name="dte">The DTE.</param> /// <param name="parent">The parent.</param> /// <param name="name">The name.</param> /// <param name="type">The type.</param> /// <param name="access">The access.</param> /// <param name="variable">The variable.</param> /// <returns></returns> public static LuaCodeVariable CreateVariable( DTE dte, CodeElement parent, string name, LuaType type, vsCMAccess access, Variable variable) { var result = new LuaCodeVariable(dte, parent, name, new LuaCodeTypeRef(dte, LuaDeclaredType.Find(type.ToString())), access, variable); 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); }