/// Parses a string into multiple exressions, and returns an array of any values calculated. /// Does not include null values in the array (ie. when define statements are interpreted). /// Environment defaults to standard library. public static TruVal[] InterpretAll(string code, Environment env = null) { env = env ?? TruLibrary.Library; TruStatement[] exprs = TruStatement.ParseAll(code); List <TruVal> rtrnVals = new List <TruVal>(); foreach (TruStatement expr in exprs) { TruVal result = expr.Interpret(env); if (result != null) { rtrnVals.Add(result); } } return(rtrnVals.ToArray()); }
/// Launches a REPL where you can evaluate Tru statements. static void Main(string[] args) { Console.WriteLine(shortHelpStr); Environment global = TruLibrary.Library; while (true) { Console.Write(">>> "); string input = Console.ReadLine(); if (input == "quit" || input == "exit") { return; } else if (input == "help") { Console.WriteLine(helpStr); } else { try { TruStatement[] statements = TruStatement.ParseAll(input); foreach (TruStatement statement in statements) { TruVal result = statement.Interpret(global); if (result != null) { Console.WriteLine(result); } } } catch (TruSyntaxError e) { Console.WriteLine($"Syntax Error: {e.Message}"); } catch (TruRuntimeException e) { Console.WriteLine($"Runtime Exception: {e.Message}"); } } } }
/// Parses a string into Tru abstract syntax that can be Executed. public static TruStatement Parse(string code) { return(TruStatement.Parse(ExprTree.Parse(code))); }
/// Parses and Interprets a string. Environment defaults to the standard library. public static TruVal Interpret(string code, Environment env = null) { env = env ?? TruLibrary.Library; return(TruStatement.Parse(code).Interpret(env)); }