1 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
|
---|
2 | using System;
|
---|
3 | using System.Collections.Generic;
|
---|
4 |
|
---|
5 | using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
|
---|
6 | using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
|
---|
7 | using HeuristicLab.Problems.ProgramSynthesis.Push.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 void 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;
|
---|
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 | }
|
---|
30 |
|
---|
31 | [PushExpression(StackType.Code, "CODE.DEFINE")]
|
---|
32 | public class CodeDefineExpression : DefineExpression<Expression> {
|
---|
33 | public override void Eval(IPushInterpreter interpreter) {
|
---|
34 | if (interpreter.Configuration.TopLevelPushCode && (interpreter.CodeStack.Count < 2)) return;
|
---|
35 |
|
---|
36 | this.Eval(
|
---|
37 | interpreter.CodeStack,
|
---|
38 | interpreter.NameStack,
|
---|
39 | interpreter.CustomExpressions,
|
---|
40 | v => new ExecPushExpression(v));
|
---|
41 |
|
---|
42 | interpreter.CodeStack.Pop();
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | [PushExpression(StackType.Exec, "EXEC.DEFINE")]
|
---|
47 | public class ExecDefineExpression : DefineExpression<Expression> {
|
---|
48 | public override void Eval(IPushInterpreter interpreter) {
|
---|
49 | if (interpreter.ExecStack.Count < 2) return;
|
---|
50 |
|
---|
51 | this.Eval(
|
---|
52 | interpreter.ExecStack,
|
---|
53 | interpreter.NameStack,
|
---|
54 | interpreter.CustomExpressions,
|
---|
55 | v => new ExecPushExpression(v));
|
---|
56 |
|
---|
57 | interpreter.ExecStack.Pop();
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | [PushExpression(StackType.Float, "FLOAT.DEFINE")]
|
---|
62 | public class FloatDefineExpression : DefineExpression<double> {
|
---|
63 |
|
---|
64 | public override void Eval(IPushInterpreter interpreter) {
|
---|
65 | this.Eval(
|
---|
66 | interpreter.FloatStack,
|
---|
67 | interpreter.NameStack,
|
---|
68 | interpreter.CustomExpressions,
|
---|
69 | v => new FloatPushExpression(v));
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | [PushExpression(StackType.Integer, "INTEGER.DEFINE")]
|
---|
74 | public class IntegerDefineExpression : DefineExpression<long> {
|
---|
75 | public override void Eval(IPushInterpreter interpreter) {
|
---|
76 | this.Eval(
|
---|
77 | interpreter.IntegerStack,
|
---|
78 | interpreter.NameStack,
|
---|
79 | interpreter.CustomExpressions,
|
---|
80 | v => new IntegerPushExpression(v));
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | [PushExpression(StackType.Boolean, "BOOLEAN.DEFINE")]
|
---|
85 | public class BooleanDefineExpression : DefineExpression<bool> {
|
---|
86 | public override void Eval(IPushInterpreter interpreter) {
|
---|
87 | this.Eval(
|
---|
88 | interpreter.BooleanStack,
|
---|
89 | interpreter.NameStack,
|
---|
90 | interpreter.CustomExpressions,
|
---|
91 | v => new BooleanPushExpression(v));
|
---|
92 | }
|
---|
93 | }
|
---|
94 | } |
---|