07/07/25 09:23:20
話がズレ始めてるんで一応書いておきますが、
特別な事をしなくとも
#define letend }}
#define let1(type, var, init) \
{ type let_tmp = init; \
{ type var = let_tmp;
#define let2(type, var, init, type2, var2, init2) \
{ type let_tmp = init; type2 let_tmp2 = init2; \
{ type var = let_tmp; type2 var2 = let_tmp2;
実はこれだけでschemeのlet束縛を再現できます。(>>72おしい)
int f(int n) { return n + 1; }
int g(int x)
let2(int, x, f(x), int, y, x)
printf("x=%d, y=%d\n", x, y);
let1(int, x, f(x))
let1(int, x, f(x))
return x;
letend
letend
letend
ここでyの初期化式として使われるxはgの引数のままであり、
f(x)の影響を受けません。g(1)を実行するとx=2, y=1と
表示されることから、Schemeのletと同じ動作だと確認できます。