using System; using System.Collections.Generic; using System.IO; using System.Linq; using Compiler.Lexer; using Compiler.Parser; using Compiler.Parser.Nodes; namespace Compiler { public static class Compiler { public static void Main() { string[] validFiles = Directory.GetFiles("../../../../tests/week_1/valid"); string[] invalidFiles = Directory.GetFiles("../../../../tests/week_1/invalid"); foreach (string filepath in validFiles) { List tokens = TestLexer(filepath, 0); TestParser(tokens, filepath, 1); } foreach (string filepath in invalidFiles) { List tokens = TestLexer(filepath, 0); TestParser(tokens, filepath, 1); } } static void PrettyPrint(Node root, string indent) { Console.WriteLine(indent + root.NodeType); foreach (Node child in root.Children) { PrettyPrint(child, indent + " "); } } static List TestLexer(string path, int debugLevel) { //List> tokenLists = new List>(); //string[] files = Directory.GetFiles(path); Lexer.Lexer lexer = new Lexer.Lexer(); StreamReader file = new StreamReader(path); string contents = file.ReadToEnd(); List tokens = lexer.Lex(contents); Console.WriteLine("Lexed \"" + path.Split("/").Last() + "\""); if (debugLevel > 0) { Console.WriteLine("-----------" + path + "-----------"); foreach (Token token in tokens) { Console.WriteLine(token.ToString()); } Console.WriteLine("--------------------------------------"); } return tokens; } static void TestParser(List tokenList, string path, int debugLevel) { Parser.Parser p = new Parser.Parser(tokenList); try { Node programNode = p.Parse(NodeType.ProgramNode); if (debugLevel > 0) { PrettyPrint(programNode, ""); } Console.WriteLine("Parsed \"" + path.Split("/").Last() + "\""); } catch (Exception e) { Console.WriteLine("Error in file \"" + path.Split("/").Last() + "\""); Console.WriteLine(e.Message); } } } }