/// <summary> /// Runs a parse IR test (parsing the emitted the IR). /// </summary> /// <param name="interpreter"> The interpreter to emit from then read. </param> /// <param name="iterations"> How many times to run. </param> /// <param name="throwOut"> Print out average of results. </param> /// <param name="withComments"> Emit then read with comments? </param> private static void RunReadEmitTest(Interpreter interpreter, int iterations, bool throwOut, bool withComments) { Log.HandleLogs = true; Stopwatch stopwatch = new Stopwatch(); long total = 0; StringBuilder builder = new StringBuilder(); using (IRWriter writer = new IRWriter(builder)) { writer.Emit(interpreter, withComments); for (int i = 0; i < iterations; i++) { stopwatch.Restart(); //.GetInterpreterFromText(builder.ToString(), Parser.ReadMode.IR); stopwatch.Stop(); if (throwOut == false) { total += stopwatch.ElapsedMilliseconds; } } } if (throwOut == false) { Log.HandleLogs = true; Log.Info($"<READ EMIT {(withComments ? "WITH COMMENTS" : "WITHOUT COMMENTS")} TEST> Took average of {(total / iterations)}ms to complete. Did {iterations} times"); Log.HandleLogs = true; } }
/// <summary> /// Runs an emit test (emitting the IR). /// </summary> /// <param name="interpreter"> The interpreter to emit from. </param> /// <param name="iterations"> How many times to run. </param> /// <param name="throwOut"> Print out average of results. </param> /// <param name="withComments"> Emit with comments? </param> private static void RunEmitTest(Interpreter interpreter, int iterations, bool throwOut, bool withComments) { Log.HandleLogs = true; Stopwatch stopwatch = new Stopwatch(); long total = 0; for (int i = 0; i < iterations; i++) { StringBuilder builder = new StringBuilder(); using (IRWriter writer = new IRWriter(builder)) { stopwatch.Restart(); writer.Emit(interpreter, withComments); stopwatch.Stop(); if (throwOut == false) { total += 1000 * stopwatch.ElapsedTicks; } } } if (throwOut == false) { Log.HandleLogs = true; Log.Info($"<EMIT {(withComments ? "WITH COMMENTS" : "WITHOUT COMMENTS")} TEST> Took average of {((total / (double)Stopwatch.Frequency) / iterations)}ms to complete. Did {iterations} times"); Log.HandleLogs = true; } }
/// <summary> /// Serialise a single token. /// </summary> /// <param name="token"></param> public static void Parse(IToken token) { SerialiseObject wrapper = new SerialiseObject(); JsonSerializerOptions options = new JsonSerializerOptions { WriteIndented = true }; var Json = JsonSerializer.Serialize(wrapper, options) + "\n"; IRWriter.Write(Json, @path); }
/// <summary> /// Parse a given array of tokens. /// </summary> /// <param name="tokens">The tokens to be parsed.</param> public static void Parse(IToken[] tokens) { JsonRoot jsonRoot; SerialiseObject serialiseObject; // Set the Json intentation option to true. JsonSerializerOptions options = new JsonSerializerOptions { WriteIndented = true }; // Instansiate a jsonroot object and set its internal array of tokens // to the number of tokens passed to it. jsonRoot = new JsonRoot(); jsonRoot.tokenarray = new SerialiseObject[tokens.Length]; // For each token. for (int i = 0; i < tokens.Length; i++) { // If it is not null if (tokens[i] != null) { // Set the serialiseObject to the values stored in the token. IToken token = tokens[i]; serialiseObject = new SerialiseObject(); serialiseObject.tokenID = token.GetID(); serialiseObject.attributes = token.GetNamedParameters(); // Style information goes here... // Add the serialiseObject representation of the token to the JsonRoot object. jsonRoot.tokenarray[i] = serialiseObject; } } // Serialise the JsonRoot object and write it. var Json = JsonSerializer.Serialize <JsonRoot>(jsonRoot, options); IRWriter.Write(Json, @path); }
public void EmitTest() { StringBuilder builder = new StringBuilder(); IRWriter.EmitToString(baseInterpreter, builder, WithCondition); }
public static void Main(string[] args) { if (Directory.Exists(Directory.GetCurrentDirectory() + "/StaticBindings") == false) { Directory.CreateDirectory(Directory.GetCurrentDirectory() + "/StaticBindings"); } CreateBindings.DirectoryPath = Directory.GetCurrentDirectory() + "/StaticBindings"; CreateBindings.Create <Color>(); CreateBindings.CreateFinalFile(); //DOML.LinkBindings(); Console.SetWindowSize((int)(Console.LargestWindowWidth / 1.5f), (int)(Console.LargestWindowHeight / 1.5f)); Console.OutputEncoding = System.Text.Encoding.UTF8; using (StreamReader reader = new StreamReader(File.OpenRead("test.doml"))) { int c = reader.Read(); while (c > 0) { Console.Write((char)c); c = reader.Read(); } } TopLevelNode topLevel = new Parser().ParseAST(new StreamReader(File.OpenRead("test.doml"))); topLevel.BasicCodegen(Console.Out, false); topLevel.BasicCodegen(Console.Out, true); foreach (Instruction instruction in topLevel.GetInstructions()) { Console.Write(instruction.OpCode); foreach (object obj in instruction.Parameters) { Console.Write(" " + obj); } Console.WriteLine(); } topLevel.Print(Console.Out); Console.Read(); return; #if BenchmarkDotNet //Summary summary = BenchmarkRunner.Run<AllTests>(); //Console.Read(); #elif StaticBindings if (Directory.Exists(Directory.GetCurrentDirectory() + "/StaticBindings") == false) { Directory.CreateDirectory(Directory.GetCurrentDirectory() + "/StaticBindings"); } CreateBindings.DirectoryPath = Directory.GetCurrentDirectory() + "/StaticBindings"; CreateBindings.Create <Color>("System"); CreateBindings.CreateFinalFile(); #elif TestBindings DOMLBindings.LinkBindings(); Interpreter interpreter = Parser.GetInterpreterFromText(@" // This is a comment // Construct a new System.Color @ Test = System.Color ... ; .RGB = 255, 64, 128 // Implicit 'array' @ TheSame = System.Color ... ; .RGB->Normalised = 1, 0.25, 0.5, // You can have trailing commas @ AgainSame = System.Color ... ; .RGB->Hex = 0xFF4080 ; .Name = ""OtherName"" /* Multi Line Comment Blocks are great */ @ Copy = System.Color... ; .RGB = Test.R, Test.G, Test.B ; .Name = ""Copy"" "); string withComments; string withoutComments; IRWriter.EmitToLocation(interpreter, Directory.GetCurrentDirectory() + "/CompactOutput.IR", false, false); IRWriter.EmitToLocation(interpreter, Directory.GetCurrentDirectory() + "/Output.IR", false, true); StringBuilder builder = new StringBuilder(); IRWriter.EmitToString(interpreter, builder, true); withComments = builder.ToString(); builder.Clear(); IRWriter.EmitToString(interpreter, builder, false); withoutComments = builder.ToString(); Interpreter c; using (StringReader reader = new StringReader(withComments)) { c = Parser.GetInterpreterFromIR(reader); c.HandleSafeInstruction(new Instruction()); } using (StringReader reader = new StringReader(withoutComments)) { c = Parser.GetInterpreterFromIR(reader); c.HandleSafeInstruction(new Instruction()); } Console.Read(); return; TestDOML.RunStringTest(@" // This is a comment // Construct a new System.Color @ Test = System.Color ... ; .RGB = 255, 64, 128 // Implicit 'array' @ TheSame = System.Color ... ; .RGB->Normalised = 1, 0.25, 0.5, // You can have trailing commas @ AgainSame = System.Color ... ; .RGB->Hex = 0xFF4080 ; .Name = ""OtherName"" /* Multi Line Comment Blocks are great */ @ Copy = System.Color... ; .RGB = Test.R, Test.G, Test.B ; .Name = ""Copy"" ", 5, Config.READ_EMIT); Console.Write(Log.HandleLogs); Console.Read(); #else Log.LogHandler += Log_LogHandler; Action <InterpreterRuntime> Set_RGB = (InterpreterRuntime runtime) => { if (runtime.Pop(out Colour result)) { // Handle if (!runtime.Pop(out result.B) || !runtime.Pop(out result.G) || !runtime.Pop(out result.R)) { Log.Error("Pops failed"); return; } result.R /= 255; result.G /= 255; result.B /= 255; } }; Action <InterpreterRuntime> Get_RGB = (InterpreterRuntime runtime) => { if (!runtime.Pop(out Colour result) || !runtime.Push(result.R, true) || !runtime.Push(result.G, true) || !runtime.Push(result.B, true)) { Log.Error("Pushes failed"); } }; InstructionRegister.RegisterConstructor("System.Color", (InterpreterRuntime runtime) => { Colour colour = new Colour(); Colours.Add(colour); if (!runtime.Push(colour, true)) { Log.Error("Creation failed"); } }); InstructionRegister.RegisterSetter("RGB", "System.Color", 3, Set_RGB); InstructionRegister.RegisterGetter("RGB", "System.Color", 3, Get_RGB); InstructionRegister.RegisterSetter("RGB.Normalised", "System.Color", 3, (InterpreterRuntime runtime) => { if (runtime.Pop(out Colour result)) { if (!runtime.Pop(out result.B) || !runtime.Pop(out result.G) || !runtime.Pop(out result.R)) { Log.Error("Pops failed"); } } }); Parser.GetInterpreterFromText(@" @ Test = System.Color ... // Comment ; .RGB(Normalised) = 0.5, 0.25, 0.1, ").Execute(true);//Emit(Directory.GetCurrentDirectory() + "/Test.doml", true, true); /*TestDOML.RunStringTest(@" * @ Test = System.Color ... // Comment * ; .RGB(Normalised) = 0.5, 0.25, 0.1, * ", 100, Config.ALL);*/ Colours.ForEach(y => Log.Info($"Colour RGB: {y.R} {y.G} {y.B}")); Console.Read(); #endif }