1 | using System;
|
---|
2 | using System.Threading.Tasks;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Linq;
|
---|
5 | using HeuristicLab.Problems.ProgramSynthesis;
|
---|
6 |
|
---|
7 | namespace HeuristicLab.Algorithms.PushGP.Cli {
|
---|
8 |
|
---|
9 | class Program {
|
---|
10 | static void Main(string[] args) {
|
---|
11 | string code = null;
|
---|
12 |
|
---|
13 | if (args.Length == 0) {
|
---|
14 | var lines = new List<string>();
|
---|
15 |
|
---|
16 | Console.WriteLine(@"Program: ( must start with '(' and end with ')' )");
|
---|
17 |
|
---|
18 | var bracesCount = 0;
|
---|
19 | var linesRead = 0;
|
---|
20 |
|
---|
21 | while (bracesCount > 0 || lines.Count == 0 || linesRead == 0) {
|
---|
22 | var line = Console.ReadLine();
|
---|
23 | linesRead++;
|
---|
24 |
|
---|
25 | if (string.IsNullOrWhiteSpace(line))
|
---|
26 | continue;
|
---|
27 |
|
---|
28 | line = line.Trim();
|
---|
29 | lines.Add(line);
|
---|
30 |
|
---|
31 | var openBraces = line.Count(c => c == PushEnvironment.ProgramStartSymbol);
|
---|
32 | var closeBraces = line.Count(c => c == PushEnvironment.ProgramEndSymbol);
|
---|
33 | bracesCount += openBraces - closeBraces;
|
---|
34 | }
|
---|
35 |
|
---|
36 | code = string.Join(" ", lines);
|
---|
37 | } else {
|
---|
38 | code = args[0];
|
---|
39 | }
|
---|
40 |
|
---|
41 | EvaluateStepwise(code).Wait();
|
---|
42 |
|
---|
43 | Console.WriteLine(@"Press any key to terminate...");
|
---|
44 | }
|
---|
45 |
|
---|
46 | static async Task EvaluateStepwise(string code) {
|
---|
47 | var interpreter = new PushInterpreter(new PushConfiguration {
|
---|
48 | TopLevelPushCode = false
|
---|
49 | });
|
---|
50 |
|
---|
51 | Console.WriteLine(@"Press ESC to cancel, SPACE to resume, Any other key to step...");
|
---|
52 | interpreter.Run(code, true);
|
---|
53 |
|
---|
54 | while (!interpreter.IsCompleted) {
|
---|
55 | Console.Clear();
|
---|
56 | interpreter.PrintStacks();
|
---|
57 | interpreter.Step();
|
---|
58 |
|
---|
59 | var input = Console.ReadKey();
|
---|
60 | if (input.Key == ConsoleKey.Escape) {
|
---|
61 | break;
|
---|
62 | }
|
---|
63 |
|
---|
64 | if (input.Key == ConsoleKey.Spacebar) {
|
---|
65 | await interpreter.ResumeAsync();
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | Console.Clear();
|
---|
70 | interpreter.PrintStacks();
|
---|
71 | }
|
---|
72 | }
|
---|
73 | } |
---|