www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - parsing again

reply "Saaa" <empty needmail.com> writes:
Is it possible to do a call like
--
arrayLength!(T[])( temp, index[0..depth] );
//temp is (multi)array being created while parsing an (multi)array in a file
//index[] is an array of the current index
//depth = current depth
--
in stead of
--
switch( depth )
{
  case 0:
    if( temp.length < index[depth] ) temp.length = temp.length * 2;
  break;
  static if( is(T A:A[][]))
  {
    case 1:
      if( temp[ index[0] ].length < index[depth] ) temp[index[0]].length = 
temp[index[0]].length * 2;
    break;
  }
  static if( is(T A:A[][][]))
  {
    case 2:
      if( temp[ index[0] ][ index[1] ].length < index[depth] ) temp[ 
index[0] ][ index[1] ].length = temp[ index[0] ][ index[1] ].length * 2;
    break;
  }
default:
assert(false);
break;
}
--
Jun 30 2009
parent reply "Saaa" <empty needmail.com> writes:
My mixin try (doesn't compile though :)
<3 downs

string ctToString(int i)
{
  if (!i) return "0";
  string res;
  while (i) {
    res = "0123456789"[i%10] ~ res;
    i /= 10;
  }
  return res;
}

string index(int len)
{
  string res;
  for (int i = 0; i < len; ++i)
    res ~= "[indices["~ctToString(i)~"]] ";
  return res;
}

string casesString(T)(T array)
{
  string res;
  for (int i = 0; i < Depth!(T); ++i)
  {
    res ~= `case `~ctToString(i)~`:`~
    ` if( temp`~ index(i) ~`.length < index[`~ctToString(i)~`] ) temp`~ 
index(i) ~`.length = temp`~ index(i) ~`.length * 2 ;`
    ~`break;`;
  }
  return res;
}


pragma(msg, casesString!(T)(parsed));

ddata\ddata.d(330): Error: variable parsed is used before initialization
ddata\ddata.d(330): Error: cannot evaluate casesString(parsed) at compile 
time
ddata\ddata.d(330): Error: string expected for message, not 
'casesString(parsed)'



 --
 switch( depth )
 {
  case 0:
    if( temp.length < index[depth] ) temp.length = temp.length * 2;
  break;
  static if( is(T A:A[][]))
  {
    case 1:
      if( temp[ index[0] ].length < index[depth] ) temp[index[0]].length = 
 temp[index[0]].length * 2;
    break;
  }
  static if( is(T A:A[][][]))
  {
    case 2:
      if( temp[ index[0] ][ index[1] ].length < index[depth] ) temp[ 
 index[0] ][ index[1] ].length = temp[ index[0] ][ index[1] ].length * 2;
    break;
  }
 default:
 assert(false);
 break;
 }
 -- 
Jul 01 2009
parent "Saaa" <empty needmail.com> writes:
Kisses Ary:
It seems you can't mixin cases separately, you need to mixin the whole 
switch (bug?)

string CasesString(int depth)
{
  string res;
  res ~= `switch( depth ){`;
  for (int i = 0; i < depth; ++i)
  {
    res ~= `case `~ctToString(i)~`:`~
    ` if( temp`~ indexString(i) ~`.length < index[`~ctToString(i)~`] ) 
temp`~ indexString(i) ~`.length = temp`~ indexString(i) ~`.length * 2 ;`~
    `break;`;
  }
  res ~= `default:assert(false);break;}`;
  return res;
}

mixin ( CasesString(Depth!(T)) ); 
Jul 02 2009