示例#1
0
        public void VisitNode(MethodCall node)
        {
            Attributes a = new Attributes("MethodCall");
            MethodCallTypeDescriptor MTD = new MethodCallTypeDescriptor();
            ErrorTypeDescriptor      ETD = new ErrorTypeDescriptor();

            node.attrRef = a;

            /* --------------- Type Check Qualified Name and extract data -----------------*/
            // Get Name
            String name = GetMethCallName(node);

            /* ----------------------- Semantics Visit ArgList (if exists)--------------------------*/
            // Go to ArgList
            List <ArgList> argListChildren = Utilities.GetChildren(node, typeof(ArgList)).Cast <ArgList>().ToList();

            // If Args exist...
            if (argListChildren.Count > 0)
            {
                // Type check the argument Node
                argListChildren[0].Accept(this);
                // Record it in our type ref
                MTD.argsRoot = argListChildren[0];
            }


            /* ------------ Configure Attributes ----------------*/
            // Put the descriptor in the attributes structure
            a.typeInfo = MTD;

            /* --------- Configure Type Descriptor --------------*/
            MTD.name = name;
            Attributes attr = currentSymbolTable.lookup(name);

            // If method exists in symbol table
            if (attr != null)
            {
                MTD.returnType   = attr.typeInfo.returnType;
                MTD.nameSpaceVar = attr.typeInfo.nameSpaceVar;
                a.typeInfo       = MTD;
            }
            // Method not found in symbol table
            else
            {
                a.typeInfo = ETD;
            }

            // Decorate AST with attrRef
            node.attrRef = a;
        }
        protected void VisitNode(MethodCall node)
        {
            // deal with arguments first
            // Arguments are in the arglist node's type descriptor.
            // If arguments were provided
            // We'll need one of these to understand what we're looking at
            MethodCallTypeDescriptor mtd = null;

            ArgListTypeDescriptor atd = null;
            ArgList proposedArgs      = null;

            // "If the arg list for this has errors" is what we're trying to get at with this block
            // If we aren't error type descriptor try to get the args root
            if (Utilities.attrToType(node.attrRef) != "ErrorTypeDescriptor")
            {
                proposedArgs = node.attrRef.typeInfo.argsRoot;

                // If arguments are invalid
                if (proposedArgs != null && proposedArgs.attrRef.typeInfo.GetType() == typeof(ErrorTypeDescriptor))
                {
                    Console.WriteLine("Error in code generator. ErrorTypeDescriptor encountered.");
                }
                // If arguments are valid


                mtd = node.attrRef.typeInfo;
                string paramResult = "";
                if (proposedArgs != null)
                {
                    atd = mtd.argsRoot.attrRef.typeInfo;
                }
                // Keep track of how many arguments we're loading onto the stack prior to a function call
                if (atd != null)
                {
                    foreach (var element in atd.Params)
                    {
                        if (Utilities.attrToType(element.attrRef).Contains("String"))
                        {
                            paramResult += "string";
                            // Overhead of pushing string to the stack
                            InsertIntoFile(String.Format("ldstr\t\t \"{0}\"\n", element.Name));
                        }
                        else if (Utilities.attrToType(element.attrRef).Contains("Integer"))
                        {
                            paramResult += "int32";

                            // If Identifier, use the number directly
                            if (element.NodeType.Name == "Identifier")
                            {
                                InsertIntoFile(String.Format("ldc.i4\t\t {0}\n", element.Name));
                            }

                            // If Qualified Name use variable name
                            else if (element.NodeType.Name == "QualifiedName")
                            {
                                int varNumber = 0;
                                // Either this is coming from function args (see here).
                                Dictionary <string, int> localArgs = new Dictionary <string, int>();
                                localArgNumber.TryGetValue(methodName, out localArgs);
                                if (localArgs.ContainsKey(element.Name))
                                {
                                    varNumber = GetArgPosit(element.Name);
                                    InsertIntoFile(String.Format("ldarg {0}\t\t //{1}\n", varNumber, element.Name));
                                }
                                else
                                {
                                    varNumber = GetStackPosit(element.Name);
                                    InsertIntoFile(String.Format("ldloc {0}\t\t //{1}\n", varNumber, element.Name));
                                }

                                // Or this is coming from function local vars (see here)
                            }

                            // If Expr, generate instructions to evaluate expr
                            else if (element.NodeType.Name == "Expr")
                            {
                                // Visit all expr nodes and have them generate the code. The other way to do this
                                // would be as defined in this comment block which I was told not to do in class
                                // (But works!)

                                /*
                                 * Expr e = element as Expr;
                                 * int expressionEval = e.eval();
                                 * InsertIntoFile(String.Format("ldc.i4\t\t {0}\n", expressionEval));
                                 */
                                element.Accept(this);
                            }
                        }

                        else
                        {
                            paramResult += "DEFAULT uh oh";
                        }
                    }
                }

                // call method
                String nameSpaceString = mtd.nameSpaceVar;
                InsertIntoFile(String.Format("call {0} {1} :: {2}({3})\n", mtd.returnType.ToLower(),
                                             mtd.nameSpaceVar, mtd.name, paramResult));
            }


#if false // Get a type descriptor for this method dcl
            MethodDclTypeDescp m  = node.attrRef.typeInfo as MethodDclTypeDescp;
            StringBuilder      sb = new StringBuilder();
            Byte[]             info;


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

            // Build parameter list
            string theFunctArgs   = "";
            int    numberOfParams = 0;
            if (m.paramList != null)
            {
                foreach (string name in m.paramList.Names)
                {
                    string entry = name + " " + m.paramList.Types;
                    if (numberOfParams++ > 0)
                    {
                        entry += ",";
                    }
                    theFunctArgs += entry + "\n";
                }
            }
            sb.AppendLine(String.Format(".method {0}{1} \n {2} ({3}) cil managed", modList, m.returnType, 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\n"); // for now

            // If field decls, do them here too
            // Visit children
            Utilities.CodeGenVisitAllChildren(node, this);
#endif
        }