Example #1
0
        private string GetMethodDclRetType(MethodDcl node)
        {
            string methodRT;
            // ReturnType is in a PrimitiveType
            // Go to MethodDcla
            List <PrimitiveType> matchingChildren = Utilities.GetChildren(node, typeof(PrimitiveType)).Cast <PrimitiveType>().ToList();

            methodRT = matchingChildren[0].Name;
            return(methodRT);
        }
Example #2
0
        private string GetMethodDclName(MethodDcl node)
        {
            string methodName;
            // Name is in a DclName under a MethodDcla child.
            // Go to MethodDcla
            List <MethodDcla> methodDclaChildren = Utilities.GetChildren(node, typeof(MethodDcla)).Cast <MethodDcla>().ToList();
            // Go to DclName
            List <DclName> dclNameChildren = Utilities.GetChildren(methodDclaChildren[0], typeof(DclName)).Cast <DclName>().ToList();

            // Extract name (ugly but the name is currently "Method -> xxxx" and we want xxxx)
            methodName = dclNameChildren[0].Name.Split(' ')[2];
            return(methodName);
        }
Example #3
0
        /* MethodDcl */
        private void GetMethodDclParams(MethodDcl node, ref List <string> types, ref List <string> names)
        {
            List <ParamList> paramListChildren;
            List <Param>     paramChildren;
            // Go to MethodDcla
            List <MethodDcla> methodDclaChildren = Utilities.GetChildren(node, typeof(MethodDcla)).Cast <MethodDcla>().ToList();

            // Go to ParamList if it exists
            if (methodDclaChildren.Count > 0)
            {
                paramListChildren = Utilities.GetChildren(methodDclaChildren[0], typeof(ParamList)).Cast <ParamList>().ToList();
                // Create a list of all of the params
                if (paramListChildren.Count > 0)
                {
                    paramChildren = Utilities.GetChildren(paramListChildren[0], typeof(Param)).Cast <Param>().ToList();

                    foreach (Param p in paramChildren)
                    {
                        // Go to Primitive type and extract the type
                        // Create a list of primitivetype children (should only be one)
                        List <PrimitiveType> typeChildren = Utilities.GetChildren(p, typeof(PrimitiveType)).Cast <PrimitiveType>().ToList();
                        // Go ahead and type visit the node we found
                        TypeVisitor t = new TypeVisitor();
                        typeChildren[0].Accept(t);
                        Attributes tempAttr = typeChildren[0].attrRef;
                        types.Add(Utilities.TypeDescToString(tempAttr));

                        // Go to DclName and extract the name
                        // Create a list of DclName children (should only be one)
                        List <DclName> nameChildren = Utilities.GetChildren(p, typeof(DclName)).Cast <DclName>().ToList();
                        // Extract the name (ugly but the name is currently "Declarator -> xxxx" and we want xxxx)
                        names.Add(nameChildren[0].Name.Split(' ')[2]);
                        // We can also add some attr Refs to it for tree printing
                        nameChildren[0].attrRef = tempAttr;
                    }
                }
            }
        }
        protected void VisitNode(MethodDcl node)
        {
            // Get a type descriptor for this method dcl
            MethodDclTypeDescp m  = node.attrRef.typeInfo as MethodDclTypeDescp;
            StringBuilder      sb = new StringBuilder();

            Byte[] info;

            methodName = m.Name;

            // Build modifier list
            string modList = "";

            foreach (string modifier in m.Modifiers)
            {
                modList += modifier + " ";
            }
            modList = modList.ToLower();

            // Build parameter list
            string theFunctArgs   = "";
            int    numberOfParams = 0;
            // Correlates variable names with stack position
            Dictionary <string, int> localArgs = new Dictionary <string, int>();

            if (m.paramList != null)
            {
                int localArgIdx = 0;
                foreach (string name in m.paramList.Names)
                {
                    localArgs.Add(name, localArgIdx++);
                    string entry = m.paramList.Types[numberOfParams] + " " + name;
                    if (numberOfParams++ > 0)
                    {
                        entry += ",";
                    }
                    theFunctArgs += entry + "\n";
                }
            }
            localArgNumber.Add(methodName, localArgs);

            sb.AppendLine(String.Format(".method {0}{1} \n {2} ({3}) cil managed", modList, m.returnType.ToLower(), m.Name, theFunctArgs));
            info = new UTF8Encoding(true).GetBytes(sb.ToString());
            TCCLParser.currentFileStream.Write(info, 0, info.Length);

            InsertIntoFile("{\n");

            if (m.Name.Contains("main") || m.Name.Contains("Main"))
            {
                InsertIntoFile(".entrypoint\n");
            }
            InsertIntoFile(".maxstack 1000 // Should be using sethi ullman algorithm here!\n"); // for now

            // If field decls, do them here too
            Utilities.VisitSpecificChild(node, typeof(LocalVarDecls), this);

            // Visi LocalVarsDeclsAndStatments
            // See if we have any fields
            Utilities.VisitSpecificChild(node, typeof(LocalVarDeclsAndStmts), this);

            // Visit Method Calls


            // Etc?
            InsertIntoFile("ret\n");

            InsertIntoFile("}\n");
        }
Example #5
0
        public void VisitNode(MethodDcl node)
        {
            /* --------- Gather some info --------------*/
            // Get Name
            String name = GetMethodDclName(node);
            // Get Return TYpe
            String returnType = GetMethodDclRetType(node);
            // Get class name that owns this method
            String definedInClass = currentClassName;

            // Get modifiers
            // Create a list of all modifier children
            List <Modifier> modifierChildren = Utilities.GetChildren(node, typeof(Modifier)).Cast <Modifier>().ToList();
            // Process modifier children
            List <String> listOfModifiers = new List <String>();

            foreach (Modifier m in modifierChildren)
            {
                listOfModifiers.Add(m.Name);
            }

            // Get parameters
            List <string> types = new List <string>();
            List <string> names = new List <string>();

            GetMethodDclParams(node, ref types, ref names);

            // Create a type descriptor for the method
            MethodDclTypeDescp typeRef = new MethodDclTypeDescp();
            // Create a new attributes structure
            Attributes attr = new Attributes("method");

            /* ------------ Configure Attributes ----------------*/
            // Put the descriptor in the attributes structure
            attr.typeInfo = typeRef;

            /* --------- Configure Type Descriptor --------------*/
            // Put a new symbol table into Type Descriptor
            typeRef.MethodSymbolTable = new SymbolTable();
            // Put the name into Type Descriptor
            typeRef.Name = name;
            // Put the retury type into Type Descriptor
            typeRef.returnType = returnType;
            // Put the list of modifiers into Type Descriptor
            typeRef.Modifiers = listOfModifiers;
            // Put the owning class into Type Descriptor
            typeRef.isDefinedIn = definedInClass;
            // Put the list of param types into Type Descriptor
            typeRef.paramList.Types = types;
            // Put the list of param names into Type Descriptor
            typeRef.paramList.Names = names;
            // Namespace
            typeRef.nameSpaceVar = typeRef.isDefinedIn; //was confused between the two until this was the easier solution

            // Throw all of the arguments into the symbol table;
            currentSymbolTable.incrNestLevel();
            for (int i = 0; i < names.Count; i++)
            {
                // If type exists in symbol table
                Attributes aa = currentSymbolTable.lookup(types[i]);
                if (aa != null)
                {
                    currentSymbolTable.enter(names[i], aa);
                }
            }

            /* ------ Ready to record into symbol table ---------*/
            // Record ourselves into the symbol table
            currentSymbolTable.enter(name, attr);
            // Decorate AST with attrRef
            node.attrRef = attr;

            /* --------------- Visit All Fields -----------------*/
            // currentSymbolTable = typeRef.MethodSymbolTable;

            // Create a list of Block children (should only be one)
            List <Block> methodBodyChildren = Utilities.GetChildren(node, typeof(Block)).Cast <Block>().ToList();

            foreach (Block b in methodBodyChildren)
            {
                this.VisitNode(b);
            }
            // Put symbol table back when we're done
            currentSymbolTable.decrNestLevel();
        }