using System; using System.Collections.Generic; using System.Reflection.Emit; using System.Text; using Sunweaver.Commands.Abstracts; namespace Sunweaver.DataPrototypes { public class Codule { public List BasePairs; private delegate void ExecuteDelegate(VirtualMachine VM); private ExecuteDelegate Run; public Codule(List dna) { BasePairs = dna; Run = Interpret; } public Codule() { BasePairs = new List(); Run = Interpret; } public void Execute(VirtualMachine VM) { Run(VM); } public void Interpret(VirtualMachine VM) { foreach(BasePair bp in BasePairs) { //TODO: Move these to the bp.Implementation if (bp.ToString() == "stop") VM.Executing = false; else if (bp.ToString() == "start") VM.Executing = true; if(VM.Executing) bp.Implementation(VM); } } public void Compile() { var method = new DynamicMethod(string.Empty, typeof(void), new Type[] { typeof(Codule), typeof(VirtualMachine) }, typeof(Codule).Module); ILGenerator il = method.GetILGenerator(); Locals locals = new Locals(il); il.Emit(OpCodes.Ldarg_1); il.Emit(OpCodes.Stloc, locals[typeof(VirtualMachine)]); Compile(il, locals); il.Emit(OpCodes.Ret); Run = (ExecuteDelegate) method.CreateDelegate(typeof(ExecuteDelegate), this); } internal void Compile(ILGenerator il, Locals locals) { //TODO: Find a better way to do this bool executing = true; foreach (BasePair bp in BasePairs) { if (bp.ToString() == "stop") executing = false; else if (bp.ToString() == "start") executing = true; if (executing) bp.Compile(il, locals); } } public override bool Equals(object obj) { if (!obj.GetType().Equals(this.GetType())) { return false; } Codule c = (Codule)obj; if (this.BasePairs.Count != c.BasePairs.Count) { return false; } for (int i = 0; i < this.BasePairs.Count; i++) { if(!this.BasePairs[i].ToString().Equals(c.BasePairs[i].ToString())){ return false; } } return true; } public override int GetHashCode() { return base.GetHashCode(); } } }