code generations for unary operators

This commit is contained in:
Clemens-Dautermann 2020-08-21 00:18:21 +02:00
parent 07079366b8
commit e6b20b5aa9
7 changed files with 202 additions and 153 deletions

View file

@ -25,30 +25,7 @@ namespace Compiler
}
else
{
DevMode();
}
}
}
static void DevMode()
{
for (int i = 1; i <= 2; i++)
{
Console.WriteLine($"---------------------valid, stage {i}-------------------------------");
foreach (string file in Directory.GetFiles($"/home/clemens/repositorys/lcc/stage_{i}/valid"))
{
Console.WriteLine("-------------");
List<Token> tokens = TestLexer(file, 0);
TestParser(tokens, file, 1);
}
Console.WriteLine($"---------------------invalid, stage {i}-------------------------------");
foreach (string file in Directory.GetFiles($"/home/clemens/repositorys/lcc/stage_{i}/invalid"))
{
Console.WriteLine("-------------");
List<Token> tokens = TestLexer(file, 0);
TestParser(tokens, file, 1);
DevFunctions.DevMode();
}
}
}
@ -57,7 +34,7 @@ namespace Compiler
{
bool debug = false;
string inputFileName = args[0].Split("/").Last();
string outputPath = args[0].Substring(0, args[0].LastIndexOf("/"));
string outputPath = args[0].Substring(0, args[0].LastIndexOf("/", StringComparison.Ordinal));
if (args.Length == 2)
{
@ -134,91 +111,5 @@ namespace Compiler
Console.WriteLine(e.Message);
}
}
static void PrettyPrint(Node root, string indent)
{
switch (root.NodeType)
{
case NodeType.FunctionNode:
Console.WriteLine(indent + root.NodeType + ":" + ((FunctionNode) root).Name);
break;
case NodeType.ConstantNode:
Console.WriteLine(indent + root.NodeType + ":" + ((ConstantNode) root).value);
break;
case NodeType.UnaryOperatorNode:
Console.WriteLine(indent + root.NodeType + ":" + ((UnaryOperatorNode) root).OperatorType);
break;
default:
Console.WriteLine(indent + root.NodeType);
break;
}
foreach (Node child in root.Children)
{
PrettyPrint(child, indent + " ");
}
}
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();
StreamReader file = new StreamReader(path);
string contents = file.ReadToEnd();
List<Token> tokens = lexer.Lex(contents);
Console.WriteLine("Lexed \"" + path.Split("/").Last() + "\"");
if (debugLevel > 0)
{
foreach (Token token in tokens)
{
Console.WriteLine(token.ToString());
}
}
return tokens;
}
static Node TestParser(List<Token> tokenList, string path, int debugLevel)
{
Parser.Parser p = new Parser.Parser(tokenList);
try
{
Node programNode = p.Parse(NodeType.ProgramNode);
Console.WriteLine("Parsed \"" + path.Split("/").Last() + "\"");
if (debugLevel > 0)
{
PrettyPrint(programNode, "");
}
return programNode;
}
catch (Exception e)
{
Console.WriteLine("Error in file \"" + path.Split("/").Last() + "\"");
Console.WriteLine(e.Message);
}
return null;
}
}
}