[14727] | 1 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
|
---|
[14777] | 2 | using System;
|
---|
| 3 |
|
---|
[14952] | 4 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[14727] | 5 | using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
|
---|
| 6 | using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
|
---|
| 7 | using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
|
---|
| 8 |
|
---|
[14777] | 9 | [Serializable]
|
---|
[14952] | 10 | [StorableClass]
|
---|
[14727] | 11 | public abstract class DoCountExpression : LoopExpression {
|
---|
| 12 |
|
---|
| 13 | protected DoCountExpression() { }
|
---|
| 14 | protected DoCountExpression(LoopState state) : base(state) { }
|
---|
[14952] | 15 | [StorableConstructor]
|
---|
| 16 | protected DoCountExpression(bool deserializing) : base(deserializing) { }
|
---|
[14834] | 17 | protected override bool HasInsufficientArguments(IInternalPushInterpreter interpreter, IPushStack<Expression> sourceStack) {
|
---|
[14727] | 18 | return interpreter.IntegerStack.Count < 1 ||
|
---|
| 19 | sourceStack.Count == 0 ||
|
---|
| 20 | interpreter.IntegerStack.Top <= 0;
|
---|
| 21 | }
|
---|
| 22 |
|
---|
[14834] | 23 | protected override LoopState InitState(IInternalPushInterpreter interpreter, IPushStack<Expression> sourceStack) {
|
---|
[14777] | 24 | var state = LoopState.Create(interpreter.PoolContainer.LoopStatePool,
|
---|
[14727] | 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>
|
---|
[14777] | 49 | [Serializable]
|
---|
[14952] | 50 | [StorableClass]
|
---|
[14777] | 51 | [PushExpression(StackTypes.Code, "CODE.DO*COUNT", StackTypes.Integer)]
|
---|
[14727] | 52 | public class CodeDoCountExpression : DoCountExpression {
|
---|
| 53 |
|
---|
| 54 | public CodeDoCountExpression() { }
|
---|
| 55 | public CodeDoCountExpression(LoopState state) : base(state) { }
|
---|
[14952] | 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 | }
|
---|
[14727] | 61 |
|
---|
[14952] | 62 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
| 63 | Eval(interpreter, interpreter.CodeStack);
|
---|
[14727] | 64 | }
|
---|
| 65 |
|
---|
[14834] | 66 | protected override LoopExpression Clone(LoopState state, IInternalPushInterpreter interpreter) {
|
---|
[14777] | 67 | var expression = interpreter.PoolContainer.GetStatefulExpression<CodeDoCountExpression>();
|
---|
| 68 | expression.State = state;
|
---|
| 69 |
|
---|
| 70 | return expression;
|
---|
[14727] | 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>
|
---|
[14777] | 88 | [Serializable]
|
---|
[14952] | 89 | [StorableClass]
|
---|
[14777] | 90 | [PushExpression(StackTypes.Exec, "EXEC.DO*COUNT", StackTypes.Integer)]
|
---|
[14727] | 91 | public class ExecDoCountExpression : DoCountExpression {
|
---|
| 92 | public ExecDoCountExpression() { }
|
---|
| 93 | public ExecDoCountExpression(LoopState state) : base(state) { }
|
---|
[14952] | 94 | [StorableConstructor]
|
---|
| 95 | protected ExecDoCountExpression(bool deserializing) : base(deserializing) { }
|
---|
[14727] | 96 |
|
---|
[14952] | 97 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
| 98 | Eval(interpreter, interpreter.ExecStack);
|
---|
[14727] | 99 | }
|
---|
| 100 |
|
---|
[14952] | 101 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
| 102 | return State.Body == null && HasInsufficientArguments(interpreter, interpreter.ExecStack);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[14834] | 105 | protected override LoopExpression Clone(LoopState state, IInternalPushInterpreter interpreter) {
|
---|
[14777] | 106 | var expression = interpreter.PoolContainer.GetStatefulExpression<ExecDoCountExpression>();
|
---|
| 107 | expression.State = state;
|
---|
| 108 |
|
---|
| 109 | return expression;
|
---|
[14727] | 110 | }
|
---|
| 111 | }
|
---|
| 112 | } |
---|