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