public void TestAddTwoVariablesAndPrint() { string[] text = new string[4]; text[0] = "let a = 5"; text[1] = "let b = 10"; text[2] = "let c = a + b"; text[3] = "print(c)"; Tuple<List<Token>, List<ILineContext>> lexicalOutput = (new LexicalAnalyzer()).GetTokens(text); List<Token> tokens = lexicalOutput.Item1; List<ILineContext> context = lexicalOutput.Item2; Base ast = (new SyntaxAnalyzer()).CheckSyntax(tokens, context, Global.InstructionSets.X86_64); SemanticAnalyzer semanticAnalyzer = new SemanticAnalyzer(); List<Table> symbolTables = semanticAnalyzer.CheckSemantics(ast); List<Module> modules = (new IntermediateCodeGenerator()).GenerateCode("source", "output", ast, symbolTables); List<Module> test = new List<Module>(); test.Add(new Module(new List<Instruction>(), new Dictionary<string, string>())); test[0].InterCode.Add(new SectionCode()); test[0].InterCode.Add(new MakeGlobal("main")); test[0].InterCode.Add(new Label("main")); test[0].InterCode.Add(new Push(rbp)); test[0].InterCode.Add(new Move(rsp, rbp)); test[0].InterCode.Add(new Sub(new ByteConstant(4), rsp)); test[0].InterCode.Add(new Sub(new ByteConstant(3), rsp)); test[0].InterCode.Add(new Call("__main")); test[0].InterCode.Add(new Move(new IntegerConstant(5), new RegisterOffset(Global.Registers.STACKBASEPOINTER, -1))); test[0].InterCode.Add(new Move(new IntegerConstant(10), new RegisterOffset(Global.Registers.STACKBASEPOINTER, -2))); test[0].InterCode.Add(new Move(new RegisterOffset(Global.Registers.STACKBASEPOINTER, -2), rdx)); test[0].InterCode.Add(new Move(new RegisterOffset(Global.Registers.STACKBASEPOINTER, -1), rax)); test[0].InterCode.Add(new Add(rdx, rax)); test[0].InterCode.Add(new Move(rax, new RegisterOffset(Global.Registers.STACKBASEPOINTER, -3))); test[0].InterCode.Add(new Move(new RegisterOffset(Global.Registers.STACKBASEPOINTER, -3), new ParamRegister(1))); test[0].InterCode.Add(new Lea(new RegisterOffset(Global.Registers.INSTRUCTIONPOINTER, ".LC0"), new ParamRegister(0))); test[0].InterCode.Add(new Call("printf")); test[0].InterCode.Add(new Nope()); test[0].InterCode.Add(new Move(rbp, rsp)); test[0].InterCode.Add(new Pop(rbp)); test[0].InterCode.Add(new Ret()); test[0].StringTable.Add("%d", ".LC0"); Assert.IsTrue(ModuleListComparer.Compare(modules, test)); }
public void TestHelloWorld() { string[] text = new string[] { "print(\"Hello World!\")" }; Tuple<List<Token>, List<ILineContext>> lexicalOutput = (new LexicalAnalyzer()).GetTokens(text); List<Token> tokens = lexicalOutput.Item1; List<ILineContext> context = lexicalOutput.Item2; Base ast = (new SyntaxAnalyzer()).CheckSyntax(tokens, context, Global.InstructionSets.X86_64); SemanticAnalyzer semanticAnalyzer = new SemanticAnalyzer(); List<Table> symbolTables = semanticAnalyzer.CheckSemantics(ast); List<Module> modules = (new IntermediateCodeGenerator()).GenerateCode("source", "output", ast, symbolTables); List<Module> test = new List<Module>(); test.Add(new Module(new List<Instruction>(), new Dictionary<string, string>())); test[0].InterCode.Add(new SectionCode()); test[0].InterCode.Add(new MakeGlobal("main")); test[0].InterCode.Add(new Label("main")); test[0].InterCode.Add(new Push(rbp)); test[0].InterCode.Add(new Move(rsp, rbp)); test[0].InterCode.Add(new Sub(new ByteConstant(4), rsp)); test[0].InterCode.Add(new Call("__main")); test[0].InterCode.Add(new Lea(new RegisterOffset(Global.Registers.INSTRUCTIONPOINTER, ".LC0"), new ParamRegister(0))); test[0].InterCode.Add(new Call("printf")); test[0].InterCode.Add(new Nope()); test[0].InterCode.Add(new Move(rbp, rsp)); test[0].InterCode.Add(new Pop(rbp)); test[0].InterCode.Add(new Ret()); test[0].StringTable.Add("Hello World!", ".LC0"); Assert.IsTrue(ModuleListComparer.Compare(modules, test)); }
public static void Main(string[] args) { string lookingFor = ""; string source = ""; string output = ""; for (int i = 0; i < args.Length; i++) { if (lookingFor == "") { if (args[i][0] == '-') { if (args[i] == "-o") lookingFor = "output"; else if (args[i] == "-IS") lookingFor = "instructionSet"; else error(new InvalidArgumentException("An invalid argument was supplied: " + args[i])); } else { if (source == "") source = args[i]; else error(new MultipleSourceFilesException("The source can be supplied only once: " + args[i])); } } else { if (lookingFor == "output") output = args[i]; else if (lookingFor == "instructionSet") { switch (args[i].ToLower()) { case "x86": architecture = Global.InstructionSets.X86; break; case "x86_64": architecture = Global.InstructionSets.X86_64; break; default: error(new UnknownInstructionSetException("an unknown Instruction Set was supplied: " + args[i])); break; } } } } Console.WriteLine("Swift Compiler by Joost Verbraeken"); string[] text = System.IO.File.ReadAllLines(source); Tuple<List<Token>, List<ILineContext>> lexicalOutput = (new LexicalAnalyzer()).GetTokens(text); List<Token> tokens = lexicalOutput.Item1; List<ILineContext> context = lexicalOutput.Item2; Base ast = (new SyntaxAnalyzer()).CheckSyntax(tokens, context, architecture); SemanticAnalyzer semanticAnalyzer = new SemanticAnalyzer(); List<Table> symbolTables = semanticAnalyzer.CheckSemantics(ast); List<Module> modules = (new IntermediateCodeGenerator()).GenerateCode(source, output, ast, symbolTables); modules = CodeOptimizer.OptimizeCode(modules); string result = CodeGenerator.MakeAssembly(source, output, modules, architecture); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(result); Console.WriteLine(""); Console.ForegroundColor = ConsoleColor.Gray; Console.ReadLine(); }