コード例 #1
0
        public void SetParent(TreeElement parent, int index = -1)
        {
            if (m_parent != null)
            {
                if (index == -1)
                {
                    index = m_parent.m_children.IndexOf(this);
                }
                m_parent.SetChild(m_parent.m_children.IndexOf(this), null);
            }

            m_parent = parent;

            if (m_parent == null)
            {
                return;
            }

            if (m_parent.ChildIndex(this) == -1)
            {
                m_parent.SetChild(index, this);
            }
        }
コード例 #2
0
 public void AddChild(TreeElement child)
 {
     SetChild(m_children.Count, child);
 }
コード例 #3
0
 public int ChildIndex(TreeElement child)
 {
     return(m_children.IndexOf(child));
 }
コード例 #4
0
ファイル: ParserSecondPass.cs プロジェクト: valsydalv/ALang
        /// <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);
        }