source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/DoCountExpressions.cs @ 14908

Last change on this file since 14908 was 14908, checked in by pkimmesw, 2 years ago

#2665 Removed "this" qualifier

File size: 4.2 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
2  using System;
3
4  using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
5  using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
6  using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
7
8  [Serializable]
9  public abstract class DoCountExpression : LoopExpression {
10
11    protected DoCountExpression() { }
12    protected DoCountExpression(LoopState state) : base(state) { }
13    protected override bool HasInsufficientArguments(IInternalPushInterpreter interpreter, IPushStack<Expression> sourceStack) {
14      return interpreter.IntegerStack.Count < 1 ||
15             sourceStack.Count == 0 ||
16             interpreter.IntegerStack.Top <= 0;
17    }
18
19    protected override LoopState InitState(IInternalPushInterpreter interpreter, IPushStack<Expression> sourceStack) {
20      var state = LoopState.Create(interpreter.PoolContainer.LoopStatePool,
21        body: sourceStack.Pop(),
22        currentIndex: 1,
23        destinationIndex: interpreter.IntegerStack.Top,
24        incrementor: 1);
25
26      interpreter.IntegerStack.SetTop(0);
27
28      return state;
29    }
30  }
31
32  /// <summary>
33  ///     An iteration instruction that performs a loop (the body of which is taken from the CODE stack) the number of times
34  ///     indicated by
35  ///     the INTEGER argument, pushing an index (which runs from zero to one less than the number of iterations) onto the
36  ///     INTEGER stack
37  ///     prior to each execution of the loop body. This should be implemented as a macro that expands into a call to
38  ///     CODE.DO*RANGE.
39  ///     CODE.DO*COUNT takes a single INTEGER argument (the number of times that the loop will be executed) and a single
40  ///     CODE argument
41  ///     (the body of the loop). If the provided INTEGER argument is negative or zero then this becomes a NOOP. Otherwise it
42  ///     expands into:
43  ///     ( 0 '1 - IntegerArg>' CODE.QUOTE 'CodeArg' CODE.DO*RANGE )
44  /// </summary>
45  [Serializable]
46  [PushExpression(StackTypes.Code, "CODE.DO*COUNT", StackTypes.Integer)]
47  public class CodeDoCountExpression : DoCountExpression {
48
49    public CodeDoCountExpression() { }
50
51    public CodeDoCountExpression(LoopState state) : base(state) { }
52
53    public override bool Eval(IInternalPushInterpreter interpreter) {
54      return Eval(interpreter, interpreter.CodeStack);
55    }
56
57    protected override LoopExpression Clone(LoopState state, IInternalPushInterpreter interpreter) {
58      var expression = interpreter.PoolContainer.GetStatefulExpression<CodeDoCountExpression>();
59      expression.State = state;
60
61      return expression;
62    }
63  }
64
65  /// <summary>
66  ///     An iteration instruction that performs a loop (the body of which is taken from the EXEC stack) the number of times
67  ///     indicated
68  ///     by the INTEGER argument, pushing an index (which runs from zero to one less than the number of iterations) onto the
69  ///     INTEGER
70  ///     stack prior to each execution of the loop body. This is similar to CODE.DO*COUNT except that it takes its code
71  ///     argument from
72  ///     the EXEC stack. This should be implemented as a macro that expands into a call to EXEC.DO*RANGE. EXEC.DO*COUNT
73  ///     takes a single
74  ///     INTEGER argument (the number of times that the loop will be executed) and a single EXEC argument (the body of the
75  ///     loop). If
76  ///     the provided INTEGER argument is negative or zero then this becomes a NOOP. Otherwise it expands into:
77  ///     ( 0 '1 - IntegerArg' EXEC.DO* RANGE'ExecArg' )
78  /// </summary>
79  [Serializable]
80  [PushExpression(StackTypes.Exec, "EXEC.DO*COUNT", StackTypes.Integer)]
81  public class ExecDoCountExpression : DoCountExpression {
82    public ExecDoCountExpression() { }
83    public ExecDoCountExpression(LoopState state) : base(state) { }
84
85    public override bool Eval(IInternalPushInterpreter interpreter) {
86      return Eval(interpreter, interpreter.ExecStack);
87    }
88
89    protected override LoopExpression Clone(LoopState state, IInternalPushInterpreter interpreter) {
90      var expression = interpreter.PoolContainer.GetStatefulExpression<ExecDoCountExpression>();
91      expression.State = state;
92
93      return expression;
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.