Ejemplo n.º 1
0
        public void AddLocalVariable(VarDeclarationElement elem)
        {
            m_localVars.Add(elem);

            m_currentNeededLocalVarsSpace += LanguageSymbols.Instance.GetTypeSize(elem.VarType);

            if (m_currentNeededLocalVarsSpace > m_maxNeededLocalVarsSpace)
            {
                m_maxNeededLocalVarsSpace = m_currentNeededLocalVarsSpace;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Parse function body
        /// </summary>
        /// <param name="funcInfo">Function to parse</param>
        private void ParseFunction(Parser1To2Pass.FunctionInfo funcInfo)
        {
            var funcElem = new FunctionElement();

            m_currentFunction = funcElem;

            funcElem.Info = funcInfo.Info;

            StatementListElement statements = new StatementListElement();

            m_currentBlock.Push(statements);

            foreach (var arg in funcInfo.Info.Arguments)
            {
                var declElem = new VarDeclarationElement
                {
                    VarType = arg.TypeInfo.Name,
                    VarName = arg.ArgName,
                    IgnoreInitialization = true
                };
                declElem.SetParent(statements);
                funcElem.AddArgumentVariable(declElem);
            }

            if (funcInfo.FuncLexemes.Count > 0)
            {
                for (int i = 0, end = funcInfo.FuncLexemes.Count;;)
                {
                    TreeElement element;
                    i = ParseStatement(funcInfo.FuncLexemes, i, out element);
                    if (element != null) //if null => ignore (block element)
                    {
                        var statementElem = new StatementElement();
                        statementElem.AddChild(element);

                        m_currentBlock.Peek().AddChild(statementElem);
                    }

                    if (i >= end)
                    {
                        break;
                    }
                }
            }

            funcElem.AddChild(statements);

            m_parserOutput.Functions.Add(funcElem);

            m_currentBlock.Pop();
        }
Ejemplo n.º 3
0
 public void AddVar(VarDeclarationElement elem)
 {
     Vars.Add(elem);
     AddChild(elem);
 }
Ejemplo n.º 4
0
 public void AddArgumentVariable(VarDeclarationElement elem)
 {
     m_argumentsVars.Add(elem);
 }
Ejemplo n.º 5
0
 public void AddLocalVariable(VarDeclarationElement elem)
 {
     m_localVariables.Add(elem);
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Builds variable declaration element (with optional initialization)
        /// </summary>
        /// <param name="lexemes">List of lexemes</param>
        /// <param name="pos">Position of current parsed lexeme</param>
        /// <param name="elem">Element builded on lexemes</param>
        /// <returns>Position of a next lexeme</returns>
        private int ParseVarDeclaration(List <Lexeme> lexemes, int pos, out TreeElement elem)
        {
            var multipleDecl = new MultipleVarDeclarationElement();

            var varDecl = new VarDeclarationElement();

            var  expectVar         = true;
            bool hasInitialization = false;
            bool breakCycle        = false;

            for (;;)
            {
                switch (lexemes[pos].Source)
                {
                case ",":
                {
                    Compilation.Assert(!expectVar, "Expected variable declaration, not comma", lexemes[pos].Line);

                    varDecl   = new VarDeclarationElement();
                    expectVar = true;

                    ++pos;
                    continue;
                }

                case "=":
                    hasInitialization = true;
                    breakCycle        = true;
                    ++pos;
                    break;

                case ";":
                    hasInitialization = false;
                    breakCycle        = true;
                    ++pos;
                    break;
                }

                if (breakCycle)
                {
                    break;
                }

                varDecl.VarType = lexemes[pos].Source;
                Compilation.Assert(m_symbols.IsTypeExist(varDecl.VarType), "Unknown type '" + varDecl.VarType + "'",
                                   lexemes[pos].Line);

                ++pos;
                varDecl.VarName = lexemes[pos].Source;
                Compilation.Assert(lexemes[pos].Code == Lexeme.CodeType.Name,
                                   "Invalid variable name '" + varDecl.VarName + "'", lexemes[pos].Line);

                multipleDecl.AddVar(varDecl);
                expectVar = false;

                ++pos;
            }

            if (!hasInitialization)
            {
                foreach (var variable in multipleDecl.GetVars())
                {
                    variable.InitVal = m_symbols.GetDefaultVal(variable.VarType);
                }
            }
            else
            {
                ValueElement initElements;
                pos = ParseExpression(lexemes, pos, -1,
                                      ExpressionFlags.AllowDefaultValue | ExpressionFlags.AllowAutoDefault,
                                      out initElements);

                Compilation.Assert(multipleDecl.GetVars().Count == initElements.NumberOfBranches,
                                   "Each variable associated with only one expression (variables: "
                                   + multipleDecl.GetVars().Count +
                                   ", initalization expressions: " + initElements.ChildrenCount(), lexemes[pos - 1].Line);

                for (int i = 0, end = multipleDecl.GetVars().Count; i < end; ++i)
                {
                    multipleDecl.GetVar(i).InitVal = (ValueElement)initElements.Child(i);
                }

                Compilation.Assert(lexemes[pos].Source == ";", "Did you forget ';' ?", lexemes[pos].Line);
                ++pos; //skip ';'
            }

            foreach (var i in multipleDecl.GetVars())
            {
                m_currentFunction.AddLocalVariable(i);
                m_currentBlock.Peek().AddLocalVariable(i);
            }

            elem = multipleDecl;

            return(pos);
        }