Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/PrintExpressions.cs @ 14897

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

#2665 Dynamic ErcValues, Separate Push from BenchmarkSuite Push

File size: 4.4 KB
Line 
1using System;
2
3namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
4  using System.Collections.Generic;
5  using System.Text;
6
7  using Attributes;
8
9  using HeuristicLab.Problems.ProgramSynthesis.Push.Constants;
10
11  using Interpreter;
12  using Stack;
13
14  [PushExpression(StackTypes.Print, "PRINT.NEWLINE")]
15  public class PrintNewLineExpression : StatelessExpression {
16    public override bool Eval(IInternalPushInterpreter interpreter) {
17      interpreter.PrintStack.Push(Environment.NewLine);
18      return true;
19    }
20  }
21
22  public abstract class PrintExpression<T> : StatelessExpression {
23    protected bool Eval(IInternalPushInterpreter interpreter, IPushStack<T> stack) {
24      if (stack.IsEmpty)
25        return false;
26
27      var value = stack.Pop().ToString();
28      interpreter.PrintStack.Push(value);
29      return true;
30    }
31
32    protected bool EvalVector(IInternalPushInterpreter interpreter, IPushStack<List<T>> vectorStack) {
33      if (vectorStack.IsEmpty)
34        return false;
35
36      var sb = new StringBuilder();
37      var vector = vectorStack.Pop();
38
39      sb.Append(PushEnvironment.VectorStartSymbol);
40
41      if (vector.Count > 0) {
42        sb.Append(vector[0]);
43
44        for (var i = 1; i < vector.Count; i++) {
45          sb.Append(PushEnvironment.VectorSeparatorSymbol);
46          sb.Append(vector[i]);
47        }
48      }
49
50      sb.Append(PushEnvironment.VectorEndSymbol);
51
52      var value = sb.ToString();
53      interpreter.PrintStack.Push(value);
54      return true;
55    }
56  }
57
58  [PushExpression(StackTypes.Print, "PRINT.PRINTBOOLEAN", StackTypes.Boolean)]
59  public class BooleanPrintExpression : PrintExpression<bool> {
60    public override bool Eval(IInternalPushInterpreter interpreter) {
61      return Eval(interpreter, interpreter.BooleanStack);
62    }
63  }
64
65  [PushExpression(StackTypes.Print, "PRINT.PRINTCHAR", StackTypes.Char)]
66  public class CharPrintExpression : PrintExpression<char> {
67    public override bool Eval(IInternalPushInterpreter interpreter) {
68      return Eval(interpreter, interpreter.CharStack);
69    }
70  }
71
72  [PushExpression(StackTypes.Print, "PRINT.PRINTEXEC", StackTypes.Exec)]
73  public class ExecPrintExpression : PrintExpression<Expression> {
74    public override bool Eval(IInternalPushInterpreter interpreter) {
75      return Eval(interpreter, interpreter.ExecStack);
76    }
77  }
78
79  [PushExpression(StackTypes.Print, "PRINT.PRINTFLOAT", StackTypes.Float)]
80  public class FloatPrintExpression : PrintExpression<double> {
81    public override bool Eval(IInternalPushInterpreter interpreter) {
82      return Eval(interpreter, interpreter.FloatStack);
83    }
84  }
85
86  [PushExpression(StackTypes.Print, "PRINT.PRINTINTEGER", StackTypes.Integer)]
87  public class IntegerPrintExpression : PrintExpression<long> {
88    public override bool Eval(IInternalPushInterpreter interpreter) {
89      return Eval(interpreter, interpreter.IntegerStack);
90    }
91  }
92
93  [PushExpression(StackTypes.Print, "PRINT.PRINTSTRING", StackTypes.String)]
94  public class StringPrintExpression : PrintExpression<string> {
95    public override bool Eval(IInternalPushInterpreter interpreter) {
96      return Eval(interpreter, interpreter.StringStack);
97    }
98  }
99
100  [PushExpression(StackTypes.Print, "PRINT.PRINTINTEGERVECTOR", StackTypes.IntegerVector)]
101  public class IntegerVectorPrintExpression : PrintExpression<long> {
102    public override bool Eval(IInternalPushInterpreter interpreter) {
103      return EvalVector(interpreter, interpreter.IntegerVectorStack);
104    }
105  }
106
107  [PushExpression(StackTypes.Print, "PRINT.PRINTFLOATVECTOR", StackTypes.FloatVector)]
108  public class FloatVectorPrintExpression : PrintExpression<double> {
109    public override bool Eval(IInternalPushInterpreter interpreter) {
110      return EvalVector(interpreter, interpreter.FloatVectorStack);
111    }
112  }
113
114  [PushExpression(StackTypes.Print, "PRINT.PRINTSTRINGVECTOR", StackTypes.StringVector)]
115  public class StringVectorPrintExpression : PrintExpression<string> {
116    public override bool Eval(IInternalPushInterpreter interpreter) {
117      return EvalVector(interpreter, interpreter.StringVectorStack);
118    }
119  }
120
121  [PushExpression(StackTypes.Print, "PRINT.PRINTBOOLEANVECTOR", StackTypes.BooleanVector)]
122  public class BooleanVectorPrintExpression : PrintExpression<bool> {
123    public override bool Eval(IInternalPushInterpreter interpreter) {
124      return EvalVector(interpreter, interpreter.BooleanVectorStack);
125    }
126  }
127}
Note: See TracBrowser for help on using the repository browser.