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 DoCountExpression : LoopExpression {
|
---|
7 |
|
---|
8 | protected DoCountExpression() { }
|
---|
9 | protected DoCountExpression(LoopState state) : base(state) { }
|
---|
10 | protected override bool HasInsufficientArguments(IPushInterpreter interpreter, IStack<Expression> sourceStack) {
|
---|
11 | return interpreter.IntegerStack.Count < 1 ||
|
---|
12 | sourceStack.Count == 0 ||
|
---|
13 | interpreter.IntegerStack.Top <= 0;
|
---|
14 | }
|
---|
15 |
|
---|
16 | protected override LoopState InitState(IPushInterpreter interpreter, IStack<Expression> sourceStack) {
|
---|
17 | var state = new LoopState(
|
---|
18 | body: sourceStack.Pop(),
|
---|
19 | currentIndex: 1,
|
---|
20 | destinationIndex: interpreter.IntegerStack.Top,
|
---|
21 | incrementor: 1);
|
---|
22 |
|
---|
23 | interpreter.IntegerStack.SetTop(0);
|
---|
24 |
|
---|
25 | return state;
|
---|
26 | }
|
---|
27 | }
|
---|
28 |
|
---|
29 | /// <summary>
|
---|
30 | /// An iteration instruction that performs a loop (the body of which is taken from the CODE stack) the number of times
|
---|
31 | /// indicated by
|
---|
32 | /// the INTEGER argument, pushing an index (which runs from zero to one less than the number of iterations) onto the
|
---|
33 | /// INTEGER stack
|
---|
34 | /// prior to each execution of the loop body. This should be implemented as a macro that expands into a call to
|
---|
35 | /// CODE.DO*RANGE.
|
---|
36 | /// CODE.DO*COUNT takes a single INTEGER argument (the number of times that the loop will be executed) and a single
|
---|
37 | /// CODE argument
|
---|
38 | /// (the body of the loop). If the provided INTEGER argument is negative or zero then this becomes a NOOP. Otherwise it
|
---|
39 | /// expands into:
|
---|
40 | /// ( 0 '1 - IntegerArg>' CODE.QUOTE 'CodeArg' CODE.DO*RANGE )
|
---|
41 | /// </summary>
|
---|
42 | [PushExpression(StackType.Code, "CODE.DO*COUNT")]
|
---|
43 | public class CodeDoCountExpression : DoCountExpression {
|
---|
44 |
|
---|
45 | public CodeDoCountExpression() { }
|
---|
46 |
|
---|
47 | public CodeDoCountExpression(LoopState state) : base(state) { }
|
---|
48 |
|
---|
49 | public override bool Eval(IPushInterpreter interpreter) {
|
---|
50 | return this.Eval(interpreter, interpreter.CodeStack);
|
---|
51 | }
|
---|
52 |
|
---|
53 | protected override LoopExpression Clone(LoopState state) {
|
---|
54 | return new CodeDoCountExpression(state);
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | /// <summary>
|
---|
59 | /// An iteration instruction that performs a loop (the body of which is taken from the EXEC stack) the number of times
|
---|
60 | /// indicated
|
---|
61 | /// by the INTEGER argument, pushing an index (which runs from zero to one less than the number of iterations) onto the
|
---|
62 | /// INTEGER
|
---|
63 | /// stack prior to each execution of the loop body. This is similar to CODE.DO*COUNT except that it takes its code
|
---|
64 | /// argument from
|
---|
65 | /// the EXEC stack. This should be implemented as a macro that expands into a call to EXEC.DO*RANGE. EXEC.DO*COUNT
|
---|
66 | /// takes a single
|
---|
67 | /// INTEGER argument (the number of times that the loop will be executed) and a single EXEC argument (the body of the
|
---|
68 | /// loop). If
|
---|
69 | /// the provided INTEGER argument is negative or zero then this becomes a NOOP. Otherwise it expands into:
|
---|
70 | /// ( 0 '1 - IntegerArg' EXEC.DO* RANGE'ExecArg' )
|
---|
71 | /// </summary>
|
---|
72 | [PushExpression(StackType.Exec, "EXEC.DO*COUNT")]
|
---|
73 | public class ExecDoCountExpression : DoCountExpression {
|
---|
74 | public ExecDoCountExpression() { }
|
---|
75 | public ExecDoCountExpression(LoopState state) : base(state) { }
|
---|
76 |
|
---|
77 | public override bool Eval(IPushInterpreter interpreter) {
|
---|
78 | return this.Eval(interpreter, interpreter.ExecStack);
|
---|
79 | }
|
---|
80 |
|
---|
81 | protected override LoopExpression Clone(LoopState state) {
|
---|
82 | return new ExecDoCountExpression(state);
|
---|
83 | }
|
---|
84 | }
|
---|
85 | } |
---|