IfStatement
using CLanguage.Compiler;
using CLanguage.Interpreter;
using System;
using System.Runtime.CompilerServices;
namespace CLanguage.Syntax
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public class IfStatement : Statement
{
public Expression Condition { get; set; }
public Statement TrueStatement { get; set; }
[System.Runtime.CompilerServices.Nullable(2)]
[field: System.Runtime.CompilerServices.Nullable(2)]
public Statement FalseStatement {
[System.Runtime.CompilerServices.NullableContext(2)]
get;
[System.Runtime.CompilerServices.NullableContext(2)]
private set;
}
public override bool AlwaysReturns {
get {
bool alwaysReturns = TrueStatement.AlwaysReturns;
bool flag = FalseStatement != null && FalseStatement.AlwaysReturns;
return alwaysReturns & flag;
}
}
public IfStatement(Expression condition, Statement trueStatement, [System.Runtime.CompilerServices.Nullable(2)] Statement falseStatement, Location loc)
{
if (condition == null)
throw new ArgumentNullException("condition");
if (trueStatement == null)
throw new ArgumentNullException("trueStatement");
Condition = condition;
TrueStatement = trueStatement;
FalseStatement = falseStatement;
base.Location = loc;
}
public IfStatement(Expression condition, Statement trueStatement, Location loc)
{
if (condition == null)
throw new ArgumentNullException("condition");
if (trueStatement == null)
throw new ArgumentNullException("trueStatement");
Condition = condition;
TrueStatement = trueStatement;
FalseStatement = null;
base.Location = loc;
}
protected override void DoEmit(EmitContext ec)
{
Label label = ec.DefineLabel();
Condition.Emit(ec);
ec.EmitCastToBoolean(Condition.GetEvaluatedCType(ec));
if (FalseStatement == null) {
ec.Emit(OpCode.BranchIfFalse, label);
TrueStatement.Emit(ec);
} else {
Label label2 = ec.DefineLabel();
ec.Emit(OpCode.BranchIfFalse, label2);
TrueStatement.Emit(ec);
ec.Emit(OpCode.Jump, label);
ec.EmitLabel(label2);
FalseStatement.Emit(ec);
}
ec.EmitLabel(label);
}
public override string ToString()
{
return string.Format("if ({0}) {1};", new object[2] {
Condition,
TrueStatement
});
}
}
}