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