ArrayElementExpression
using CLanguage.Interpreter;
using CLanguage.Types;
using System;
namespace CLanguage.Syntax
{
public class ArrayElementExpression : Expression
{
public Expression Array { get; set; }
public Expression ElementIndex { get; set; }
public ArrayElementExpression(Expression array, Expression elementIndex)
{
Array = array;
ElementIndex = elementIndex;
}
public override CType GetEvaluatedCType(EmitContext ec)
{
CType evaluatedCType = Array.GetEvaluatedCType(ec);
CArrayType cArrayType;
if ((cArrayType = (evaluatedCType as CArrayType)) != null)
return cArrayType.ElementType;
CPointerType cPointerType;
if ((cPointerType = (evaluatedCType as CPointerType)) != null)
return cPointerType.InnerType;
ec.Report.Error(601, "Left hand side of [ must be an array or pointer");
return CType.Void;
}
protected override void DoEmit(EmitContext ec)
{
Array.Emit(ec);
ElementIndex.Emit(ec);
ec.Emit(OpCode.LoadValue, GetEvaluatedCType(ec).GetSize(ec));
ec.Emit(OpCode.MultiplyInt32);
ec.Emit(OpCode.AddInt32);
ec.Emit(OpCode.LoadMemory);
throw new NotImplementedException();
}
public override string ToString()
{
return $"{Array}""{ElementIndex}""";
}
}
}