1 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
|
---|
2 |
|
---|
3 | using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
|
---|
4 | using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
|
---|
5 |
|
---|
6 | using Interpreter;
|
---|
7 |
|
---|
8 | /// <summary>
|
---|
9 | /// If the top item of the BOOLEAN stack is TRUE then this removes the second item on the EXEC stack, leaving the first
|
---|
10 | /// item to be executed. If it is false then it removes the first item, leaving the second to be executed. This is similar to
|
---|
11 | /// CODE.IF except that it operates on the EXEC stack. This acts as a NOOP unless there are at least two items on the EXEC stack and
|
---|
12 | /// one item on the BOOLEAN stack.
|
---|
13 | /// </summary>
|
---|
14 | [PushExpression(StackTypes.Exec, "EXEC.IF", StackTypes.Boolean)]
|
---|
15 | public class ExecIfExpression : StatelessExpression {
|
---|
16 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
17 | // not enough arguments on stack
|
---|
18 | if ((interpreter.BooleanStack.Count == 0) || (interpreter.ExecStack.Count < 2)) return false;
|
---|
19 |
|
---|
20 | var condition = interpreter.BooleanStack.Pop();
|
---|
21 |
|
---|
22 | if (condition) interpreter.ExecStack.RemoveAt(interpreter.ExecStack.Count - 2);
|
---|
23 | else interpreter.ExecStack.RemoveTop();
|
---|
24 |
|
---|
25 | return true;
|
---|
26 | }
|
---|
27 | }
|
---|
28 |
|
---|
29 | /// <summary>
|
---|
30 | /// Inserts beneath the top item of the EXEC stack a new item of the form
|
---|
31 | /// "( EXEC.Y <TopItem> )".
|
---|
32 | /// </summary>
|
---|
33 | [PushExpression(StackTypes.Exec, "EXEC.Y")]
|
---|
34 | public class ExecYExpression : StatelessExpression {
|
---|
35 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
36 | // not enough arguments on stack
|
---|
37 | if (interpreter.ExecStack.Count == 0 ||
|
---|
38 | interpreter.Configuration.MaxPointsInProgram < 2)
|
---|
39 | return false;
|
---|
40 |
|
---|
41 | var top = interpreter.ExecStack.Top;
|
---|
42 |
|
---|
43 | if (top is PushProgram && ((PushProgram)top).Depth == interpreter.Configuration.MaxDepth)
|
---|
44 | return false;
|
---|
45 |
|
---|
46 | var execYExpression = ExpressionTable.GetStatelessExpression<ExecYExpression>();
|
---|
47 | var expressions = interpreter.PoolContainer.ExpressionListPool.Get();
|
---|
48 |
|
---|
49 | expressions.Add(top);
|
---|
50 | expressions.Add(execYExpression);
|
---|
51 |
|
---|
52 | var result = PushProgram.Create(interpreter.PoolContainer.PushProgramPool, expressions);
|
---|
53 |
|
---|
54 | interpreter.ExecStack.SetTop(result);
|
---|
55 | interpreter.ExecStack.Add(top);
|
---|
56 |
|
---|
57 | return true;
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | /// <summary>
|
---|
62 | /// Removes the second item on the EXEC stack.
|
---|
63 | /// </summary>
|
---|
64 | [PushExpression(StackTypes.Exec, "EXEC.K")]
|
---|
65 | public class ExecKExpression : StatelessExpression {
|
---|
66 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
67 | if (interpreter.ExecStack.Count < 2) return false;
|
---|
68 |
|
---|
69 | var top = interpreter.ExecStack.Pop();
|
---|
70 | interpreter.ExecStack.SetTop(top);
|
---|
71 |
|
---|
72 | return true;
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | /// <summary>
|
---|
77 | /// Pops 3 items from the EXEC stack, which we will call A, B, and C
|
---|
78 | /// (with A being the first one popped). Then pushes a list containing B and C back onto the EXEC stack, followed by
|
---|
79 | /// another instance of C, followed by another instance of A.
|
---|
80 | /// </summary>
|
---|
81 | [PushExpression(StackTypes.Exec, "EXEC.S")]
|
---|
82 | public class ExecSExpression : StatelessExpression {
|
---|
83 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
84 | if (interpreter.ExecStack.Count < 3) return false;
|
---|
85 |
|
---|
86 | // check max depth
|
---|
87 | if ((interpreter.ExecStack.Top is PushProgram && ((PushProgram)interpreter.ExecStack.Top).Depth == interpreter.Configuration.MaxDepth) ||
|
---|
88 | (interpreter.ExecStack[1] is PushProgram && ((PushProgram)interpreter.ExecStack[1]).Depth == interpreter.Configuration.MaxDepth))
|
---|
89 | return false;
|
---|
90 |
|
---|
91 | var a = interpreter.ExecStack.Top;
|
---|
92 | var b = interpreter.ExecStack[1];
|
---|
93 | var c = interpreter.ExecStack[2];
|
---|
94 | interpreter.ExecStack.Remove(2);
|
---|
95 |
|
---|
96 | var expressions = interpreter.PoolContainer.ExpressionListPool.Get();
|
---|
97 | expressions.Add(c);
|
---|
98 | expressions.Add(b);
|
---|
99 |
|
---|
100 | var newTop = PushProgram.Create(interpreter.PoolContainer.PushProgramPool, expressions);
|
---|
101 |
|
---|
102 | interpreter.ExecStack.SetTop(newTop);
|
---|
103 | interpreter.ExecStack.Push(c, a);
|
---|
104 |
|
---|
105 | return true;
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | /// <summary>
|
---|
110 | /// Does nothing.
|
---|
111 | /// </summary>
|
---|
112 | [PushExpression(StackTypes.Exec, "EXEC.NOOP")]
|
---|
113 | public class ExecNoopExpression : StatelessExpression {
|
---|
114 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
115 | return false;
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | [PushExpression(StackTypes.Exec, "EXEC.WHILE", StackTypes.Boolean)]
|
---|
120 | public class ExecWhileExpression : StatelessExpression {
|
---|
121 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
122 | if (interpreter.ExecStack.IsEmpty)
|
---|
123 | return false;
|
---|
124 |
|
---|
125 | if (interpreter.BooleanStack.IsEmpty) {
|
---|
126 | interpreter.ExecStack.Pop();
|
---|
127 | return true;
|
---|
128 | }
|
---|
129 |
|
---|
130 | var booleanTop = interpreter.BooleanStack.Pop();
|
---|
131 |
|
---|
132 | if (!booleanTop) {
|
---|
133 | interpreter.ExecStack.Pop();
|
---|
134 | return true;
|
---|
135 | }
|
---|
136 |
|
---|
137 | interpreter.ExecStack.Push(this, interpreter.ExecStack.Top);
|
---|
138 | return true;
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | [PushExpression(StackTypes.Exec, "EXEC.DO*WHILE")]
|
---|
143 | public class ExecDoWhileExpression : StatelessExpression {
|
---|
144 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
145 | if (interpreter.ExecStack.IsEmpty)
|
---|
146 | return false;
|
---|
147 |
|
---|
148 | interpreter.ExecStack.Push(this, interpreter.ExecStack.Top);
|
---|
149 | return true;
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | [PushExpression(StackTypes.Exec, "EXEC.WHEN", StackTypes.Boolean)]
|
---|
154 | public class ExecWhenExpression : StatelessExpression {
|
---|
155 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
156 | if (interpreter.ExecStack.IsEmpty ||
|
---|
157 | interpreter.BooleanStack.IsEmpty)
|
---|
158 | return false;
|
---|
159 |
|
---|
160 | var when = interpreter.BooleanStack.Pop();
|
---|
161 |
|
---|
162 | if (!when) {
|
---|
163 | interpreter.ExecStack.Pop();
|
---|
164 | }
|
---|
165 |
|
---|
166 | return true;
|
---|
167 | }
|
---|
168 | }
|
---|
169 | } |
---|