Hello!
I'm writing a C# compiler in C# for fun and I'm having some trouble with the && operator as to how it should be implemented. I'm using Red Gate's free .NET Reflector to check the MSIL my code generates. That tool can also decompile MSIL into C# but it fails to do so correctly with my && operator.
Here's my code to generate the && operator:
[EDIT] Sorry, I had posted the code of the || op.
Label endLabel = IlGenerator.DefineLabel();
LeftOperand.Emit(IlGenerator); // Pushes the (boolean) value of the operand
IlGenerator.Emit(OpCodes.Dup);
IlGenerator.Emit(OpCodes.Brfalse_S, endLabel);
IlGenerator.Emit(OpCodes.Pop);
RightOperand.Emit(IlGenerator); // Pushes the (boolean) value of the operand
IlGenerator.MarkLabel(endLabel);
In my compilation test case, I have the following code:
void Test()
{
if (true && false) debugprint @"Hello World!"; // "debugprint" is a temporary keyword that calls Console.WriteLine until I implement method call parsing =)
}
The generated MSIL, as seen in the .NET Reflector, is:
.method public hidebysig static void Test() cil managed
{
.maxstack 2
L_0000: ldc.i4.1
L_0001: dup
L_0002: brfalse.s L_0006
L_0004: pop
L_0005: ldc.i4.0
L_0006: brfalse.s L_0017
L_0008: ldstr "Hello World!"
L_000d: call void [mscorlib]System.Console::WriteLine(string)
L_0012: br L_0017
L_0017: ret
}
But the decompiled C# appears as:
public static void Test()
{
// This item is obfuscated and can not be translated.
if (1 == 0)
{
goto Label_0006;
}
if (0 != 0)
{
Console.WriteLine("Hello World!");
}
}
I suspect I'm not implementing the && operator the usual way, any ideas on what I'm doing wrong?
Thanks!
[Edited by - Trillian on October 13, 2008 11:11:02 AM]