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