private static bool RunFile(string filePath, Scope scope = null) { Parser = new ProgramParser(new FileInfo(filePath).FullName); try { WulInterpreter.Interpret(LoadFile(filePath), scope); } catch (ParseException pe) { string program = File.ReadLines(filePath).Skip(pe.Line - 1).First(); Console.WriteLine(program); string underline = pe.GetUnderline; if (underline != null) { Console.WriteLine(underline); } Console.WriteLine(pe.GetErrorMessage); return(false); } catch (Exception e) { Console.WriteLine($"Error: {e.Message}"); #if DEBUG Console.WriteLine(e.StackTrace); //TODO fix up traceback line numbers //StdLib.Debug.Traceback(Value.EmptyList, null); #endif return(false); } return(true); }
public List <IValue> Evaluate(List <IValue> arguments, Scope scope) { Scope currentScope = ParentScope.EmptyChildScope(); currentScope["self"] = this; //Bind arguments to names for (int i = 0; i < ArgumentNames.Count; ++i) { string argName = ArgumentNames[i]; if (argName == "...") { currentScope[argName] = new ListTable(arguments.Skip(i)); } else { IValue argValue = i >= arguments.Count ? Value.Nil : arguments[i]; currentScope[argName] = argValue; } } // If the function has no named parameters, then bind them to $0, $1... if (ArgumentNames.Count == 0) { currentScope["$args"] = new ListTable(arguments); for (int i = 0; i < arguments.Count; ++i) { currentScope["$" + i] = arguments[i]; } } return(WulInterpreter.Interpret(Body, currentScope)); }
public void Interpreter_PrintNil() { ProgramParser parser = new ProgramParser(); string program = "(print nil)"; ProgramNode node = (ProgramNode)parser.Parse(program); WulInterpreter.Interpret(node); }
public static List <IValue> EvalMany(this SyntaxNode node, Scope scope) { List <IValue> result = WulInterpreter.Interpret(node, scope) ?? new List <IValue>(); while (result.FirstOrDefault() is SyntaxNode) { result = WulInterpreter.Interpret(result.FirstOrDefault() as SyntaxNode, scope); } return(result); }
public static IValue Eval(this SyntaxNode node, Scope scope) { IValue result = WulInterpreter.Interpret(node, scope).FirstOrDefault() ?? Value.Nil; while (result is SyntaxNode) { result = WulInterpreter.Interpret(result as SyntaxNode, scope).FirstOrDefault() ?? Value.Nil; } return(result); }
public override string Value(Scope scope = null) { var strings = new List <string>(); foreach (var chunk in _chunks) { if (chunk.String != null) { strings.Add(chunk.String); } else if (chunk.Interpolation != null) { strings.Add(WulInterpreter.Interpret(chunk.Interpolation, scope).First().AsString()); } } return(string.Join("", strings)); }
private static bool RunString(string input, Scope scope = null) { Parser = new ProgramParser("stdin"); Scope currentScope = scope ?? Global.Scope.EmptyChildScope(); try { var programNode = (ProgramNode)Parser.Parse(input); var result = WulInterpreter.Interpret(programNode, currentScope); if (result != null && result.Any()) { foreach (var item in result) { if (ReferenceEquals(item, Value.Nil) && result.Count == 1) { continue; } var args = new List <IValue> { item is WulString ? new WulString($"'{item.AsString()}'") : item }; IO.Print(args, Global.Scope); } } return(true); } catch (ParseException pe) { string underline = pe.GetUnderline; if (underline != null) { Console.WriteLine(underline); } Console.WriteLine(pe.GetErrorMessage); return(false); } catch (Exception e) { Console.WriteLine($"Error: {e.Message}"); System.Diagnostics.Debug.WriteLine(e.StackTrace); return(false); } }
public List <IValue> Execute(ListNode list, Scope scope) { Scope currentScope = ParentScope.EmptyChildScope(); var arguments = list.Children.ToArray(); //Bind arguments to names if (arguments.Any()) { currentScope["self"] = arguments[0]; } for (int i = 1; i <= ArgumentNames.Count; ++i) { string argName = ArgumentNames[i - 1]; if (argName == "...") { currentScope[argName] = new ListNode(list, arguments.Skip(i).ToList()); } else { IValue argValue = i >= arguments.Length ? new IdentifierNode(list, "nil") : arguments[i]; currentScope[argName] = argValue; } } // If the function has no named parameters, then bind them to $0, $1... if (ArgumentNames.Count == 0) { currentScope["$args"] = new ListNode(list, arguments.Skip(1).ToList()); for (int i = 1; i < arguments.Length; ++i) { currentScope["$" + (i - 1)] = arguments[i]; } } return(WulInterpreter.Interpret(Body, currentScope)); }
// full file name public static List <IValue> LoadFile(string fileName, Scope scope) { ProgramNode program = ParseFile(fileName); return(WulInterpreter.Interpret(program, scope)); }
public static IValue EvalOnce(this SyntaxNode node, Scope scope) { return(WulInterpreter.Interpret(node, scope).FirstOrDefault() ?? Value.Nil); }
public static List <IValue> EvalManyOnce(this SyntaxNode node, Scope scope) { return(WulInterpreter.Interpret(node, scope)); }