view dmd/expression/ArrayLength.d @ 0:10317f0c89a5

Initial commit
author korDen
date Sat, 24 Oct 2009 08:42:06 +0400
parents
children e28b18c23469
line wrap: on
line source

module dmd.expression.ArrayLength;

import dmd.Type;
import dmd.Expression;
import dmd.StringExp;
import dmd.IntegerExp;
import dmd.ArrayLiteralExp;
import dmd.Loc;
import dmd.TOK;
import dmd.AssocArrayLiteralExp;
import dmd.GlobalExpressions;

Expression ArrayLength(Type type, Expression e1)
{
	Expression e;
    Loc loc = e1.loc;

    if (e1.op == TOKstring)
    {	
		StringExp es1 = cast(StringExp)e1;
		e = new IntegerExp(loc, es1.len, type);
    }
    else if (e1.op == TOKarrayliteral)
    {
		ArrayLiteralExp ale = cast(ArrayLiteralExp)e1;
		size_t dim = ale.elements ? ale.elements.dim : 0;
		e = new IntegerExp(loc, dim, type);
    }
    else if (e1.op == TOKassocarrayliteral)
    {	
		AssocArrayLiteralExp ale = cast(AssocArrayLiteralExp)e1;
		size_t dim = ale.keys.dim;
		e = new IntegerExp(loc, dim, type);
    }
    else
		e = EXP_CANT_INTERPRET;

    return e;
}