Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/DoTimesExpressions.cs @ 15341

Last change on this file since 15341 was 15334, checked in by pkimmesw, 7 years ago

#2665 Testet Problems, Testet error functions, Small fixes, Created HL files

File size: 4.7 KB
Line 
1namespace 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  ///     Like CODE.DO*COUNT but does not push the loop counter.
10  ///     This should be implemented as a macro that expands into CODE.DO*RANGE, similarly to the implementation
11  ///     of CODE.DO*COUNT, except that a call to INTEGER.POP should be tacked on to the front of the loop body code
12  ///     in the call to CODE.DO*RANGE.
13  /// </summary>
14  [Serializable]
15  [StorableClass]
16  [PushExpression(StackTypes.Code, "CODE.DO*TIMES", "Like CODE.DO*COUNT but does not push the loop counter", StackTypes.Integer)]
17  public class CodeDoTimesExpression : StatelessExpression {
18    public CodeDoTimesExpression() { }
19    [StorableConstructor]
20    protected CodeDoTimesExpression(bool deserializing) : base(deserializing) { }
21
22    public override void Eval(IInternalPushInterpreter interpreter) {
23      var destinationIndex = Math.Max(0, interpreter.IntegerStack.Top - 1);
24
25      if (destinationIndex == 0) {
26        interpreter.IntegerStack.Pop();
27        interpreter.ExecStack.Push((PushProgram)interpreter.CodeStack.Pop());
28        return;
29      }
30
31      interpreter.IntegerStack.Top = 0;
32      interpreter.IntegerStack.Push(destinationIndex);
33
34      var popExpressions = ExpressionTable.GetStatelessExpression<IntegerPopExpression>();
35      PushProgram program;
36
37      if (!interpreter.CodeStack.Top.IsProgram) {
38        var expressions = interpreter.PoolContainer.ExpressionListPool.Get();
39        expressions.Add(interpreter.ExecStack.Top);
40        expressions.Add(popExpressions);
41
42        program = PushProgram.Create(interpreter.PoolContainer.PushProgramPool, expressions);
43      } else {
44        program = PushProgram.Merge(
45          interpreter.PoolContainer.PushProgramPool,
46          interpreter.PoolContainer.ExpressionListPool,
47          (PushProgram)interpreter.CodeStack.Top,
48          popExpressions);
49      }
50
51      interpreter.CodeStack.Top = program;
52      interpreter.ExecStack.Push(ExpressionTable.GetStatelessExpression<CodeDoRangeExpression>());
53    }
54
55    public override bool IsNoop(IInternalPushInterpreter interpreter) {
56      return interpreter.IntegerStack.IsEmpty ||
57             interpreter.IntegerStack.Top < 0 ||
58             interpreter.CodeStack.IsEmpty;
59    }
60  }
61
62  /// <summary>
63  /// Like EXEC.DO*COUNT but does not push the loop counter. This should be implemented as a macro that expands into
64  /// EXEC.DO*RANGE, similarly to the implementation of EXEC.DO*COUNT, except that a call to INTEGER.POP should be tacked on to the
65  /// front of the loop body code in the call to EXEC.DO*RANGE. This call to INTEGER.POP will remove the loop counter, which will have
66  /// been pushed by EXEC.DO*RANGE, prior to the execution of the loop body.
67  /// </summary>
68  [Serializable]
69  [StorableClass]
70  [PushExpression(StackTypes.Exec, "EXEC.DO*TIMES", "Like EXEC.DO*COUNT but does not push the loop counter", StackTypes.Integer, requiredBlockCount: 1)]
71  public class ExecDoTimesExpression : StatelessExpression {
72    public ExecDoTimesExpression() { }
73    [StorableConstructor]
74    protected ExecDoTimesExpression(bool deserializing) : base(deserializing) { }
75
76    public override void Eval(IInternalPushInterpreter interpreter) {
77      var destinationIndex = Math.Max(0, interpreter.IntegerStack.Top - 1);
78
79      if (destinationIndex == 0) {
80        interpreter.IntegerStack.Pop();
81        return;
82      }
83
84      interpreter.IntegerStack.Top = 0;
85      interpreter.IntegerStack.Push(destinationIndex);
86
87      var popExpressions = ExpressionTable.GetStatelessExpression<IntegerPopExpression>();
88      PushProgram program;
89
90      if (!interpreter.ExecStack.Top.IsProgram) {
91        var expressions = interpreter.PoolContainer.ExpressionListPool.Get();
92        expressions.Add(interpreter.ExecStack.Top);
93        expressions.Add(popExpressions);
94
95        program = PushProgram.Create(interpreter.PoolContainer.PushProgramPool, expressions);
96      } else {
97        program = PushProgram.Merge(
98          interpreter.PoolContainer.PushProgramPool,
99          interpreter.PoolContainer.ExpressionListPool,
100          (PushProgram)interpreter.ExecStack.Top,
101          popExpressions);
102      }
103
104      interpreter.ExecStack.Top = program;
105      interpreter.ExecStack.Push(ExpressionTable.GetStatelessExpression<ExecDoRangeExpression>());
106    }
107
108    public override bool IsNoop(IInternalPushInterpreter interpreter) {
109      return interpreter.IntegerStack.IsEmpty ||
110             interpreter.IntegerStack.Top < 0 ||
111             interpreter.ExecStack.IsEmpty;
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.