1 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
|
---|
2 | using System.Collections.Generic;
|
---|
3 |
|
---|
4 | using HeuristicLab.Common;
|
---|
5 | using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
|
---|
6 | using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
|
---|
7 | using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
|
---|
8 |
|
---|
9 | /// <summary>
|
---|
10 | /// Pushes TRUE onto the BOOLEAN stack if the top two items are equal, or FALSE otherwise
|
---|
11 | /// </summary>
|
---|
12 | /// <typeparam name="T">Stacktype</typeparam>
|
---|
13 | public abstract class EqualsExpression<T> : StatelessExpression {
|
---|
14 | public bool Eval(IPushStack<T> stack, IPushStack<bool> booleanStack) {
|
---|
15 | if (stack.Count < 2) return false;
|
---|
16 |
|
---|
17 | var first = stack[1];
|
---|
18 | var second = stack.Top;
|
---|
19 | stack.Remove(2);
|
---|
20 |
|
---|
21 | booleanStack.Push(first.Equals(second));
|
---|
22 |
|
---|
23 | return true;
|
---|
24 | }
|
---|
25 | }
|
---|
26 |
|
---|
27 | [PushExpression(StackTypes.Integer, "INTEGER.=", StackTypes.Boolean)]
|
---|
28 | public class IntegerEqualsExpression : EqualsExpression<long> {
|
---|
29 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
30 | return Eval(interpreter.IntegerStack, interpreter.BooleanStack);
|
---|
31 | }
|
---|
32 | }
|
---|
33 |
|
---|
34 | [PushExpression(StackTypes.Float, "FLOAT.=", StackTypes.Boolean)]
|
---|
35 | public class FloatEqualsExpression : EqualsExpression<double> {
|
---|
36 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
37 | if (interpreter.FloatStack.Count < 2) return false;
|
---|
38 |
|
---|
39 | var first = interpreter.FloatStack[1];
|
---|
40 | var second = interpreter.FloatStack.Top;
|
---|
41 | interpreter.FloatStack.Remove(2);
|
---|
42 |
|
---|
43 | interpreter.BooleanStack.Push(first.IsAlmost(second));
|
---|
44 | return true;
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | [PushExpression(StackTypes.Boolean, "BOOLEAN.=", StackTypes.Boolean)]
|
---|
49 | public class BooleanEqualsExpression : EqualsExpression<bool> {
|
---|
50 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
51 | return Eval(interpreter.BooleanStack, interpreter.BooleanStack);
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | [PushExpression(StackTypes.Name, "NAME.=", StackTypes.Boolean)]
|
---|
56 | public class NameEqualsExpression : EqualsExpression<string> {
|
---|
57 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
58 | return Eval(interpreter.NameStack, interpreter.BooleanStack);
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | [PushExpression(StackTypes.Exec, "EXEC.=", StackTypes.Boolean)]
|
---|
63 | public class ExecEqualsExpression : EqualsExpression<Expression> {
|
---|
64 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
65 | return Eval(interpreter.ExecStack, interpreter.BooleanStack);
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | [PushExpression(StackTypes.Code, "CODE.=", StackTypes.Boolean)]
|
---|
70 | public class CodeEqualsExpression : EqualsExpression<Expression> {
|
---|
71 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
72 | return Eval(interpreter.CodeStack, interpreter.BooleanStack);
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | [PushExpression(StackTypes.Char, "CHAR.=", StackTypes.Boolean)]
|
---|
77 | public class CharEqualsExpression : EqualsExpression<char> {
|
---|
78 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
79 | return Eval(interpreter.CharStack, interpreter.BooleanStack);
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | [PushExpression(StackTypes.String, "STRING.=", StackTypes.Boolean)]
|
---|
84 | public class StringEqualsExpression : EqualsExpression<string> {
|
---|
85 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
86 | return Eval(interpreter.StringStack, interpreter.BooleanStack);
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | [PushExpression(StackTypes.IntegerVector, "INTEGER[].=", StackTypes.Boolean)]
|
---|
91 | public class IntegerVectorEqualsExpression : EqualsExpression<List<long>> {
|
---|
92 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
93 | return Eval(interpreter.IntegerVectorStack, interpreter.BooleanStack);
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | [PushExpression(StackTypes.FloatVector, "FLOAT[].=", StackTypes.Boolean)]
|
---|
98 | public class FloatVectorEqualsExpression : EqualsExpression<List<double>> {
|
---|
99 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
100 | return Eval(interpreter.FloatVectorStack, interpreter.BooleanStack);
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | [PushExpression(StackTypes.BooleanVector, "BOOLEAN[].=", StackTypes.Boolean)]
|
---|
105 | public class BooleanVectorEqualsExpression : EqualsExpression<List<bool>> {
|
---|
106 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
107 | return Eval(interpreter.BooleanVectorStack, interpreter.BooleanStack);
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | [PushExpression(StackTypes.StringVector, "STRING[].=", StackTypes.Boolean)]
|
---|
112 | public class StringVectorEqualsExpression : EqualsExpression<List<string>> {
|
---|
113 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
114 | return Eval(interpreter.StringVectorStack, interpreter.BooleanStack);
|
---|
115 | }
|
---|
116 | }
|
---|
117 | } |
---|