public override TruVal Interpret(Environment env) { TruVal funcVal = this.func.Interpret(env); if (funcVal is TruCallable callable) { return(callable.call(env, this.args)); } else { throw new TruRuntimeException($"'{funcVal}' is not callable."); } }
/// Interprets expr, checks its type, and returns it. Throws an error if the type doesn't match. private static T Check <T>(TruExpr expr, Environment env) where T : TruVal { TruVal val = expr.Interpret(env); if (val is T t) { return(t); } else { throw new TruRuntimeException($"Incorrect types, given {val.GetType().Name} expected {typeof(T).Name}."); } }
/// 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}"); } } } }
public Node(string name, TruVal val, Node next) { this.name = name; this.val = val; this.next = next; }