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 DoCountExpression : LoopExpression {
|
---|
12 |
|
---|
13 | protected DoCountExpression() { }
|
---|
14 | protected DoCountExpression(LoopState state) : base(state) { }
|
---|
15 | [StorableConstructor]
|
---|
16 | protected DoCountExpression(bool deserializing) : base(deserializing) { }
|
---|
17 | protected override bool HasInsufficientArguments(IInternalPushInterpreter interpreter, IPushStack<Expression> sourceStack) {
|
---|
18 | return interpreter.IntegerStack.Count < 1 ||
|
---|
19 | sourceStack.Count == 0 ||
|
---|
20 | interpreter.IntegerStack.Top <= 0;
|
---|
21 | }
|
---|
22 |
|
---|
23 | protected override LoopState InitState(IInternalPushInterpreter interpreter, IPushStack<Expression> sourceStack) {
|
---|
24 | var state = LoopState.Create(interpreter.PoolContainer.LoopStatePool,
|
---|
25 | body: sourceStack.Pop(),
|
---|
26 | currentIndex: 1,
|
---|
27 | destinationIndex: interpreter.IntegerStack.Top,
|
---|
28 | incrementor: 1);
|
---|
29 |
|
---|
30 | interpreter.IntegerStack.SetTop(0);
|
---|
31 |
|
---|
32 | return state;
|
---|
33 | }
|
---|
34 | }
|
---|
35 |
|
---|
36 | /// <summary>
|
---|
37 | /// An iteration instruction that performs a loop (the body of which is taken from the CODE stack) the number of times
|
---|
38 | /// indicated by
|
---|
39 | /// the INTEGER argument, pushing an index (which runs from zero to one less than the number of iterations) onto the
|
---|
40 | /// INTEGER stack
|
---|
41 | /// prior to each execution of the loop body. This should be implemented as a macro that expands into a call to
|
---|
42 | /// CODE.DO*RANGE.
|
---|
43 | /// CODE.DO*COUNT takes a single INTEGER argument (the number of times that the loop will be executed) and a single
|
---|
44 | /// CODE argument
|
---|
45 | /// (the body of the loop). If the provided INTEGER argument is negative or zero then this becomes a NOOP. Otherwise it
|
---|
46 | /// expands into:
|
---|
47 | /// ( 0 '1 - IntegerArg>' CODE.QUOTE 'CodeArg' CODE.DO*RANGE )
|
---|
48 | /// </summary>
|
---|
49 | [Serializable]
|
---|
50 | [StorableClass]
|
---|
51 | [PushExpression(StackTypes.Code, "CODE.DO*COUNT", StackTypes.Integer)]
|
---|
52 | public class CodeDoCountExpression : DoCountExpression {
|
---|
53 |
|
---|
54 | public CodeDoCountExpression() { }
|
---|
55 | public CodeDoCountExpression(LoopState state) : base(state) { }
|
---|
56 | [StorableConstructor]
|
---|
57 | protected CodeDoCountExpression(bool deserializing) : base(deserializing) { }
|
---|
58 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
59 | return State.Body == null && HasInsufficientArguments(interpreter, interpreter.CodeStack);
|
---|
60 | }
|
---|
61 |
|
---|
62 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
63 | Eval(interpreter, interpreter.CodeStack);
|
---|
64 | }
|
---|
65 |
|
---|
66 | protected override LoopExpression Clone(LoopState state, IInternalPushInterpreter interpreter) {
|
---|
67 | var expression = interpreter.PoolContainer.GetStatefulExpression<CodeDoCountExpression>();
|
---|
68 | expression.State = state;
|
---|
69 |
|
---|
70 | return expression;
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | /// <summary>
|
---|
75 | /// An iteration instruction that performs a loop (the body of which is taken from the EXEC stack) the number of times
|
---|
76 | /// indicated
|
---|
77 | /// by the INTEGER argument, pushing an index (which runs from zero to one less than the number of iterations) onto the
|
---|
78 | /// INTEGER
|
---|
79 | /// stack prior to each execution of the loop body. This is similar to CODE.DO*COUNT except that it takes its code
|
---|
80 | /// argument from
|
---|
81 | /// the EXEC stack. This should be implemented as a macro that expands into a call to EXEC.DO*RANGE. EXEC.DO*COUNT
|
---|
82 | /// takes a single
|
---|
83 | /// INTEGER argument (the number of times that the loop will be executed) and a single EXEC argument (the body of the
|
---|
84 | /// loop). If
|
---|
85 | /// the provided INTEGER argument is negative or zero then this becomes a NOOP. Otherwise it expands into:
|
---|
86 | /// ( 0 '1 - IntegerArg' EXEC.DO* RANGE'ExecArg' )
|
---|
87 | /// </summary>
|
---|
88 | [Serializable]
|
---|
89 | [StorableClass]
|
---|
90 | [PushExpression(StackTypes.Exec, "EXEC.DO*COUNT", StackTypes.Integer)]
|
---|
91 | public class ExecDoCountExpression : DoCountExpression {
|
---|
92 | public ExecDoCountExpression() { }
|
---|
93 | public ExecDoCountExpression(LoopState state) : base(state) { }
|
---|
94 | [StorableConstructor]
|
---|
95 | protected ExecDoCountExpression(bool deserializing) : base(deserializing) { }
|
---|
96 |
|
---|
97 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
98 | Eval(interpreter, interpreter.ExecStack);
|
---|
99 | }
|
---|
100 |
|
---|
101 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
102 | return State.Body == null && HasInsufficientArguments(interpreter, interpreter.ExecStack);
|
---|
103 | }
|
---|
104 |
|
---|
105 | protected override LoopExpression Clone(LoopState state, IInternalPushInterpreter interpreter) {
|
---|
106 | var expression = interpreter.PoolContainer.GetStatefulExpression<ExecDoCountExpression>();
|
---|
107 | expression.State = state;
|
---|
108 |
|
---|
109 | return expression;
|
---|
110 | }
|
---|
111 | }
|
---|
112 | } |
---|