namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions { using System.Linq; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes; using HeuristicLab.Problems.ProgramSynthesis.Push.Stack; using Interpreter; [StorableClass] public class NameDefineXExecExpression : StatefulExpression { public NameDefineXExecExpression(string state) : base(state) { } [StorableConstructor] protected NameDefineXExecExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return false; } public override void Eval(IInternalPushInterpreter interpreter) { Expression expression; if (!interpreter.IsNameQuoteFlagSet && interpreter.CustomExpressions.TryGetValue(State, out expression)) { interpreter.ExecStack.Push(expression); } else { interpreter.NameStack.Push(State); interpreter.IsNameQuoteFlagSet = false; } } } /// /// Sets a flag indicating that the next State encountered will be pushed onto the NAME stack (and not have its /// associated value /// pushed onto the EXEC stack), regardless of whether or not it has a definition. Upon encountering such a State and /// pushing it /// onto the NAME stack the flag will be cleared (whether or not the pushed State had a definition). /// [PushExpression(StackTypes.Name, "NAME.QUOTE")] [StorableClass] public class NameQuoteExpression : StatelessExpression { public NameQuoteExpression() { } [StorableConstructor] protected NameQuoteExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.IsNameQuoteFlagSet; } public override void Eval(IInternalPushInterpreter interpreter) { interpreter.IsNameQuoteFlagSet = true; } } /// /// Pushes a randomly selected NAME that already has a definition. /// [PushExpression(StackTypes.Name, "NAME.RANDBOUNDNAME")] [StorableClass] public class NameRandBoundNameExpression : StatelessExpression { public NameRandBoundNameExpression() { } [StorableConstructor] protected NameRandBoundNameExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.CustomExpressions.Count == 0; } public override void Eval(IInternalPushInterpreter interpreter) { var index = interpreter.CustomExpressions.Keys.Count == 1 ? 0 : interpreter.Random.Next(0, interpreter.CustomExpressions.Keys.Count - 1); var state = interpreter.CustomExpressions.Keys.ElementAt(index); interpreter.NameStack.Push(state); } } }