public override void Compile(CodeContext c) { Label looplabel = c.IL.DefineLabel(); Label elselabel = c.IL.DefineLabel(); Label endlabel = c.IL.DefineLabel(); LocalBuilder enumerator = c.IL.DeclareLocal(typeof(IEnumerator)); Type collectiontype = collection.Evaluate(c); if (collectiontype == null) { throw new ApplicationException(); } if (!typeof(IEnumerable).IsAssignableFrom(collectiontype)) { throw new ApplicationException(); } Type valuetype; if (!c.Types.TryGetValue(variabletype, out valuetype)) { throw new ApplicationException(); } LocalBuilder value = c.IL.DeclareLocal(valuetype); CodeContext c2 = new CodeContext(c); c2.Locals.Add(variablename, value); if (collectiontype.IsValueType) { c.IL.Emit(OpCodes.Constrained, typeof(IEnumerable)); } c.IL.Emit(OpCodes.Callvirt, typeof(IEnumerable).GetMethod("GetEnumerator")); c.IL.Emit(OpCodes.Stloc, enumerator); c.IL.MarkLabel(looplabel); c.IL.Emit(OpCodes.Ldloc, enumerator); c.IL.Emit(OpCodes.Callvirt, typeof(IEnumerator).GetMethod("MoveNext")); c.IL.Emit(OpCodes.Brfalse, elselabel); c.IL.Emit(OpCodes.Ldloc, enumerator); c.IL.Emit(OpCodes.Callvirt, typeof(IEnumerator).GetProperty("Current").GetGetMethod()); c.IL.Emit(OpCodes.Isinst, valuetype); c.IL.Emit(OpCodes.Dup); c.IL.Emit(OpCodes.Stloc, value); c.IL.Emit(OpCodes.Brfalse, looplabel); loopstatement.SubCompile(c2); c.IL.Emit(OpCodes.Br, looplabel); c.IL.MarkLabel(elselabel); elsestatement.SubCompile(c); c.IL.MarkLabel(endlabel); }
public override void Compile(CodeContext c) { Label elselabel = c.IL.DefineLabel(); Label endlabel = c.IL.DefineLabel(); Type type = condition.Evaluate(c); if (type != typeof(bool)) { throw new ApplicationException(); } c.IL.Emit(OpCodes.Brfalse, elselabel); thenstatement.SubCompile(c); c.IL.Emit(OpCodes.Br, endlabel); c.IL.MarkLabel(elselabel); elsestatement.SubCompile(c); c.IL.MarkLabel(endlabel); }