1 | namespace 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 : StatefullExpression<string> {
|
---|
8 | public NameDefineXExecExpression(string state) : base(state) { }
|
---|
9 |
|
---|
10 | public override void Eval(IPushGpInterpreter 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 |
|
---|
21 | public override string StringRepresentation { get { return this.State; } }
|
---|
22 | }
|
---|
23 |
|
---|
24 | /// <summary>
|
---|
25 | /// Sets a flag indicating that the next State encountered will be pushed onto the NAME stack (and not have its
|
---|
26 | /// associated value
|
---|
27 | /// pushed onto the EXEC stack), regardless of whether or not it has a definition. Upon encountering such a State and
|
---|
28 | /// pushing it
|
---|
29 | /// onto the NAME stack the flag will be cleared (whether or not the pushed State had a definition).
|
---|
30 | /// </summary>
|
---|
31 | [PushExpression(StackType.Name, "NAME.QUOTE")]
|
---|
32 | public class NameQuoteExpression : StatelessExpression {
|
---|
33 | public override void Eval(IPushGpInterpreter interpreter) {
|
---|
34 | interpreter.IsNameQuoteFlagSet = true;
|
---|
35 | }
|
---|
36 | }
|
---|
37 |
|
---|
38 | /// <summary>
|
---|
39 | /// Pushes a randomly selected NAME that already has a definition.
|
---|
40 | /// </summary>
|
---|
41 | [PushExpression(StackType.Name, "NAME.RANDBOUNDNAME")]
|
---|
42 | public class NameRandBoundNameExpression : StatelessExpression {
|
---|
43 | public override void Eval(IPushGpInterpreter interpreter) {
|
---|
44 | if (interpreter.CustomExpressions.Count == 0)
|
---|
45 | return;
|
---|
46 |
|
---|
47 | var index = interpreter.CustomExpressions.Keys.Count == 1
|
---|
48 | ? 0
|
---|
49 | : interpreter.Random.Next(0, interpreter.CustomExpressions.Keys.Count - 1);
|
---|
50 |
|
---|
51 | var state = interpreter.CustomExpressions.Keys.ElementAt(index);
|
---|
52 |
|
---|
53 | interpreter.NameStack.Push(state);
|
---|
54 | }
|
---|
55 | }
|
---|
56 | } |
---|