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