source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/DefineExpressions.cs @ 14777

Last change on this file since 14777 was 14777, checked in by pkimmesw, 3 years ago

#2665 simplifier, push solution results view, performance improvements, small bug fixes, ui fixes

File size: 4.0 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
2  using System;
3  using System.Collections.Generic;
4
5  using Attributes;
6  using Interpreter;
7  using Stack;
8
9  /// <summary>
10  ///     Defines the name on top of the NAME stack as an instruction that will push the top item of the T stack onto the
11  ///     EXEC stack.
12  /// </summary>
13  /// <typeparam name="T">Stacktype</typeparam>
14  public abstract class DefineExpression<T> : StatelessExpression {
15    protected bool Eval(
16      IStack<T> stack,
17      IStack<string> nameStack,
18      IDictionary<string, Expression> customExpressions,
19      Func<T, Expression> creator) {
20      // not enough arguments on stack
21      if ((nameStack.Count == 0) || (stack.Count < 1)) return false;
22
23      var name = nameStack.Pop();
24      var expression = creator(stack.Top);
25
26      if (customExpressions.ContainsKey(name)) customExpressions[name] = expression;
27      else customExpressions.Add(name, expression);
28
29      return true;
30    }
31  }
32
33  [PushExpression(StackTypes.Code, "CODE.DEFINE", StackTypes.Name)]
34  public class CodeDefineExpression : DefineExpression<Expression> {
35    public override bool Eval(IPushInterpreter interpreter) {
36      if (interpreter.Configuration.TopLevelPushCode && (interpreter.CodeStack.Count < 2)) return false;
37
38      this.Eval(
39        interpreter.CodeStack,
40        interpreter.NameStack,
41        interpreter.CustomExpressions,
42        v => new ExecPushExpression(v));
43
44      interpreter.CodeStack.Pop();
45
46      return true;
47    }
48  }
49
50  [PushExpression(StackTypes.Exec, "EXEC.DEFINE", StackTypes.Name)]
51  public class ExecDefineExpression : DefineExpression<Expression> {
52    public override bool Eval(IPushInterpreter interpreter) {
53      if (interpreter.ExecStack.Count < 2) return false;
54
55      var noop = this.Eval(
56        interpreter.ExecStack,
57        interpreter.NameStack,
58        interpreter.CustomExpressions,
59        v => new ExecPushExpression(v));
60
61      if (!noop)
62        interpreter.ExecStack.Pop();
63
64      return noop;
65    }
66  }
67
68  [PushExpression(StackTypes.Float, "FLOAT.DEFINE", StackTypes.Name)]
69  public class FloatDefineExpression : DefineExpression<double> {
70
71    public override bool Eval(IPushInterpreter interpreter) {
72      return this.Eval(
73        interpreter.FloatStack,
74        interpreter.NameStack,
75        interpreter.CustomExpressions,
76        v => new FloatPushExpression(v));
77    }
78  }
79
80  [PushExpression(StackTypes.Integer, "INTEGER.DEFINE", StackTypes.Name)]
81  public class IntegerDefineExpression : DefineExpression<long> {
82    public override bool Eval(IPushInterpreter interpreter) {
83      return this.Eval(
84        interpreter.IntegerStack,
85        interpreter.NameStack,
86        interpreter.CustomExpressions,
87        v => new IntegerPushExpression(v));
88    }
89  }
90
91  [PushExpression(StackTypes.Boolean, "BOOLEAN.DEFINE", StackTypes.Name)]
92  public class BooleanDefineExpression : DefineExpression<bool> {
93    public override bool Eval(IPushInterpreter interpreter) {
94      return this.Eval(
95        interpreter.BooleanStack,
96        interpreter.NameStack,
97        interpreter.CustomExpressions,
98        v => new BooleanPushExpression(v));
99    }
100  }
101
102  [PushExpression(StackTypes.Char, "CHAR.DEFINE", StackTypes.Name)]
103  public class CharDefineExpression : DefineExpression<char> {
104    public override bool Eval(IPushInterpreter interpreter) {
105      return Eval(
106        interpreter.CharStack,
107        interpreter.NameStack,
108        interpreter.CustomExpressions,
109        v => new CharPushExpression(v));
110    }
111  }
112
113  [PushExpression(StackTypes.String, "STRING.DEFINE", StackTypes.Name)]
114  public class StringDefineExpression : DefineExpression<string> {
115    public override bool Eval(IPushInterpreter interpreter) {
116      return Eval(
117        interpreter.StringStack,
118        interpreter.NameStack,
119        interpreter.CustomExpressions,
120        v => new StringPushExpression(v));
121    }
122  }
123}
Note: See TracBrowser for help on using the repository browser.