/// <summary> /// Execute the program represented by a bunch of lines of source code. /// </summary> /// <param name="program"></param> /// <param name="input">The values to pass to the program.</param> public IEnumerable <Value> Execute(IEnumerable <string> program, GetNext input) { var functions = parser.Parse(program); Linker abraham = new Linker(verbose: false); //who kept the country as one big compilation unit foreach (var kvp in BuiltInFactory.BuiltInFunctions) { abraham.Register(kvp.Key, kvp.Value); } foreach (var functionGraph in functions) { abraham.Register(functionGraph.Name, (topArgs, bottomArgs) => new UserFunctionStub(functionGraph, topArgs, bottomArgs, abraham)); } abraham.Register("Expand", (topArgs, bottomArgs) => new Expand(topArgs, bottomArgs, abraham)); IFunction main; try { main = abraham.Resolve("Main", new Value[0], new Value[0]); } catch (ExecutionException ex) { throw new ExecutionException("You must define exactly one function called Main.", ex); } return(RunMain(main, input)); }
private GetNext WrapInput(GetNext input, Queue <Value> buffer, bool allowMoreThanBuffered) { if (input == null) { return(null); } return(() => TryTakeBuffer(input, buffer, allowMoreThanBuffered)); }
public IEnumerable <Value> ReadOutput(GetNext f) { Value next; while (next = f()) { yield return(next); } yield return(Value.Finished); }
private Value TryTakeBuffer(GetNext input, Queue <Value> buffer, bool readIfEmpty) { if (buffer.TryDequeue(out Value tmp)) { return(tmp); } else { return(readIfEmpty ? input() : Value.Finished); } }
private Value ReadOrGetStored(GetNext input, bool isTop) { if (isTop == topStored && stored != null) { return(stored); } else if (input != null) { return(input()); } else { throw new ExecutionException("{0} tried to read from a {1} input that doesn't exist. Are you missing a pipeline?", Name, isTop ? "top" : "bottom"); } }
private IEnumerable <Value> RunMain(IFunction main, GetNext input) { ITopIn mainIn = main.Is <ITopIn>(); ITopOut mainOut = main.Is <ITopOut>(); mainIn.TopInput = input; Value next; if (printReady) { yield return(new Value("Ready!\n", FluencyType.String)); } while (next = mainOut.Top()) { yield return(next); } yield return(Value.Finished); }
private bool BufferArguments(int ensureBufferHas, Queue <Value> bufferInto, Value[] arguments, GetNext next) { bufferInto.EnqueueRange(arguments); while (bufferInto.Count < ensureBufferHas) { Value v = next(); if (v.Done) { bufferInto.Clear(); return(false); } else { bufferInto.Enqueue(v); } } return(true); }
static void Main(string[] args) { ArgumentParser a = new ArgumentParser(args); if (a.Verbose) { Console.WriteLine("My arguments are: {0}", string.Join(' ', args)); } if (a.Help) { PrintHelp(Environment.GetCommandLineArgs()[0]); return; } if (a.Autofix) { var fixer = new Autofix(a.Autofix, a.TabWidth); foreach (var file in a.Leftover) { if (fixer.FixFile(file)) { Console.WriteLine("Autofixed " + file); } } return; } var p = new Parser(a.Verbose, a.TabWarn, a.TabWidth); if (a.WriteGraph) { if (!Directory.Exists("../Graphs")) { Directory.CreateDirectory("../Graphs"); } p.Register(graph => { var writer = new GraphWriter(); writer.WalkFunctionGraph(graph.Head); writer.Serialize(graph.Name + ".dgml"); }); } if (a.PrintGraph) { p.Register(graph => { var printer = new GraphPrinter(graph.Head, a.Unicode); Console.WriteLine(printer.Print()); Console.WriteLine(); }); } var fileLines = a.Leftover.Where(x => x.EndsWith(".fl")).SelectMany(x => File.ReadAllLines(x)); var console = new ConsoleIO(); GetNext readFrom = console.Read; bool printready = true; if (a.CountFrom != null) { readFrom = new NumberGenerator(a.CountFrom.Value, a.CountTo ?? int.MaxValue).ReadSequential; printready = false; } if (a.ReadFile != null) { readFrom = new FileReader(a.ReadFile, a.Separator).Read; printready = false; } var result = new Interpreter(p, printReady: printready).Execute(fileLines, readFrom); console.Write(result); }