LogicExpression
using CLanguage.Compiler;
using CLanguage.Interpreter;
using CLanguage.Types;
using System;
using System.Runtime.CompilerServices;
namespace CLanguage.Syntax
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public class LogicExpression : Expression
{
public Expression Left { get; set; }
public LogicOp Op { get; set; }
public Expression Right { get; set; }
public LogicExpression(Expression left, LogicOp op, Expression right)
{
Left = left;
Op = op;
Right = right;
}
protected override void DoEmit(EmitContext ec)
{
Left.Emit(ec);
ec.EmitCastToBoolean(Left.GetEvaluatedCType(ec));
Right.Emit(ec);
ec.EmitCastToBoolean(Right.GetEvaluatedCType(ec));
switch (Op) {
case LogicOp.And:
ec.Emit(OpCode.LogicalAnd);
break;
case LogicOp.Or:
ec.Emit(OpCode.LogicalOr);
break;
default:
throw new NotSupportedException("Unsupported logical operator '" + Op.ToString() + "'");
}
}
public override CType GetEvaluatedCType(EmitContext ec)
{
return CBasicType.Bool;
}
public override string ToString()
{
return string.Format("({0} {1} {2})", new object[3] {
Left,
Op,
Right
});
}
}
}