public static void Term(GATNode node)
        {
            int     childnum = node.ChildCount();
            string  mulop    = "";
            GATNode child;

            Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.FullName);

            child = node.getChild(0);
            node.SetProperty("value", child.GetProperty("value"));
            for (int i = 1; i < childnum; i++)
            {
                child = node.getChild(i);
                if (child.GetProperty("value") == "*")
                {
                    mulop = "*";
                }
                else if (child.GetProperty("value") == "/")
                {
                    mulop = "/";
                }
                else//有待解决如何记录结果
                {
                    CodeGenerator.AddCode(mulop, node.GetProperty("value"), child.GetProperty("value"), "T" + CodeGenerator.tempnum);
                    Console.WriteLine(mulop + " " + node.GetProperty("value") + " " + child.GetProperty("value") + " " + "T" + CodeGenerator.tempnum);
                    node.SetProperty("value", "T" + CodeGenerator.tempnum);
                    CodeGenerator.tempnum++;
                }
            }
        }
        public static void Call(GATNode node)
        {
            //TODO:支持参数表,栈帧处理
            Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.FullName);

            var processName = node.getChild(0).GetProperty("value");

            var args   = node.getChild(1);
            var argNum = args.ChildCount();

            if (argNum == 0)
            {
                CodeGenerator.CallFunction(processName, new List <string>());
                node.SetProperty("value", "void");
            }
            else
            {
                var paramList = new List <string>();
                var argList   = args.getChild(0);
                for (int i = 0; i < argList.ChildCount(); i++)
                {
                    paramList.Add(argList.getChild(i).GetProperty("value"));
                }
                var retNode = CodeGenerator.CallFunction(processName, paramList);
                node.SetProperty("value", retNode);
            }
        }
Exemple #3
0
        public static void Expression2(GATNode node)
        {
            //var = expression
            GATNode left, right;

            Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.FullName);

            left  = node.getChild(0);
            right = node.getChild(1);

            node.SetProperty("value", left.GetProperty("value"));

            CodeGenerator.AddCode("=", right.GetProperty("value"), null, left.GetProperty("value"));
            Console.WriteLine("=" + " " + right.GetProperty("value") + " " + "\t" + " " + left.GetProperty("value"));
        }
Exemple #4
0
        public static void IterationStmt(GATNode node)
        {
            Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.FullName);

            var expressionLabel = node.getChild(0).GetProperty("LabelName");
            var endLabel        = node.getChild(5).GetProperty("LabelName");
            var jmpLine         = Int32.Parse(node.getChild(2).GetProperty("CodeLine"));
            var retLine         = Int32.Parse(node.getChild(4).GetProperty("CodeLine"));

            CodeGenerator.SetCode(retLine, "j", "#", "#", "#");
            CodeGenerator.PutLabel(expressionLabel, retLine);

            CodeGenerator.SetCode(jmpLine, "je", node.getChild(1).GetProperty("value"), "0", "#");
            CodeGenerator.PutLabel(endLabel, jmpLine);
        }
Exemple #5
0
        public static void Var(GATNode node)
        {
            Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.FullName);
            GATNode IDnode = node.getChild(0);

            node.SetProperty("value", IDnode.GetProperty("value"));
        }
        public static void SelectionStmt(GATNode node)
        {
            Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.FullName);
            var expression = node.getChild(0);

            switch (node.ChildCount())
            {
            case 4:
            {
                var falseLabel = node.getChild(3).GetProperty("LabelName");
                var jmpLine    = Int32.Parse(node.getChild(1).GetProperty("CodeLine"));
                CodeGenerator.SetCode(jmpLine, "je", expression.GetProperty("value"), "0", "0");
                CodeGenerator.PutLabel(falseLabel, jmpLine);
                break;
            }

            case 7:
            {
                var falseLabel = node.getChild(4).GetProperty("LabelName");
                var jmpLine    = Int32.Parse(node.getChild(1).GetProperty("CodeLine"));
                CodeGenerator.SetCode(jmpLine, "je", expression.GetProperty("value"), "0", "0");
                CodeGenerator.PutLabel(falseLabel, jmpLine);

                var endLabel    = node.getChild(6).GetProperty("LabelName");
                var trueEndLine = Int32.Parse(node.getChild(3).GetProperty("CodeLine"));
                CodeGenerator.SetCode(trueEndLine, "j", "#", "#", "#");
                CodeGenerator.PutLabel(endLabel, trueEndLine);
                break;
            }
            }
        }
        public static void varDeclaration(GATNode node)
        {
            Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.FullName);

            string type, id;

            type = node.getChild(0).GetProperty("value");
            id   = node.getChild(1).GetProperty("value");
            node.SetProperty("value", id);
            node.SetProperty("type", type);
            GATNode process = node.GetParent();

            while (process.name == "")
            {
                process = process.GetParent();
            }
            CodeGenerator.AddSymbol(node.GetProperty("value"), process.name, null);
        }
Exemple #8
0
        public static void Expression1(GATNode node)
        {
            //simple expression
            GATNode child;

            Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.FullName);

            child = node.getChild(0);
            node.SetProperty("value", child.GetProperty("value"));
        }
        public static void program(GATNode node)
        {
            Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.FullName);

            var startLine = Int32.Parse(node.getChild(0).GetProperty("CodeLine"));

            CodeGenerator.SetCode(startLine, "j", "#", "#", "#");
            CodeGenerator.PutLabel("main", startLine);

            //CodeGenerator.AddLabel("global", 0);
        }
        public static void FunDeclaration(GATNode node)
        {
            //支持参数表
            Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.FullName);
            CodeGenerator.AddLabel(node.getChild(1).GetProperty("value"), Int32.Parse(node.getChild(3).GetProperty("CodeLine")));

            var funcName   = node.getChild(1).GetProperty("value");
            var returnType = node.getChild(0).GetProperty("value");
            var paramCount = node.getChild(2).ChildCount() == 0 ? 0 : node.getChild(2).getChild(0).ChildCount();
            var paramName  = new List <string>();

            if (paramCount != 0)
            {
                var paramList = node.getChild(2).getChild(0);
                for (int i = 0; i < paramList.ChildCount(); i++)
                {
                    var param = paramList.getChild(i);
                    paramName.Add(param.getChild(1).GetProperty("value"));
                }
            }
            CodeGenerator.AddFunction(new FunctionStackFrame
            {
                name           = funcName,
                returnType     = returnType,
                parameterCount = paramCount,
                parameterName  = paramName
            });
        }
        public static void ReturnStmt(GATNode node)
        {
            Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.FullName);

            if (node.ChildCount() == 0)
            {
                CodeGenerator.AddCode("ret", "#", "#", "#");
            }
            else
            {
                var tmpNode = "T" + CodeGenerator.tempnum++;
                CodeGenerator.AddCode("+", "EBP", "12", tmpNode);
                CodeGenerator.AddCode("*", tmpNode, "4", tmpNode);
                CodeGenerator.AddCode("+", "EBP", tmpNode, tmpNode);
                CodeGenerator.AddCode("+", tmpNode, "16", tmpNode);
                CodeGenerator.AddCode("[", tmpNode, "#", tmpNode);
                CodeGenerator.AddCode("[", tmpNode, "#", tmpNode);
                CodeGenerator.AddCode("]", node.getChild(0).GetProperty("value"), "#", tmpNode);
                CodeGenerator.AddCode("ret", "#", "#", "#");
            }
        }