Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/Templates/DuplicateExpressionTemplate.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.0 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 DuplicateExpressionTemplate<T> : ExpressionTemplate
8    {
9        private readonly Func<IInterpreter, IStack<T>> sourceStackProvider;
10        private readonly bool isCodeOp;
11        public DuplicateExpressionTemplate(string symbol, Func<IInterpreter, IStack<T>> sourceStackProvider, bool isCodeOp = false) : base(symbol)
12        {
13            this.sourceStackProvider = sourceStackProvider;
14            this.isCodeOp = isCodeOp;
15        }
16
17        public override bool IsCodeOp { get { return this.isCodeOp; } }
18
19        public override void Eval(IInterpreter interpreter)
20        {
21            var sourceStack = this.sourceStackProvider(interpreter);
22
23            // not enough arguments on stack
24            if (sourceStack.Count == 0)
25                return;
26
27            sourceStack.Push(sourceStack.Top);
28        }
29    }
30}
Note: See TracBrowser for help on using the repository browser.