Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/ExpressionCreatorTable.cs @ 14320

Last change on this file since 14320 was 14320, checked in by pkimmesw, 8 years ago

#2665 Added Interpreter, Parser, 16 Examples, Expressions needed for the examples

File size: 4.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using HeuristicLab.Algorithms.PushGP.Expressions;
4
5namespace HeuristicLab.Algorithms.PushGP
6{
7    public static class ExpressionCreatorTable
8    {
9        private static Dictionary<OpCode, Func<Expression>> symbolTable = new Dictionary<OpCode, Func<Expression>>
10        {
11            #region Exec
12            { OpCode.ExecPop, GetExpressionCreator(typeof(ExecPopExpression)) },
13            { OpCode.ExecDup, GetExpressionCreator(typeof(ExecDuplicateExpression)) },
14            { OpCode.ExecDefine, GetExpressionCreator(typeof(ExecDefineExpression)) },
15            { OpCode.ExecDoXRange, GetExpressionCreator(typeof(ExecDoRangeExpression)) },
16            { OpCode.ExecY, GetExpressionCreator(typeof(ExecYExpression)) },
17            { OpCode.ExecIf, GetExpressionCreator(typeof(ExecIfExpression)) },
18            #endregion
19
20            #region Code
21            { OpCode.CodeDup, GetExpressionCreator(typeof(CodeDuplicateExpression)) },
22            { OpCode.CodeDefine, GetExpressionCreator(typeof(CodeDefineExpression)) },
23            { OpCode.CodeQuote, GetExpressionCreator(typeof(CodeQuoteExpression)) },
24            { OpCode.CodeDo, GetExpressionCreator(typeof(CodeDoExpression)) },
25            { OpCode.CodeIf, GetExpressionCreator(typeof(CodeIfExpression)) },
26            #endregion Code
27
28            #region Name
29            { OpCode.NamePop, GetExpressionCreator(typeof(NamePopExpression)) },
30            { OpCode.NameDup, GetExpressionCreator(typeof(NameDuplicateExpression)) },
31            #endregion Name
32
33            #region Boolean
34            { OpCode.BooleanPop, GetExpressionCreator(typeof(BooleanPopExpression)) },
35            { OpCode.BooleanDup, GetExpressionCreator(typeof(BooleanDuplicateExpression)) },
36            { OpCode.BooleanDefine, GetExpressionCreator(typeof(BooleanDefineExpression)) },
37            { OpCode.BooleanOr, GetExpressionCreator(typeof(BooleanOrExpression)) },
38            { OpCode.BooleanAnd, GetExpressionCreator(typeof(BooleanAndExpression)) },
39            #endregion Boolean
40
41            #region Integer
42            { OpCode.IntegerPop, GetExpressionCreator(typeof(IntegerPopExpression)) },
43            { OpCode.IntegerDup, GetExpressionCreator(typeof(IntegerDuplicateExpression)) },
44            { OpCode.IntegerDefine, GetExpressionCreator(typeof(IntegerDefineExpression)) },
45            { OpCode.IntegerMultiply, GetExpressionCreator(typeof(IntegerMultiplyExpression)) },
46            { OpCode.IntegerDivide, GetExpressionCreator(typeof(IntegerDivideExpression)) },
47            { OpCode.IntegerAdd, GetExpressionCreator(typeof(IntegerAddExpression)) },
48            { OpCode.IntegerSubtract, GetExpressionCreator(typeof(IntegerSubtractExpression)) },
49            { OpCode.IntegerMin, GetExpressionCreator(typeof(IntegerMinExpression)) },
50            { OpCode.IntegerMax, GetExpressionCreator(typeof(IntegerMaxExpression)) },
51            { OpCode.IntegerSmallerThan, GetExpressionCreator(typeof(IntegerSmallerThanExpression)) },
52            { OpCode.IntegerGreaterThan, GetExpressionCreator(typeof(IntegerGreaterThanExpression)) },
53            { OpCode.IntegerEquals, GetExpressionCreator(typeof(IntegerEqualsExpression)) },
54            #endregion Integer
55
56            #region Float
57            { OpCode.FloatPop, GetExpressionCreator(typeof(FloatPopExpression)) },
58            { OpCode.FloatDup, GetExpressionCreator(typeof(FloatDuplicateExpression)) },
59            { OpCode.FloatDefine, GetExpressionCreator(typeof(FloatDefineExpression)) },
60            { OpCode.FloatAdd, GetExpressionCreator(typeof(FloatAddExpression)) },
61            { OpCode.FloatMultiply, GetExpressionCreator(typeof(FloatMultiplyExpression)) },
62            { OpCode.FloatDivide, GetExpressionCreator(typeof(FloatDivideExpression)) },
63            { OpCode.FloatSubtract, GetExpressionCreator(typeof(FloatSubtractExpression)) },
64            { OpCode.FloatMin, GetExpressionCreator(typeof(FloatMinExpression)) },
65            { OpCode.FloatMax, GetExpressionCreator(typeof(FloatMaxExpression)) },
66            { OpCode.FloatSmallerThan, GetExpressionCreator(typeof(FloatSmallerThanExpression)) },
67            { OpCode.FloatGreaterThan, GetExpressionCreator(typeof(FloatGreaterThanExpression)) },
68            { OpCode.FloatEquals, GetExpressionCreator(typeof(FloatEqualsExpression)) },
69            #endregion Float
70        };
71
72        private static Func<Expression> GetExpressionCreator(Type type)
73        {
74            return System.Linq.Expressions.Expression.Lambda<Func<Expression>>(
75                       System.Linq.Expressions.Expression.New(type.GetConstructor(Type.EmptyTypes))
76                     ).Compile();
77        }
78
79        public static Func<Expression> GetCreator(OpCode opCode)
80        {
81            Func<Expression> creator;
82            if (symbolTable.TryGetValue(opCode, out creator)) return creator;
83            else throw new NotSupportedException("OpCode: " + opCode);
84        }
85    }
86}
Note: See TracBrowser for help on using the repository browser.