Code generation and output file gen
This commit is contained in:
parent
3ea0178678
commit
26d10cfef0
8 changed files with 197 additions and 69 deletions
|
|
@ -1,30 +1,75 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using Compiler.Lexer;
|
||||
using Compiler.Parser;
|
||||
using Compiler.Parser.Nodes;
|
||||
|
||||
namespace Compiler
|
||||
{
|
||||
public static class Compiler
|
||||
{
|
||||
public static void Main()
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
string[] validFiles = Directory.GetFiles("../../../../tests/week_1/valid");
|
||||
string[] invalidFiles = Directory.GetFiles("../../../../tests/week_1/invalid");
|
||||
|
||||
foreach (string filepath in validFiles)
|
||||
if (args.Length != 1)
|
||||
{
|
||||
List<Token> tokens = TestLexer(filepath, 0);
|
||||
TestParser(tokens, filepath, 1);
|
||||
Console.WriteLine("Usage: Compiler <input path>");
|
||||
}
|
||||
|
||||
foreach (string filepath in invalidFiles)
|
||||
else
|
||||
{
|
||||
List<Token> tokens = TestLexer(filepath, 0);
|
||||
TestParser(tokens, filepath, 1);
|
||||
string outputPath = args[0].Substring(0, args[0].LastIndexOf("/"));
|
||||
|
||||
Compile(args[0], $"{outputPath}/assembly.s");
|
||||
Console.WriteLine($"Compiled to {outputPath}/assembly.s");
|
||||
|
||||
ProcessStartInfo startInfo = new ProcessStartInfo()
|
||||
{FileName = "gcc", Arguments = $"{outputPath}/assembly.s -o {outputPath}/program"};
|
||||
Process proc = new Process() {StartInfo = startInfo,};
|
||||
proc.Start();
|
||||
|
||||
while (!proc.HasExited)
|
||||
{
|
||||
Thread.Sleep(1);
|
||||
}
|
||||
Console.WriteLine($"Assembled to {outputPath}/program");
|
||||
File.Delete($"{outputPath}/assembly.s");
|
||||
Console.WriteLine("Deleted assembly.s file. Done!");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void Compile(string inputPath, string outputPath)
|
||||
{
|
||||
//Lexing
|
||||
Lexer.Lexer lexer = new Lexer.Lexer();
|
||||
|
||||
|
||||
StreamReader file = new StreamReader(inputPath);
|
||||
string contents = file.ReadToEnd();
|
||||
|
||||
List<Token> tokens = lexer.Lex(contents);
|
||||
Console.WriteLine($"Lexed {inputPath.Split("/").Last()}.");
|
||||
|
||||
//Parsing
|
||||
Parser.Parser p = new Parser.Parser(tokens);
|
||||
|
||||
try
|
||||
{
|
||||
Node programNode = p.Parse(NodeType.ProgramNode);
|
||||
Console.WriteLine($"Parsed \"{inputPath.Split("/").Last()}\"");
|
||||
|
||||
//Generating
|
||||
Generator.Generator generator = new Generator.Generator();
|
||||
string program = generator.Generate(programNode);
|
||||
|
||||
File.WriteAllText(outputPath, program);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Error in file \"{inputPath.Split("/").Last()}\"");
|
||||
Console.WriteLine(e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -37,6 +82,20 @@ namespace Compiler
|
|||
}
|
||||
}
|
||||
|
||||
static void TestGenerator(Node root, string destinationPath, int debugLevel)
|
||||
{
|
||||
if (root != null)
|
||||
{
|
||||
Generator.Generator gen = new Generator.Generator();
|
||||
string asm = gen.Generate(root);
|
||||
|
||||
if (debugLevel > 0)
|
||||
{
|
||||
Console.Write(asm);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static List<Token> TestLexer(string path, int debugLevel)
|
||||
{
|
||||
Lexer.Lexer lexer = new Lexer.Lexer();
|
||||
|
|
@ -50,20 +109,17 @@ namespace Compiler
|
|||
|
||||
if (debugLevel > 0)
|
||||
{
|
||||
Console.WriteLine("-----------" + path + "-----------");
|
||||
foreach (Token token in tokens)
|
||||
{
|
||||
Console.WriteLine(token.ToString());
|
||||
}
|
||||
|
||||
Console.WriteLine("--------------------------------------");
|
||||
}
|
||||
|
||||
|
||||
return tokens;
|
||||
}
|
||||
|
||||
static void TestParser(List<Token> tokenList, string path, int debugLevel)
|
||||
static Node TestParser(List<Token> tokenList, string path, int debugLevel)
|
||||
{
|
||||
Parser.Parser p = new Parser.Parser(tokenList);
|
||||
|
||||
|
|
@ -75,12 +131,16 @@ namespace Compiler
|
|||
{
|
||||
PrettyPrint(programNode, "");
|
||||
}
|
||||
|
||||
return programNode;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine("Error in file \"" + path.Split("/").Last() + "\"");
|
||||
Console.WriteLine(e.Message);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue