1 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
|
---|
2 | using System;
|
---|
3 |
|
---|
4 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
5 | using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
|
---|
6 | using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
|
---|
7 | using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
|
---|
8 |
|
---|
9 | [Serializable]
|
---|
10 | [StorableClass]
|
---|
11 | public abstract class DoTimesExpression : LoopExpression {
|
---|
12 | protected DoTimesExpression() { }
|
---|
13 | protected DoTimesExpression(LoopState state) : base(state) { }
|
---|
14 | [StorableConstructor]
|
---|
15 | protected DoTimesExpression(bool deserializing) : base(deserializing) { }
|
---|
16 |
|
---|
17 | protected override bool HasInsufficientArguments(IInternalPushInterpreter interpreter, IPushStack<Expression> sourceStack) {
|
---|
18 | return (interpreter.IntegerStack.Count < 1) || (sourceStack.Count == 0) || (interpreter.IntegerStack.Top <= 0);
|
---|
19 | }
|
---|
20 |
|
---|
21 | protected override LoopState InitState(IInternalPushInterpreter interpreter, IPushStack<Expression> sourceStack) {
|
---|
22 | return LoopState.Create(interpreter.PoolContainer.LoopStatePool,
|
---|
23 | body: sourceStack.Pop(),
|
---|
24 | currentIndex: 1,
|
---|
25 | destinationIndex: interpreter.IntegerStack.Pop(),
|
---|
26 | incrementor: 1);
|
---|
27 | }
|
---|
28 |
|
---|
29 | protected override void PushIteration(IInternalPushInterpreter interpreter) {
|
---|
30 | var newState = LoopState.Create(interpreter.PoolContainer.LoopStatePool, State.Body, State.CurrentIndex + State.Incrementor, State.DestinationIndex, State.Incrementor);
|
---|
31 | var nextLoopExpression = Clone(newState, interpreter);
|
---|
32 |
|
---|
33 | interpreter.ExecStack.Push(nextLoopExpression, State.Body);
|
---|
34 | }
|
---|
35 |
|
---|
36 | protected override void PushLastIteration(IInternalPushInterpreter interpreter) {
|
---|
37 | interpreter.ExecStack.Push(State.Body);
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 | /// <summary>
|
---|
42 | /// Like CODE.DO*COUNT but does not push the loop counter.
|
---|
43 | /// This should be implemented as a macro that expands into CODE.DO*RANGE, similarly to the implementation
|
---|
44 | /// of CODE.DO*COUNT, except that a call to INTEGER.POP should be tacked on to the front of the loop body code
|
---|
45 | /// in the call to CODE.DO*RANGE.
|
---|
46 | /// </summary>
|
---|
47 | [Serializable]
|
---|
48 | [StorableClass]
|
---|
49 | [PushExpression(StackTypes.Code, "CODE.DO*TIMES", StackTypes.Integer)]
|
---|
50 | public class CodeDoTimesExpression : DoTimesExpression {
|
---|
51 | public CodeDoTimesExpression() { }
|
---|
52 | public CodeDoTimesExpression(LoopState state) : base(state) { }
|
---|
53 | [StorableConstructor]
|
---|
54 | protected CodeDoTimesExpression(bool deserializing) : base(deserializing) { }
|
---|
55 |
|
---|
56 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
57 | Eval(interpreter, interpreter.CodeStack);
|
---|
58 | }
|
---|
59 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
60 | return State.Body == null && HasInsufficientArguments(interpreter, interpreter.CodeStack);
|
---|
61 | }
|
---|
62 |
|
---|
63 | protected override LoopExpression Clone(LoopState state, IInternalPushInterpreter interpreter) {
|
---|
64 | var expression = interpreter.PoolContainer.GetStatefulExpression<CodeDoTimesExpression>();
|
---|
65 | expression.State = state;
|
---|
66 |
|
---|
67 | return expression;
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | /// <summary>
|
---|
72 | /// Like EXEC.DO*COUNT but does not push the loop counter. This should be implemented as a macro that expands into
|
---|
73 | /// EXEC.DO*RANGE,
|
---|
74 | /// similarly to the implementation of EXEC.DO*COUNT, except that a call to INTEGER.POP should be tacked on to the
|
---|
75 | /// front of the
|
---|
76 | /// loop body code in the call to EXEC.DO*RANGE. This call to INTEGER.POP will remove the loop counter, which will have
|
---|
77 | /// been
|
---|
78 | /// pushed by EXEC.DO*RANGE, prior to the execution of the loop body.
|
---|
79 | /// </summary>
|
---|
80 | [Serializable]
|
---|
81 | [StorableClass]
|
---|
82 | [PushExpression(StackTypes.Exec, "EXEC.DO*TIMES", StackTypes.Integer)]
|
---|
83 | public class ExecDoTimesExpression : DoTimesExpression {
|
---|
84 | public ExecDoTimesExpression() { }
|
---|
85 | public ExecDoTimesExpression(LoopState state) : base(state) { }
|
---|
86 | [StorableConstructor]
|
---|
87 | protected ExecDoTimesExpression(bool deserializing) : base(deserializing) { }
|
---|
88 |
|
---|
89 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
90 | Eval(interpreter, interpreter.ExecStack);
|
---|
91 | }
|
---|
92 |
|
---|
93 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
94 | return State.Body == null && HasInsufficientArguments(interpreter, interpreter.ExecStack);
|
---|
95 | }
|
---|
96 |
|
---|
97 | protected override LoopExpression Clone(LoopState state, IInternalPushInterpreter interpreter) {
|
---|
98 | var expression = interpreter.PoolContainer.GetStatefulExpression<ExecDoTimesExpression>();
|
---|
99 | expression.State = state;
|
---|
100 |
|
---|
101 | return expression;
|
---|
102 | }
|
---|
103 | }
|
---|
104 | } |
---|