IfStatement
using CLanguage.Interpreter;
namespace CLanguage.Syntax
{
public class IfStatement : Statement
{
public Expression Condition { get; set; }
public Statement TrueStatement { get; set; }
public Statement FalseStatement { get; 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, Statement falseStatement, Location loc)
{
Condition = condition;
TrueStatement = trueStatement;
FalseStatement = falseStatement;
base.Location = loc;
}
public IfStatement(Expression condition, Statement trueStatement, Location loc)
{
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 $"""{Condition}""{TrueStatement}""";
}
}
}