BinaryExpression
using CLanguage.Interpreter;
using CLanguage.Types;
using System;
namespace CLanguage.Syntax
{
public class BinaryExpression : Expression
{
public Expression Left { get; set; }
public Binop Op { get; set; }
public Expression Right { get; set; }
public BinaryExpression(Expression left, Binop op, Expression right)
{
if (left == null)
throw new ArgumentNullException("left");
if (right == null)
throw new ArgumentNullException("right");
Left = left;
Op = op;
Right = right;
}
protected override void DoEmit(EmitContext ec)
{
Expression left = Left;
Expression right = Right;
Binop op = Op;
CBasicType arithmeticType = Expression.GetArithmeticType(left, right, op.ToString(), ec);
Left.Emit(ec);
ec.EmitCast(Left.GetEvaluatedCType(ec), arithmeticType);
Right.Emit(ec);
ec.EmitCast(Right.GetEvaluatedCType(ec), arithmeticType);
int instructionOffset = Expression.GetInstructionOffset(arithmeticType, ec);
op = Op;
switch (op) {
case Binop.Add:
ec.Emit((OpCode)(15 + instructionOffset));
break;
case Binop.Subtract:
ec.Emit((OpCode)(19 + instructionOffset));
break;
case Binop.Multiply:
ec.Emit((OpCode)(23 + instructionOffset));
break;
case Binop.Divide:
ec.Emit((OpCode)(27 + instructionOffset));
break;
case Binop.Mod:
ec.Emit((OpCode)(31 + instructionOffset));
break;
default:
throw new NotSupportedException("Unsupported binary operator '" + Op + "'");
}
}
public override CType GetEvaluatedCType(EmitContext ec)
{
return Expression.GetArithmeticType(Left, Right, Op.ToString(), ec);
}
public override string ToString()
{
return $"""{Left}""{Op}""{Right}""";
}
}
}