Ejemplo n.º 1
0
        public void VisitNode(ArgList node)
        {
            // Arg list for which method call?
            String owningMethod = null;
            // Find sibling with the method call info
            AbstractNode tempNode;

            tempNode = node.First;

            while (tempNode != null)
            {
                // Find qualified name sibling and leave
                if (tempNode.GetType() == typeof(QualifiedName))
                {
                    owningMethod = Utilities.FilterOutKeywords(tempNode.Name);
                    break;
                }
                tempNode = tempNode.Sib;
            }


            Utilities.TypeVisitAllChildren(node);

            Attributes            a   = new Attributes("ArgList");
            ArgListTypeDescriptor ATD = new ArgListTypeDescriptor();

            a.typeInfo   = ATD;
            node.attrRef = a;

            // Error type if problem with assignment
            if (IsValidArgList(node) == false)
            {
                Error(String.Format("Warning! Invalid Argument List when calling method {0}", owningMethod));
                node.attrRef.typeInfo = new ErrorTypeDescriptor();
            }

            // Also add the parameters to the typeInfo for printing later
            List <AbstractNode> listOfParams = new List <AbstractNode>();

            ATD.Params       = listOfParams;
            ATD.OwningMethod = owningMethod;
            //Add every child to the parameters
            tempNode = node.Child;

            while (tempNode != null)
            {
                listOfParams.Add(tempNode);
                // Find qualified name sibling and leave
                if (tempNode.GetType() == typeof(QualifiedName))
                {
                    owningMethod = Utilities.FilterOutKeywords(tempNode.Name);
                    break;
                }
                tempNode = tempNode.Sib;
            }
        }
Ejemplo n.º 2
0
        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
        }