[15771] | 1 | using System;
|
---|
| 2 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; |
---|
| 3 | |
---|
| 4 | namespace HeuristicLab.Problems.ProgramSynthesis { |
---|
[14727] | 5 | /// <summary>
|
---|
[15017] | 6 | /// An iteration instruction that performs a loop (the body of which is taken from the CODE stack) the number of times
|
---|
| 7 | /// indicated by the INTEGER argument, pushing an index (which runs from zero to one less than the number of iterations) onto the
|
---|
| 8 | /// INTEGER stack prior to each execution of the loop body. This should be implemented as a macro that expands into a call to
|
---|
| 9 | /// CODE.DO*RANGE. CODE.DO*COUNT takes a single INTEGER argument (the number of times that the loop will be executed) and a single
|
---|
| 10 | /// CODE argument (the body of the loop). If the provided INTEGER argument is negative or zero then this becomes a NOOP. Otherwise it
|
---|
| 11 | /// expands into: ( 0 '1 - IntegerArg>' CODE.QUOTE 'CodeArg' CODE.DO*RANGE )
|
---|
[14727] | 12 | /// </summary>
|
---|
[14777] | 13 | [Serializable]
|
---|
[14952] | 14 | [StorableClass]
|
---|
[15032] | 15 | [PushExpression(
|
---|
| 16 | StackTypes.Code,
|
---|
| 17 | "CODE.DO*COUNT",
|
---|
| 18 | "An iteration instruction that performs a loop (the body of which is taken from the CODE stack) the number of times indicated by the INTEGER argument, pushing an index (which runs from zero to one less than the number of iterations) onto the INTEGER stack prior to each execution of the loop body.",
|
---|
| 19 | StackTypes.Integer)]
|
---|
[15017] | 20 | public class CodeDoCountExpression : StatelessExpression {
|
---|
[14727] | 21 |
|
---|
| 22 | public CodeDoCountExpression() { }
|
---|
[14952] | 23 | [StorableConstructor]
|
---|
| 24 | protected CodeDoCountExpression(bool deserializing) : base(deserializing) { }
|
---|
[15017] | 25 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
| 26 | var destinationIndex = interpreter.IntegerStack.Top;
|
---|
| 27 | interpreter.IntegerStack.Top = 0;
|
---|
| 28 | interpreter.IntegerStack.Push(destinationIndex);
|
---|
[14727] | 29 |
|
---|
[15017] | 30 | interpreter.ExecStack.Push(ExpressionTable.GetStatelessExpression<CodeDoRangeExpression>());
|
---|
[14727] | 31 | }
|
---|
| 32 |
|
---|
[15017] | 33 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
| 34 | return interpreter.IntegerStack.IsEmpty ||
|
---|
| 35 | interpreter.IntegerStack.Top < 1 ||
|
---|
| 36 | interpreter.CodeStack.IsEmpty;
|
---|
[14727] | 37 | }
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | /// <summary>
|
---|
[15017] | 41 | /// An iteration instruction that performs a loop (the body of which is taken from the EXEC stack) the number of times
|
---|
| 42 | /// indicated by the INTEGER argument, pushing an index (which runs from zero to one less than the number of iterations) onto the
|
---|
| 43 | /// INTEGER stack prior to each execution of the loop body. This is similar to CODE.DO*COUNT except that it takes its code
|
---|
| 44 | /// argument from the EXEC stack. This should be implemented as a macro that expands into a call to EXEC.DO*RANGE. EXEC.DO*COUNT
|
---|
| 45 | /// takes a single INTEGER argument (the number of times that the loop will be executed) and a single EXEC argument (the body of the
|
---|
| 46 | /// loop). If the provided INTEGER argument is negative or zero then this becomes a NOOP. Otherwise it expands into:
|
---|
| 47 | /// ( 0 '1 - IntegerArg' EXEC.DO* RANGE'ExecArg' )
|
---|
[14727] | 48 | /// </summary>
|
---|
[14777] | 49 | [Serializable]
|
---|
[14952] | 50 | [StorableClass]
|
---|
[15032] | 51 | [PushExpression(
|
---|
| 52 | StackTypes.Exec,
|
---|
| 53 | "EXEC.DO*COUNT",
|
---|
| 54 | "An iteration instruction that performs a loop (the body of which is taken from the EXEC stack) the number of times indicated by the INTEGER argument, pushing an index (which runs from zero to one less than the number of iterations) onto the INTEGER stack prior to each execution of the loop body.",
|
---|
| 55 | StackTypes.Integer,
|
---|
[15334] | 56 | requiredBlockCount: 1)]
|
---|
[15017] | 57 | public class ExecDoCountExpression : StatelessExpression {
|
---|
[14727] | 58 | public ExecDoCountExpression() { }
|
---|
[14952] | 59 | [StorableConstructor]
|
---|
| 60 | protected ExecDoCountExpression(bool deserializing) : base(deserializing) { }
|
---|
[14727] | 61 |
|
---|
[14952] | 62 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
[15017] | 63 | var destinationIndex = interpreter.IntegerStack.Top;
|
---|
| 64 | interpreter.IntegerStack.Top = 0;
|
---|
| 65 | interpreter.IntegerStack.Push(destinationIndex);
|
---|
| 66 |
|
---|
| 67 | interpreter.ExecStack.Push(ExpressionTable.GetStatelessExpression<ExecDoRangeExpression>());
|
---|
[14727] | 68 | }
|
---|
| 69 |
|
---|
[14952] | 70 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
[15017] | 71 | return interpreter.IntegerStack.IsEmpty ||
|
---|
| 72 | interpreter.IntegerStack.Top < 1 ||
|
---|
| 73 | interpreter.ExecStack.IsEmpty;
|
---|
[14952] | 74 | }
|
---|
[14727] | 75 | }
|
---|
| 76 | } |
---|