コード例 #1
0
        // TODO: structs added as requirement
        // TypeDeclaring (n/a only for array and struct types)

        private void VisitNode(ClassDeclaration node)
        {
            AbstractNode modifiers  = node.Child;
            AbstractNode identifier = modifiers.Sib;
            AbstractNode classBody  = identifier.Sib;

            ClassTypeDescriptor classDesc = new ClassTypeDescriptor();

            classDesc.ClassBody = new ScopeTable();
            Modifiers mods = modifiers as Modifiers;

            if (mods != null)
            {
                classDesc.Modifiers = mods.ModifierTokens;
            }
            else
            {
                node.TypeDescriptor = new ErrorDescriptor("Expected " +
                                                          "modifier node, found: " + nameof(node));
            }

            Attr attr = new Attr();

            attr.Kind           = Kind.ClassType;
            attr.TypeDescriptor = classDesc;
            string id = ((Identifier)identifier).ID;

            Table.enter(id, attr);
            CurrentClass        = classDesc;
            node.TypeDescriptor = classDesc;
            node.AttributesRef  = attr;

            // push the class body scope table onto the symbol table stack
            Table.openScope
                (((ClassTypeDescriptor)attr.TypeDescriptor).ClassBody);
            AbstractNode fieldDecls = classBody.Child;

            // grammar allows one class: if body empty, parsing complete
            if (fieldDecls == null)
            {
                return;
            }
            fieldDecls.Accept(this);
            Table.closeScope();
            CurrentClass = null;
        }
コード例 #2
0
        private void VisitNode(PrimitiveTypeBoolean node)
        {
            Attributes attr = new Attr(node.TypeDescriptor);

            node.AttributesRef = attr;
        }
コード例 #3
0
        private void VisitNode(MethodCall node)
        {
            AbstractNode methodReference = node.Child;
            AbstractNode argumentList    = methodReference.Sib; // may be null

            TypeDescriptor descriptor;

            QualifiedName qualifiedName = methodReference as QualifiedName;

            if (qualifiedName == null)
            {
                descriptor = new ErrorDescriptor
                                 ("Only Qualified Name supported for Method Call reference");
            }
            else
            {
                // get parameters from method call
                List <TypeDescriptor> argListTypes =
                    GetParameterTypes(argumentList as ArgumentList);

                // get parameters (signature) from declared method
                Attributes           attr             = Table.lookup(qualifiedName.GetStringName());
                MethodTypeDescriptor methodDescriptor =
                    attr.TypeDescriptor as MethodTypeDescriptor;

                if (methodDescriptor != null)
                {
                    SignatureDescriptor methodSignature = methodDescriptor.Signature;

                    if (methodSignature != null &&
                        ParametersMatch(methodSignature, argListTypes))
                    {
                        // method descriptor for only current signature
                        MethodTypeDescriptor temp = new MethodTypeDescriptor();
                        temp.ReturnType = methodDescriptor.ReturnType;
                        temp.Signature.ParameterTypes = argListTypes;
                        temp.Signature.Next           = null;
                        descriptor = temp;
                    }
                    else if (methodSignature == null)
                    {
                        descriptor = new ErrorDescriptor
                                         ("No signature found for method: " +
                                         qualifiedName.GetStringName());
                    }
                    else
                    {
                        descriptor = new ErrorDescriptor
                                         ("No method signature found matching: (" +
                                         String.Join(", ", argListTypes) + ")");
                    }
                }
                else
                {
                    descriptor = new ErrorDescriptor("Method not declared: " +
                                                     qualifiedName.GetStringName());
                }
                node.TypeDescriptor = descriptor;
                Attributes methodCallAttr = new Attr(descriptor);
                methodCallAttr.Kind = Kind.MethodType;
                node.AttributesRef  = methodCallAttr;
            }
        }
