Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/NameExpressions.cs @ 14744

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

#2665 Renamings due to typos, ManagedPool tests, Skip Noops in Debugger

File size: 2.3 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
2  using System.Linq;
3  using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
4  using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
5  using Interpreter;
6
7  public class NameDefineXExecExpression : StatefulExpression<string> {
8    public NameDefineXExecExpression(string state) : base(state) { }
9
10    public override bool Eval(IPushInterpreter interpreter) {
11      Expression expression;
12      if (!interpreter.IsNameQuoteFlagSet &&
13          interpreter.CustomExpressions.TryGetValue(State, out expression)) {
14        interpreter.ExecStack.Push(expression);
15      } else {
16        interpreter.NameStack.Push(State);
17        interpreter.IsNameQuoteFlagSet = false;
18      }
19
20      return true;
21    }
22
23    public override string StringRepresentation { get { return this.State; } }
24  }
25
26  /// <summary>
27  ///     Sets a flag indicating that the next State encountered will be pushed onto the NAME stack (and not have its
28  ///     associated value
29  ///     pushed onto the EXEC stack), regardless of whether or not it has a definition. Upon encountering such a State and
30  ///     pushing it
31  ///     onto the NAME stack the flag will be cleared (whether or not the pushed State had a definition).
32  /// </summary>
33  [PushExpression(StackType.Name, "NAME.QUOTE")]
34  public class NameQuoteExpression : StatelessExpression {
35    public override bool Eval(IPushInterpreter interpreter) {
36      if (interpreter.IsNameQuoteFlagSet) return false;
37
38      interpreter.IsNameQuoteFlagSet = true;
39      return true;
40    }
41  }
42
43  /// <summary>
44  ///     Pushes a randomly selected NAME that already has a definition.
45  /// </summary>
46  [PushExpression(StackType.Name, "NAME.RANDBOUNDNAME")]
47  public class NameRandBoundNameExpression : StatelessExpression {
48    public override bool Eval(IPushInterpreter interpreter) {
49      if (interpreter.CustomExpressions.Count == 0)
50        return false;
51
52      var index = interpreter.CustomExpressions.Keys.Count == 1
53        ? 0
54        : interpreter.Random.Next(0, interpreter.CustomExpressions.Keys.Count - 1);
55
56      var state = interpreter.CustomExpressions.Keys.ElementAt(index);
57
58      interpreter.NameStack.Push(state);
59
60      return true;
61    }
62  }
63}
Note: See TracBrowser for help on using the repository browser.