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