Exemple #1
0
        //check if variable (int x) is declared in symbol table and add if not
        public void VisitNode(LocalVariableDeclarationStatement node)
        {
            AbstractNode typeName    = node.Child;
            AbstractNode idName      = typeName.Sib.Child;
            TypeVisitor  typeVisitor = new TypeVisitor();

            typeName.Accept(typeVisitor);
            while (idName != null)
            {
                string name = ((Identifier)idName).Name;
                try
                {
                    VariableAttributes attr = new VariableAttributes(null, typeName.TypeRef);
                    idName.TypeRef = typeName.TypeRef;
                    table.enter(name, attr);
                    idName.AttributesRef = attr;
                }
                catch (FoundKeyException e)
                {
                    Console.WriteLine(e.Message);
                    node.TypeRef       = new ErrorTypeDescriptor();
                    node.AttributesRef = null;
                }
                idName = idName.Sib;
            }
        }
Exemple #2
0
        //check if field declaration (static int x) is declared in Symbol table and add if not
        public void VisitNode(FieldVariableDeclaration node)
        {
            AbstractNode mods        = node.Child;
            AbstractNode type        = mods.Sib;
            AbstractNode dcls        = type.Sib.Child;
            TypeVisitor  typeVisitor = new TypeVisitor();

            dcls.Accept(typeVisitor);
            while (dcls != null)
            {
                string name = ((Identifier)dcls).Name;
                try
                {
                    VariableAttributes attr = new VariableAttributes(((ModifierList)mods).list, type.TypeRef);
                    dcls.TypeRef = type.TypeRef;
                    table.enter(name, attr);
                    dcls.AttributesRef = attr;
                }
                catch (FoundKeyException e)
                {
                    Console.WriteLine(e.Message);
                    node.TypeRef       = new ErrorTypeDescriptor();
                    node.AttributesRef = null;
                }
                dcls = dcls.Sib;
                Console.Write(((Identifier)dcls).Name + "   ");
                node.TypeRef.PrintType();
            }
        }