WhileStatement
using CLanguage.Interpreter;
using System;
namespace CLanguage.Syntax
{
public class WhileStatement : Statement
{
public bool IsDo { get; set; }
public Expression Condition { get; set; }
public Block Loop { get; set; }
public override bool AlwaysReturns => false;
public WhileStatement(bool isDo, Expression condition, Block loop)
{
IsDo = isDo;
Condition = condition;
Loop = loop;
}
protected override void DoEmit(EmitContext ec)
{
if (IsDo)
throw new NotImplementedException();
Label label = ec.DefineLabel();
Label l = ec.DefineLabel();
Label label2 = ec.DefineLabel();
ec.EmitLabel(label);
Condition.Emit(ec);
ec.EmitCastToBoolean(Condition.GetEvaluatedCType(ec));
ec.Emit(OpCode.BranchIfFalse, label2);
ec.EmitLabel(l);
Loop.Emit(ec);
ec.Emit(OpCode.Jump, label);
ec.EmitLabel(label2);
}
public override string ToString()
{
if (IsDo)
return string.Format("do {1} while({0});", Condition, Loop);
return $"""{Condition}""{Loop}""";
}
}
}