Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Stack/InterpreterStackStringifier.cs @ 17104

Last change on this file since 17104 was 15289, checked in by pkimmesw, 7 years ago

#2665 Fixed analyzer, fixed Plush encoding + operators, adpated print evaluation according to McPhee

File size: 3.7 KB
Line 
1using System.Collections.Generic;
2using System.Linq;
3
4namespace HeuristicLab.Problems.ProgramSynthesis.Push.Stack {
5  using System.Globalization;
6
7  using HeuristicLab.Problems.ProgramSynthesis.Push.Constants;
8  using HeuristicLab.Problems.ProgramSynthesis.Push.Extensions;
9  using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
10
11  public static class InterpreterStackStringifier {
12
13    public static IEnumerable<string> StringifyStack(this IPushInterpreter interpreter, StackTypes type) {
14      switch (type) {
15        case StackTypes.Boolean: return StringifyLiteralStack(interpreter.BooleanStack);
16        case StackTypes.Integer: return StringifyLiteralStack(interpreter.IntegerStack);
17        case StackTypes.Float: return StringifyFloatStack(interpreter.FloatStack, interpreter.Configuration.FloatStringFormat);
18        case StackTypes.Char: return StringifyCharStack(interpreter.CharStack);
19        case StackTypes.String: return StringifyLiteralStack(interpreter.StringStack, PushEnvironment.StringSymbolStr, PushEnvironment.StringSymbolStr);
20        case StackTypes.Name: return StringifyLiteralStack(interpreter.NameStack);
21        case StackTypes.Code: return StringifyLiteralStack(interpreter.CodeStack);
22        case StackTypes.Exec: return StringifyLiteralStack(interpreter.ExecStack);
23        case StackTypes.Print: return StringifyLiteralStack(interpreter.PrintStack, PushEnvironment.StringSymbolStr, PushEnvironment.StringSymbolStr, false);
24
25        case StackTypes.BooleanVector: return StringifyVectorStack(interpreter.BooleanVectorStack);
26        case StackTypes.IntegerVector: return StringifyVectorStack(interpreter.IntegerVectorStack);
27        case StackTypes.FloatVector: return StringifyFloatVectorStack(interpreter.FloatVectorStack, interpreter.Configuration.FloatStringFormat);
28        case StackTypes.StringVector: return StringifyVectorStack(interpreter.StringVectorStack, PushEnvironment.StringSymbolStr, PushEnvironment.StringSymbolStr);
29
30        default: return Enumerable.Empty<string>();
31      }
32    }
33
34    private static IEnumerable<string> StringifyCharStack(IPushStack<char> stack) {
35      var result = stack.AsEnumerable();
36
37      return result.Select(x => PushEnvironment.CharSymbol + x.Printify() + PushEnvironment.CharSymbol);
38    }
39
40    private static IEnumerable<string> StringifyFloatStack(IPushStack<double> stack, string format) {
41      var result = stack.AsEnumerable();
42
43      return result.Select(x => x.ToString(format, CultureInfo.InvariantCulture));
44    }
45
46    private static IEnumerable<string> StringifyLiteralStack(IPushStack stack, string prefix = "", string postfix = "", bool reverse = true) {
47      var result = stack.AsStrings();
48
49      if (reverse)
50        result = result.Reverse();
51
52      return result.Select(x => prefix + x.ToString() + postfix);
53    }
54
55    private static IEnumerable<string> StringifyFloatVectorStack(IPushStack<IReadOnlyList<double>> stack, string format) {
56      var separator = PushEnvironment.VectorSeparatorSymbol.ToString();
57      return stack
58        .Reverse()
59        .Select(x => PushEnvironment.VectorStartSymbol + string.Join(separator, x.Select(_ => _.ToString(format, CultureInfo.InvariantCulture))) + PushEnvironment.VectorEndSymbol);
60    }
61
62    private static IEnumerable<string> StringifyVectorStack<T>(IPushStack<IReadOnlyList<T>> stack, string prefix = "", string postfix = "") {
63      var separator = PushEnvironment.VectorSeparatorSymbol.ToString();
64      return stack
65        .Reverse()
66        .Select(x => PushEnvironment.VectorStartSymbol + string.Join(separator, x.Select(_ => prefix + _.ToString() + postfix)) + PushEnvironment.VectorEndSymbol);
67    }
68  }
69}
Note: See TracBrowser for help on using the repository browser.