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