Changeset 7989 for branches/HeuristicLab.TimeSeries/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding
- Timestamp:
- 06/12/12 10:31:56 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.TimeSeries/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Compiler/SymbolicExpressionTreeCompiler.cs
r7268 r7989 25 25 26 26 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { 27 public class SymbolicExpressionTreeCompiler { 28 private Dictionary<string, ushort> entryPoint = new Dictionary<string, ushort>(); 29 private List<Func<Instruction, Instruction>> postInstructionCompiledHooks = new List<Func<Instruction, Instruction>>(); 27 public static class SymbolicExpressionTreeCompiler { 30 28 31 public Instruction[] Compile(ISymbolicExpressionTree tree, Func<ISymbolicExpressionTreeNode, byte> opCodeMapper) { 29 public static Instruction[] Compile(ISymbolicExpressionTree tree, Func<ISymbolicExpressionTreeNode, byte> opCodeMapper) { 30 return Compile(tree, opCodeMapper, Enumerable.Empty<Func<Instruction, Instruction>>()); 31 } 32 public static Instruction[] Compile(ISymbolicExpressionTree tree, Func<ISymbolicExpressionTreeNode, byte> opCodeMapper, IEnumerable<Func<Instruction, Instruction>> postInstructionCompiledHooks) { 33 Dictionary<string, ushort> entryPoint = new Dictionary<string, ushort>(); 32 34 List<Instruction> code = new List<Instruction>(); 33 entryPoint.Clear();34 35 // compile main body branches 35 36 foreach (var branch in tree.Root.GetSubtree(0).Subtrees) { 36 code.AddRange(Compile(branch, opCodeMapper ));37 code.AddRange(Compile(branch, opCodeMapper, postInstructionCompiledHooks)); 37 38 } 38 39 // compile function branches … … 43 44 if (code.Count > ushort.MaxValue) throw new ArgumentException("Code for the tree is too long (> ushort.MaxValue)."); 44 45 entryPoint[branch.FunctionName] = (ushort)code.Count; 45 code.AddRange(Compile(branch.GetSubtree(0), opCodeMapper ));46 code.AddRange(Compile(branch.GetSubtree(0), opCodeMapper, postInstructionCompiledHooks)); 46 47 } 47 48 // address of all functions is fixed now … … 59 60 } 60 61 61 private IEnumerable<Instruction> Compile(ISymbolicExpressionTreeNode branch, Func<ISymbolicExpressionTreeNode, byte> opCodeMapper) {62 private static IEnumerable<Instruction> Compile(ISymbolicExpressionTreeNode branch, Func<ISymbolicExpressionTreeNode, byte> opCodeMapper, IEnumerable<Func<Instruction, Instruction>> postInstructionCompiledHooks) { 62 63 foreach (var node in branch.IterateNodesPrefix()) { 63 64 Instruction instr = new Instruction(); … … 77 78 } 78 79 } 79 80 /// <summary>81 /// Adds a function that will be called every time an instruction is compiled.82 /// The compiled will insert the instruction returned by the hook into the code.83 /// </summary>84 /// <param name="hook">The hook that should be called for each compiled instruction.</param>85 public void AddInstructionPostProcessingHook(Func<Instruction, Instruction> hook) {86 postInstructionCompiledHooks.Add(hook);87 }88 80 } 89 81 }
Note: See TracChangeset
for help on using the changeset viewer.