Exemplo n.º 1
0
        private void VisitNode(LocalVariableDeclarationStatement node)
        {
            AbstractNode typeSpecifier            = node.Child;
            AbstractNode localVariableDeclarators = typeSpecifier.Sib;

            typeSpecifier.Accept(this);
            TypeDescriptor declType = typeSpecifier.TypeDescriptor;

            AbstractNode identifier = localVariableDeclarators.Child;

            while (identifier != null)
            {
                Identifier id = (Identifier)identifier;
                if (Table.isDeclaredLocally(id.ID))
                {
                    id.TypeDescriptor = new ErrorDescriptor("Symbol table " +
                                                            "already contains id: " + id.ID);
                    id.AttributesRef = null;
                }
                else
                {
                    VariableDeclarationAttributes attr =
                        new VariableDeclarationAttributes();
                    attr.Kind           = Kind.VariableAttributes;
                    attr.TypeDescriptor = declType;
                    attr.IsAssignable   = true;
                    attr.Modifiers      = null;
                    Table.enter(id.ID, attr);
                    id.TypeDescriptor = declType;
                    id.AttributesRef  = attr;
                }
                identifier = identifier.Sib;
            }
        }
Exemplo n.º 2
0
        private void VisitNode(FieldVariableDeclaration node)
        {
            AbstractNode modifiers     = node.Child;
            AbstractNode typeSpecifier = modifiers.Sib;
            AbstractNode fieldVarDecls = typeSpecifier.Sib;

            typeSpecifier.Accept(TypeVisitor);

            TypeDescriptor        declType      = typeSpecifier.TypeDescriptor;
            List <ModifiersEnums> modifiersList =
                ((Modifiers)modifiers).ModifierTokens;

            // note: grammar doesn't allow initialization at declaration
            // add each identifier in the list to the symbol table
            AbstractNode fieldVarDeclName = fieldVarDecls.Child;

            while (fieldVarDeclName != null)
            {
                Identifier identifier = fieldVarDeclName.Child as Identifier;
                if (identifier == null)
                {
                    string msg = "Expected Identifier type, found " +
                                 nameof(identifier);
                    fieldVarDeclName.Child.TypeDescriptor =
                        new ErrorDescriptor(msg);
                }
                else
                {
                    string id = identifier.ID;
                    // if variable already declared locally, assign an error
                    if (Table.isDeclaredLocally(id))
                    {
                        identifier.TypeDescriptor = new ErrorDescriptor(
                            "Variable name cannot be redeclared: " + id);
                        identifier.AttributesRef = null;
                    }
                    else
                    {
                        VariableDeclarationAttributes attr =
                            new VariableDeclarationAttributes();
                        attr.Kind           = Kind.VariableAttributes;
                        attr.TypeDescriptor = identifier.TypeDescriptor;
                        attr.Modifiers      = modifiersList;
                        attr.IsAssignable   = true;
                        Table.enter(id, attr);
                        identifier.TypeDescriptor = declType;
                        identifier.AttributesRef  = attr;
                    }
                }
                fieldVarDeclName = fieldVarDeclName.Sib;
            }
            VisitChildren(node);
        }