[14727] | 1 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
|
---|
| 2 | using System.Linq;
|
---|
[14952] | 3 |
|
---|
| 4 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[14727] | 5 | using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
|
---|
| 6 | using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
|
---|
| 7 | using Interpreter;
|
---|
| 8 |
|
---|
[14952] | 9 | [StorableClass]
|
---|
[14733] | 10 | public class NameDefineXExecExpression : StatefulExpression<string> {
|
---|
[14727] | 11 | public NameDefineXExecExpression(string state) : base(state) { }
|
---|
[14952] | 12 | [StorableConstructor]
|
---|
| 13 | protected NameDefineXExecExpression(bool deserializing) : base(deserializing) { }
|
---|
[14727] | 14 |
|
---|
[14952] | 15 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
| 16 | return false;
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
[14727] | 20 | Expression expression;
|
---|
| 21 | if (!interpreter.IsNameQuoteFlagSet &&
|
---|
| 22 | interpreter.CustomExpressions.TryGetValue(State, out expression)) {
|
---|
| 23 | interpreter.ExecStack.Push(expression);
|
---|
| 24 | } else {
|
---|
| 25 | interpreter.NameStack.Push(State);
|
---|
| 26 | interpreter.IsNameQuoteFlagSet = false;
|
---|
| 27 | }
|
---|
| 28 | }
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | /// <summary>
|
---|
| 32 | /// Sets a flag indicating that the next State encountered will be pushed onto the NAME stack (and not have its
|
---|
| 33 | /// associated value
|
---|
| 34 | /// pushed onto the EXEC stack), regardless of whether or not it has a definition. Upon encountering such a State and
|
---|
| 35 | /// pushing it
|
---|
| 36 | /// onto the NAME stack the flag will be cleared (whether or not the pushed State had a definition).
|
---|
| 37 | /// </summary>
|
---|
[14777] | 38 | [PushExpression(StackTypes.Name, "NAME.QUOTE")]
|
---|
[14952] | 39 | [StorableClass]
|
---|
[14727] | 40 | public class NameQuoteExpression : StatelessExpression {
|
---|
[14952] | 41 | public NameQuoteExpression() { }
|
---|
| 42 | [StorableConstructor]
|
---|
| 43 | protected NameQuoteExpression(bool deserializing) : base(deserializing) { }
|
---|
[14744] | 44 |
|
---|
[14952] | 45 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
| 46 | return interpreter.IsNameQuoteFlagSet;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
[14727] | 50 | interpreter.IsNameQuoteFlagSet = true;
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | /// <summary>
|
---|
| 55 | /// Pushes a randomly selected NAME that already has a definition.
|
---|
| 56 | /// </summary>
|
---|
[14777] | 57 | [PushExpression(StackTypes.Name, "NAME.RANDBOUNDNAME")]
|
---|
[14952] | 58 | [StorableClass]
|
---|
[14727] | 59 | public class NameRandBoundNameExpression : StatelessExpression {
|
---|
[14952] | 60 | public NameRandBoundNameExpression() { }
|
---|
| 61 | [StorableConstructor]
|
---|
| 62 | protected NameRandBoundNameExpression(bool deserializing) : base(deserializing) { }
|
---|
[14727] | 63 |
|
---|
[14952] | 64 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
| 65 | return interpreter.CustomExpressions.Count == 0;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
[14727] | 69 | var index = interpreter.CustomExpressions.Keys.Count == 1
|
---|
| 70 | ? 0
|
---|
| 71 | : interpreter.Random.Next(0, interpreter.CustomExpressions.Keys.Count - 1);
|
---|
| 72 |
|
---|
| 73 | var state = interpreter.CustomExpressions.Keys.ElementAt(index);
|
---|
| 74 | interpreter.NameStack.Push(state);
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 | } |
---|