Changeset 9083 for branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Compiler/SymbolicExpressionTreeCompiler.cs
- Timestamp:
- 12/20/12 16:24:54 (12 years ago)
- Location:
- branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding
- Property svn:mergeinfo changed
-
branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Compiler/SymbolicExpressionTreeCompiler.cs
r7439 r9083 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 … … 52 53 var invokeNode = (InvokeFunctionTreeNode)instr.dynamicNode; 53 54 instr.iArg0 = entryPoint[invokeNode.Symbol.FunctionName]; 54 code[i] = instr;55 55 } 56 56 } … … 59 59 } 60 60 61 private IEnumerable<Instruction> Compile(ISymbolicExpressionTreeNode branch, Func<ISymbolicExpressionTreeNode, byte> opCodeMapper) {61 private static IEnumerable<Instruction> Compile(ISymbolicExpressionTreeNode branch, Func<ISymbolicExpressionTreeNode, byte> opCodeMapper, IEnumerable<Func<Instruction, Instruction>> postInstructionCompiledHooks) { 62 62 foreach (var node in branch.IterateNodesPrefix()) { 63 63 Instruction instr = new Instruction(); … … 77 77 } 78 78 } 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 79 } 89 80 }
Note: See TracChangeset
for help on using the changeset viewer.