Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/PushGP.Cli/Program.cs @ 15771

Last change on this file since 15771 was 15771, checked in by bburlacu, 6 years ago

#2895: Add solution skeleton for PushGP with genealogy analysis.

File size: 1.9 KB
Line 
1using System;
2using System.Threading.Tasks;
3using System.Collections.Generic;
4using System.Linq;
5using HeuristicLab.Problems.ProgramSynthesis;
6
7namespace 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}
Note: See TracBrowser for help on using the repository browser.