Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.Title = "Canonical Equations Interactive Mode";

                while (true)
                {
                    Console.WriteLine("Enter An equation of the form: " +
                                      "P1 + P2 + ... = ... + PN");
                    Console.WriteLine("Press Ctrl + C to close the program");
                    string equation = Console.ReadLine();

                    if (String.IsNullOrEmpty(equation))
                    {
                        continue;
                    }

                    Canonizer canonizer = new Canonizer();

                    try
                    {
                        string result = canonizer.ParseEquation(equation);
                        Console.WriteLine(result);
                        Console.WriteLine();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Entry was invalid. try again");
                        Console.WriteLine();
                    }
                }
            }
            else
            {
                string fileName = args[0];


                StreamReader input  = new StreamReader(fileName);
                StreamWriter output = new StreamWriter(fileName + ".out");

                Console.WriteLine("Reading input file");

                string result;
                string line;
                while ((line = input.ReadLine()) != null)
                {
                    Canonizer canonizer = new Canonizer();
                    result = canonizer.ParseEquation(line);

                    output.WriteLine(result);
                }

                input.Close();
                output.Flush();
                output.Close();;

                Console.WriteLine("Finished");
            }
        }
Ejemplo n.º 2
0
        public void SimpleParsing()
        {
            IToken[] tokens =
            {
                new Term(1,        "x"),
                new Operator('+'),
                new Term(2,        "y"),
                new Operator('='),
                new Term(3,        "y")
            };

            IToken[] expected =
            {
                new Term(1,             "x"),
                new Operator('+'),
                new Term(2,             "y"),
                new Operator('-'),
                new LeftParenthesis(),
                new Term(3,             "y"),
                new RightParenthesis(),
                new Operator('='),
                new Term(0,             string.Empty)
            };

            var canonizer = new Canonizer();
            var result    = canonizer.Canonize(tokens).ToArray();

            Assert.Equal(expected, result);
        }
        public void VariableOrder()
        {
            string    equation4 = "1 + x - y + xx - yy + xy - x^2 + y^2 = 0";
            string    answer4   = "-x^2 + x + y^2 - y + xx + xy - yy + 1 = 0";
            Canonizer canonizer = new Canonizer();

            Assert.AreEqual(canonizer.ParseEquation(equation4), answer4);
        }
        public void Equation4()
        {
            string    equation4 = "x - (0 - (0 - x)) = 0";
            string    answer4   = "0 = 0";
            Canonizer canonizer = new Canonizer();

            Assert.AreEqual(canonizer.ParseEquation(equation4), answer4);
        }
        public void Equation3()
        {
            string    equation3 = "x - (y^2 - x) = 0";
            string    answer3   = "2x - y^2 = 0";
            Canonizer canonizer = new Canonizer();

            Assert.AreEqual(canonizer.ParseEquation(equation3), answer3);
        }
        public void Equation2()
        {
            string    equation2 = "x = 1";
            string    answer2   = "x - 1 = 0";
            Canonizer canonizer = new Canonizer();

            Assert.AreEqual(canonizer.ParseEquation(equation2), answer2);
        }
        public void Equation1()
        {
            string    equation1 = "x ^ 2 + 3.5xy + y = y ^ 2 - xy + y";
            string    answer1   = "x^2 - y^2 + 4.5xy = 0";
            Canonizer canonizer = new Canonizer();

            Assert.AreEqual(canonizer.ParseEquation(equation1), answer1);
        }
Ejemplo n.º 8
0
        public void MultipleEqualityError()
        {
            IToken[] tokens =
            {
                new Term(1,        "x"),
                new Operator('='),
                new Term(2,        "y"),
                new Operator('='),
                new Term(3,        "y")
            };

            var canonizer         = new Canonizer();
            var argumentException = Assert.Throws <ArgumentException>("tokens", () => canonizer.Canonize(tokens).ToArray());

            Assert.Contains("Single equality operator is allowed in equation", argumentException.Message);
        }
Ejemplo n.º 9
0
        public void Parse(string input, string[] compilerOptions)
        {
            string fileName = Path.GetFileNameWithoutExtension(compilerOptions[0]);

            ParseCompilerOptions(compilerOptions);

            AntlrInputStream inputStream = new AntlrInputStream(input);

            MiniJavaLexer miniJavaLexer = new MiniJavaLexer(inputStream);

            CommonTokenStream commonTokenStream = new CommonTokenStream(miniJavaLexer);
            MiniJavaParser    miniJavaParser    = new MiniJavaParser(commonTokenStream);

            miniJavaParser.AddErrorListener(ErrorListener.INSTANCE);

            var cst = miniJavaParser.prg();

            if (ErrorListener.INSTANCE.errorCounter > 0)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Sorry this is all my fault :(");
            }
            else
            {
                // Generate AST //

                var ast = cst.ToAst();

                // Type Checking //

                TypeChecker typeChecker = new TypeChecker();
                typeChecker.InitTypeChecking(ast);
                typeChecker.StartTypeChecking(ast);

                // Convert to intermediate //

                IntermediateAstBuilder intermediate = new IntermediateAstBuilder();
                var interTree     = intermediate.BuildIntermediateAst(ast);
                var canonizedTree = new Canonizer().CanonPrg((TreePrg)interTree);

                // assembly //
                I386CodeGenerator codeGenerator = new I386CodeGenerator();
                var i386Prg = (I386Prg)codeGenerator.CodeGen(canonizedTree);

                if (WithoutAlloc)
                {
                    File.WriteAllText(fileName + "_noallocs.s", i386Prg.RenderAssembly());
                }

                // Liveness analysis //
                RegisterAllocator registerAllocator = new RegisterAllocator();
                registerAllocator.AllocateRegisters(i386Prg, codeGenerator.GetGeneralPurposeRegisters());

                Afterburner afterburner = new Afterburner();
                afterburner.RemoveRedundancies(i386Prg);



                if (Intermediate)
                {
                    File.WriteAllText(fileName + ".tree", canonizedTree.ToString());
                }
                File.WriteAllText(fileName + ".s", i386Prg.RenderAssembly());

                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("You did great!");
                Console.ForegroundColor = ConsoleColor.White;
            }
        }