макрос forpermutation
От: Аноним  
Дата: 09.04.07 19:45
Оценка:
Пытаюсь написать макрос отсюда:
http://nemerle.org/Macros_tutorial

Add a new syntactic construct forpermutation to your program. It should be defined as the macro
macro forp (i, n : int, m : int, body)

and introduce syntax, which allows writing the following program
mutable i = 0;
forpermutation (i in 3 to 10) Nemerle.IO.printf ("%d\n", i)

It should create a random permutation p of numbers x_j, m <= x_j <= n at the compile-time. Then generate the code executing body of the loop n — m + 1 times, preceding each of them with assignment of permutation element to i.

Написал пока так:

macro @forpermutation (i, n : int, m : int, body)
syntax ("forpermutation", "(", i, "from", n, "to", m, ")", body)
{
    <[
      $i = 0;
//      foreach ($i in [$n..$m]) {
          $body;
//      }
    ]>
}


Для начала хочется сделать просто цикл от n до m,
но в этом и загвоздка, как вытащить значение из n и засунуть скажем в i?
Вот так не работает:

$i = $n;


Пытался изпользовать так:


[Hygienic]
cache(e : Expr) : Expr * Expr
{ | <[ $obj.$mem ]> => (<[ def tmp = $obj ]>, <[ tmp.$mem ]>) 
  | <[ $tab [$idx] ]> => 
    (<[ def (tmp1, tmp2) = ($tab, $idx) ]>, <[ tmp1 [tmp2] ]>)
  | _ => (<[()]>, e)
}


После этого:


def (cached1, safe1) = cache(n);
<[
 $cached1;
 $i = safe1;


Но компилятор ругается на Hygienic
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.