Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Templates/DefineExpressionTemplate.cs @ 14328

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

#2665 Set .NET version to 4.5, C# version to 5.0, Added expression templates and factory

File size: 1.5 KB
Line 
1using System;
2using HeuristicLab.Algorithms.PushGP.Interpreter;
3using HeuristicLab.Algorithms.PushGP.Stack;
4
5namespace HeuristicLab.Algorithms.PushGP.Expressions.Templates
6{
7    public class DefineExpressionTemplate<T> : ExpressionTemplate
8    {
9        private readonly Func<IInterpreter, IStack<T>> sourceStackProvider;
10        private readonly Func<T, Expression> creator;
11        private readonly bool isCodeOp;
12        public DefineExpressionTemplate(string symbol, Func<IInterpreter, IStack<T>> sourceStackProvider, Func<T, Expression> creator, bool isCodeOp = false) : base(symbol)
13        {
14            this.sourceStackProvider = sourceStackProvider;
15            this.creator = creator;
16            this.isCodeOp = isCodeOp;
17        }
18
19        public override bool IsCodeOp { get { return this.isCodeOp; } }
20
21        public override void Eval(IInterpreter interpreter)
22        {
23            var sourceStack = this.sourceStackProvider(interpreter);
24
25            // not enough arguments on stack
26            if (interpreter.NameStack.Count == 0 ||
27                sourceStack.Count == 0)
28                return;
29
30            var name = interpreter.NameStack.Pop();
31            var expression = creator(sourceStack.Top);
32
33            if (interpreter.CustomExpressions.ContainsKey(name))
34            {
35                interpreter.CustomExpressions[name] = expression;
36            }
37            else
38            {
39                interpreter.CustomExpressions.Add(name, expression);
40            }
41        }
42    }
43}
Note: See TracBrowser for help on using the repository browser.