static void Main(string[] args) { if (args.Length == 0) { PrintUsageAndExit(-1); } if (args[0] == "-h") { PrintUsageAndExit(0); } try { #if DEBUG if (!Debugger.IsAttached) { Console.Error.WriteLine("This is a debug exe."); Console.Error.WriteLine("Do you want to attach the debugger? (y/N)"); var answer = Console.ReadLine(); // I know this looks horrible if (answer.ToLower().StartsWith("y")) { Debugger.Launch(); } Console.Error.WriteLine("Do you want redirect Debug.Print to Console.Error? (y/N)"); answer = Console.ReadLine(); // I know this also looks horrible if (answer.ToLower().StartsWith("y")) { // Redirect Debug.Print/WriteLines to console.error var consoleListener = new ConsoleTraceListener(true); Debug.Listeners.Add(consoleListener); } } #endif // Read file var src = File.ReadAllText(Path.Combine(Environment.CurrentDirectory, args[0])); var app = MtCompiler.CreateScriptApp(Console.OpenStandardOutput()); // Evaluate program var wait = app.Evaluate(src) as MtResult; if (wait == null) { throw new Exception("Evaluate(src) returned null. WTF??"); } // Wait for the end of the program wait.GetValueSync((o) => { }); } catch (Exception e) { PrintExceptionAndExit(e); } }
public void EvalHelloWorldApp() { var stream = new MemoryStream(); var app = MtCompiler.CreateScriptApp(stream); var res = app.Evaluate("print(\"Hello World!\");\n") as MtResult; Assert.IsNotNull(res); res.GetValueSync((o) => { /* Do nothing */ }); String expected = string.Format("Hello World!{0}", Environment.NewLine); String outputed = Encoding.UTF8.GetString(stream.ToArray()); Assert.AreEqual <string>(expected, outputed, string.Format("Actualy is '{0}'", outputed)); }
private void Exec(object param) { var out_stream = new MemoryStreamWithNotifications(); out_stream.Writed += out_stream_Writed; out_stream.WriteUTF8("[INFO] Starting...\r\n"); var start = DateTime.Now; try { // This is fast var compiler = MtCompiler.CreateScriptApp(out_stream); // This may take a while var res = compiler.Evaluate(param as string); if (res == null) { throw new Exception("Evaluation returned null. WTF!?"); } // This may take a LONG time res.WaitForValue(); var dur = DateTime.Now - start; out_stream.WriteUTF8("[INFO] Completed in {0} seconds.\r\n".ExtFormat(dur.TotalSeconds)); } catch (ThreadAbortException) { var dur = DateTime.Now - start; out_stream.WriteUTF8("[ABORTED] After {0} seconds.\r\n".ExtFormat(dur.TotalSeconds)); } catch (Exception ex) { out_stream.WriteUTF8("[ERROR] Exception executing script {0}.\r\n".ExtFormat(ex.Message)); } finally { Done(); } }