Ejemplo n.º 1
0
        /// <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);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Recurse and translate a statement.
        /// </summary>
        /// <param name="statement">Instance of statement.</param>
        /// <param name="parentElement">Parent of statement.</param>
        private void RecurseStatement(CodeElement parentElement, Node statement)
        {
            //Trace.WriteLine("RecurseStatement::" + statement);
            if (statement == null)
            {
                return;
            }

            //--------------------------------------------------------------------
            if (statement is Block)
            {
                var block = statement as Block;
                block.StatementList.ForEach(child => RecurseStatement(parentElement, child));
            }
            //--------------------------------------------------------------------
            else if (statement is Identifier)
            {
                var identifier = statement as Identifier;
                var element    = LuaCodeElementFactory.CreateLuaCodeElement <Identifier>
                                     (dte, identifier.Name, parentElement, identifier);
                if (!isInStatement)
                {
                    Node nextNode = statement.Next;
                    while (nextNode != null)
                    {
                        isInStatement = true;
                        RecurseStatement(parentElement, nextNode);
                        nextNode = nextNode.Next;
                    }
                    isInStatement = false;
                }
                //statement.GetChildNodes().ForEach(child => RecurseStatement(element, child));
                AddElementToParent(parentElement, element);
            }
            //--------------------------------------------------------------------
            else if (statement is Literal)
            {
                var literal = statement as Literal;
                var element = LuaCodeElementFactory.
                              CreateLuaCodeElement <Literal>(dte, literal.Value, parentElement, literal);
                if (!isInStatement)
                {
                    Node nextNode = statement.Next;
                    while (nextNode != null)
                    {
                        isInStatement = true;
                        RecurseStatement(parentElement, nextNode);
                        nextNode = nextNode.Next;
                    }
                    isInStatement = false;
                }
                //literal.GetChildNodes().ForEach(child => RecurseStatement(element, child));
                AddElementToParent(parentElement, element);
            }
            //--------------------------------------------------------------------
            else if (statement is LocalDeclaration)
            {
                var localDeclaration = statement as LocalDeclaration;

                foreach (Identifier identifier in localDeclaration.IdentifierList)
                {
                    var element = LuaCodeElementFactory.CreateVariable
                                      (dte, parentElement, identifier.Name, LuaType.Unknown, true,
                                      new Variable(identifier.Location)
                    {
                        Identifier = identifier
                    });
                    AddElementToParent(parentElement, element);
                }
            }
            //--------------------------------------------------------------------
            else if (statement is Break)
            {
                var element = LuaCodeElementFactory.CreateLuaCodeElement <Break>
                                  (dte, "Break", parentElement, statement as Break);
                AddElementToParent(parentElement, element);
            }
            //--------------------------------------------------------------------
            else if (statement is Assignment)
            {
                var variables = RecurseAssignmentNode(parentElement, statement as Assignment);
                foreach (LuaCodeVariable variable in variables)
                {
                    AddElementToParent(parentElement, variable);
                }
            }
            //--------------------------------------------------------------------
            else if (statement is If)
            {
                var ifStatement = statement as If;
                var element     = LuaCodeElementFactory.CreateLuaCodeElement <If>
                                      (dte, "If", parentElement, ifStatement);
                RecurseStatement(element, ifStatement.Expression);
                RecurseStatement(element, ifStatement.ThenBlock);
                AddElementToParent(parentElement, element);
            }
            //--------------------------------------------------------------------
            else if (statement is ThenBlock)
            {
                var thenBlock = statement as ThenBlock;
                var element   = LuaCodeElementFactory.CreateLuaCodeElement <ThenBlock>
                                    (dte, "ThenBlock", parentElement, thenBlock);
                //thenBlock.GetChildNodes().ForEach(child => RecurseStatement(element, child));
                RecurseStatement(element, thenBlock.Block);
                RecurseStatement(element, thenBlock.ElseIfBlockList);
                RecurseStatement(element, thenBlock.ElseBlock);
                AddElementToParent(parentElement, element);
            }
            else if (statement is ElseIfBlock)
            {
                var elseIfBlock = statement as ElseIfBlock;
                var element     = LuaCodeElementFactory.CreateLuaCodeElement <ElseIfBlock>
                                      (dte, "ThenBlock", parentElement, elseIfBlock);
                //thenBlock.GetChildNodes().ForEach(child => RecurseStatement(element, child));
                RecurseStatement(element, elseIfBlock.Expression);
                RecurseStatement(element, elseIfBlock.Block);
                AddElementToParent(parentElement, element);
            }
            //--------------------------------------------------------------------
            else if (statement is Return)
            {
                var returnStatement = statement as Return;
                var element         = LuaCodeElementFactory.CreateLuaCodeElement <Return>
                                          (dte, "Return", parentElement, returnStatement);
                RecurseStatement(element, returnStatement.ExpressionList);
                AddElementToParent(parentElement, element);
            }
            //--------------------------------------------------------------------
            else if (statement is FunctionCall)
            {
                var functionCall = statement as FunctionCall;
                var element      = LuaCodeElementFactory.CreateLuaCodeElement <FunctionCall>
                                       (dte, functionCall.Identifier == null ? "FunctionCall" : functionCall.Identifier.Name, parentElement, functionCall);
                functionCall.GetChildNodes().ForEach(child => RecurseStatement(element, child));
                var identifier = element.Children.OfType <LuaCodeElement <Identifier> >().FirstOrDefault();
                element.Name = identifier != null ? identifier.Name : string.Empty;
                AddElementToParent(parentElement, element);
            }
            //--------------------------------------------------------------------
            else if (statement is TableConstructor)
            {
                var             tableNode = statement as TableConstructor;
                LuaCodeVariable variable  = CreateTable(tableNode, parentElement, tableNode.Name, true);
                AddElementToParent(parentElement, variable);
            }
            //--------------------------------------------------------------------
            else if (statement is ForLoop)
            {
                var loop    = statement as ForLoop;
                var element = LuaCodeElementFactory.CreateLuaCodeElement <ForLoop>
                                  (dte, loop.Name, parentElement, loop);
                //statement.GetChildNodes().ForEach<Node>(child => RecurseStatement(element, child));
                RecurseStatement(element, loop.Identifier);
                RecurseStatement(element, loop.IdentifierList);
                RecurseStatement(element, loop.Expression);
                Node subExp = loop.Expression.Next;
                while (subExp != null)
                {
                    RecurseStatement(element, subExp);
                    subExp = subExp.Next;
                }
                RecurseStatement(element, loop.Block);
                AddElementToParent(parentElement, element);
            }
            //--------------------------------------------------------------------
            else if (statement is Loop || statement is WhileLoop || statement is RepeatUntilLoop)
            {
                var loop    = statement as Loop;
                var element = LuaCodeElementFactory.CreateLuaCodeElement <Loop>
                                  (dte, loop.Name, parentElement, loop);
                RecurseStatement(element, loop.Expression);
                RecurseStatement(element, loop.Block);
                AddElementToParent(parentElement, element);
            }
            //--------------------------------------------------------------------
            else if (statement is BinaryExpression)
            {
                var exp           = statement as BinaryExpression;
                var codeStatement = LuaCodeElementFactory.CreateLuaCodeStatement
                                        (dte, String.Empty, parentElement, exp);
                RecurseStatement(codeStatement, (exp.LeftExpression));
                codeStatement.UserData = exp.Operator;
                RecurseStatement(codeStatement, (exp.RightExpression));
                AddElementToParent(parentElement, codeStatement);
            }
            //--------------------------------------------------------------------
            else if (statement is Variable)
            {
                var    variable     = statement as Variable;
                string variableName = String.Empty;
                if (variable.PrefixExpression is Identifier)
                {
                    variableName = variable.PrefixExpression == null
                                                        ? String.Empty
                                                        : ((Identifier)variable.PrefixExpression).Name;
                }
                var luaVariable = LuaCodeElementFactory.CreateVariable
                                      (dte, parentElement, variableName, LuaType.Table, vsCMAccess.vsCMAccessPrivate, variable);
                RecurseStatement(luaVariable, variable.Identifier);
                RecurseStatement(luaVariable, variable.PrefixExpression);
                RecurseStatement(luaVariable, variable.Expression);
                AddElementToParent(parentElement, luaVariable);
            }
            //--------------------------------------------------------------------
            else if (statement is UnaryExpression)
            {
                var exp           = statement as UnaryExpression;
                var codeStatement = LuaCodeElementFactory.CreateLuaCodeStatement
                                        (dte, String.Empty, parentElement, exp);
                codeStatement.UserData = exp.Operator;
                exp.GetChildNodes().ForEach(child => RecurseStatement(codeStatement, child));
                AddElementToParent(parentElement, codeStatement);
            }
            //--------------------------------------------------------------------
            else if (statement is ExplicitBlock)
            {
                var block = statement as ExplicitBlock;
                block.Block.ForEach(child => RecurseStatement(parentElement, child));
            }
            //--------------------------------------------------------------------
            else if (statement is FunctionDeclaration)
            {
                var functionNode = statement as FunctionDeclaration;
                RecurseFunctionDeclarationNode(parentElement, functionNode);
            }
            //--------------------------------------------------------------------
            else if (statement is Function)
            {
                var function    = statement as Function;
                var luaVariable = LuaCodeElementFactory.CreateVariable
                                      (dte, parentElement, function.Name, LuaType.Unknown, true,
                                      new Variable(function.Location));
                RecurseFunctionNode(function, luaVariable);
                AddElementToParent(parentElement, luaVariable);
            }
            else
            {
                Trace.WriteLine("WARNING IN STATEMENT - " + statement);
            }
        }
Ejemplo n.º 3
0
        /// <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);
        }