コード例 #4
0
        private void setBaseline()
        {
            ScopeTable currScopeTable = symbolTable.Pop();
            // Until given additional info, Java objects are placeholders
            Attr javaAttr = new Attr(new JavaObjectDescriptor());

            javaAttr.Kind = Kind.TypeAttributes;
            currScopeTable.Add("java", javaAttr);
            currScopeTable.Add("io", javaAttr);
            currScopeTable.Add("lang", javaAttr);
            currScopeTable.Add("System", javaAttr);
            currScopeTable.Add("out", javaAttr);
            currScopeTable.Add("print", javaAttr);
            currScopeTable.Add("outint", javaAttr);
            currScopeTable.Add("PrintStream", javaAttr);
            currScopeTable.Add("TestClasses", javaAttr);

            // primitive types
            PrimitiveAttributes primVoidAttrs =
                new PrimitiveAttributes(new PrimitiveTypeVoidDescriptor());

            currScopeTable.Add("VOID", primVoidAttrs);
            PrimitiveAttributes primIntAttrs =
                new PrimitiveAttributes(new PrimitiveTypeIntDescriptor());

            currScopeTable.Add("INT", primIntAttrs);
            PrimitiveAttributes primBooleanAttrs =
                new PrimitiveAttributes(new PrimitiveTypeBooleanDescriptor());

            currScopeTable.Add("BOOLEAN", primBooleanAttrs);

            // special names
            SpecialNameAttributes spNameThis = new SpecialNameAttributes
                                                   (SpecialNameEnums.THIS);

            currScopeTable.Add(spNameThis.Name, spNameThis);
            SpecialNameAttributes spNameNull = new SpecialNameAttributes(
                SpecialNameEnums.NULL);

            currScopeTable.Add(spNameNull.Name, spNameNull);

            // Write & WriteLine
            PrimitiveTypeVoidDescriptor returnType = new PrimitiveTypeVoidDescriptor();
            // no parameters
            SignatureDescriptor sigDescNone = new SignatureDescriptor();
            // integer parameter
            SignatureDescriptor sigDescInt = new SignatureDescriptor();

            sigDescInt.AddParameter(new PrimitiveTypeIntDescriptor());
            // boolean parameter
            SignatureDescriptor sigDescBoolean = new SignatureDescriptor();

            sigDescBoolean.AddParameter(new PrimitiveTypeBooleanDescriptor());
            // literal parameter (string)
            SignatureDescriptor sigDescLiteral = new SignatureDescriptor();

            sigDescLiteral.AddParameter(new LiteralTypeDescriptor());
            // chain signature type descriptors together (sigDescNode = first)
            sigDescNone.Next    = sigDescInt;
            sigDescInt.Next     = sigDescBoolean;
            sigDescBoolean.Next = sigDescLiteral;
            MethodTypeDescriptor methodTypeDescriptor = new MethodTypeDescriptor();

            methodTypeDescriptor.ReturnType = returnType;
            methodTypeDescriptor.Signature  = sigDescNone;
            Attr methodAttr = new Attr(methodTypeDescriptor);

            currScopeTable.Add("Write", methodAttr);
            currScopeTable.Add("WriteLine", methodAttr);

            // TODO: add additional baseline information

            symbolTable.Push(currScopeTable);
        }
コード例 #5
0
        private void VisitNode(MethodDeclaration node)
        {
            AbstractNode modifiers        = node.Child;
            AbstractNode typeSpecifier    = modifiers.Sib;
            AbstractNode methodDeclarator = typeSpecifier.Sib;
            AbstractNode methodBody       = methodDeclarator.Sib;

            AbstractNode retType = typeSpecifier.Child;

            if (retType is PrimitiveTypeVoid)
            {
                ((PrimitiveTypeVoid)retType).Accept(TypeVisitor);
            }
            else if (retType is PrimitiveTypeBoolean)
            {
                ((PrimitiveTypeBoolean)retType).Accept(TypeVisitor);
            }
            else if (retType is PrimitiveTypeInt)
            {
                ((PrimitiveTypeInt)retType).Accept(TypeVisitor);
            }
            else if (retType is QualifiedName)
            {
                ((QualifiedName)retType).Accept(TypeVisitor);
            }
            else
            {
                string msg = "Return type of a method must be a " +
                             "PrimitiveType or QualifiedName (found: " +
                             GetSimpleName(retType.TypeDescriptor);
                retType.TypeDescriptor = new ErrorDescriptor(msg);
            }

            AbstractNode methodDeclaratorName = methodDeclarator.Child;
            AbstractNode parameterList        = methodDeclaratorName.Sib; // may be null

            MethodTypeDescriptor methDesc = new MethodTypeDescriptor();

            methDesc.ReturnType  = retType.TypeDescriptor;
            methDesc.Modifiers   = ((Modifiers)modifiers).ModifierTokens;
            methDesc.Locals      = new ScopeTable();
            methDesc.IsDefinedIn = CurrentClass;

            Attributes attr = new Attr(methDesc);

            attr.Kind = Kind.MethodType;

            string name = ((Identifier)methodDeclaratorName).ID;

            Table.enter(name, attr);
            node.TypeDescriptor = attr.TypeDescriptor;
            node.AttributesRef  = attr;

            Table.openScope(methDesc.Locals);
            MethodTypeDescriptor oldCurrentMethod = CurrentMethod;

            CurrentMethod = methDesc;

            if (parameterList != null)
            {
                parameterList.Accept(this);
                methDesc.Signature.ParameterTypes =
                    ((ParameterListTypeDescriptor)
                     parameterList.TypeDescriptor).ParamTypeDescriptors;
                attr.TypeDescriptor = methDesc;
                Table.updateValue(name, attr);
                node.TypeDescriptor = methDesc;
                node.AttributesRef  = Table.lookup(name);
            }
            methodBody.Accept(this);
            CurrentMethod = oldCurrentMethod;
            Table.closeScope();
        }