- Timestamp:
- 10/11/12 17:48:27 (12 years ago)
- Location:
- trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding
- Property svn:mergeinfo changed
-
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Compiler/SymbolicExpressionTreeCompiler.cs
r7259 r8798 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 } -
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeTerminalNode.cs
r7259 r8798 30 30 public abstract class SymbolicExpressionTreeTerminalNode : SymbolicExpressionTreeNode { 31 31 public override IEnumerable<ISymbolicExpressionTreeNode> Subtrees { 32 get { 33 return Enumerable.Empty<ISymbolicExpressionTreeNode>(); 34 } 32 get { return Enumerable.Empty<ISymbolicExpressionTreeNode>(); } 35 33 } 36 34
Note: See TracChangeset
for help on using the changeset viewer.