using System; using System.Threading.Tasks; namespace HeuristicLab.Algorithms.PushGP.Cli { using System.Collections.Generic; using System.Linq; using HeuristicLab.Problems.ProgramSynthesis.Push.Configuration; using HeuristicLab.Problems.ProgramSynthesis.Push.Constants; using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter; class Program { static void Main(string[] args) { string code = null; if (args.Length == 0) { var lines = new List(); Console.WriteLine(@"Program: ( must start with '(' and end with ')' )"); var bracesCount = 0; var linesRead = 0; while (bracesCount > 0 || lines.Count == 0 || linesRead == 0) { var line = Console.ReadLine(); linesRead++; if (string.IsNullOrWhiteSpace(line)) continue; line = line.Trim(); lines.Add(line); var openBraces = line.Count(c => c == PushEnvironment.ProgramStartSymbol); var closeBraces = line.Count(c => c == PushEnvironment.ProgramEndSymbol); bracesCount += openBraces - closeBraces; } code = string.Join(" ", lines); } else { code = args[0]; } EvaluateStepwise(code).Wait(); Console.WriteLine(@"Press any key to terminate..."); } static async Task EvaluateStepwise(string code) { var interpreter = new PushInterpreter(new PushConfiguration { TopLevelPushCode = false }); Console.WriteLine(@"Press ESC to cancel, SPACE to resume, Any other key to step..."); interpreter.Run(code, true); while (!interpreter.IsCompleted) { Console.Clear(); interpreter.PrintStacks(); interpreter.Step(); var input = Console.ReadKey(); if (input.Key == ConsoleKey.Escape) { break; } if (input.Key == ConsoleKey.Spacebar) { await interpreter.ResumeAsync(); } } Console.Clear(); interpreter.PrintStacks(); } } }