@@ -39,6 +39,8 @@ inline namespace kernel
39
39
machine ()
40
40
{}
41
41
42
+ IMPORT (environment, fork, const );
43
+
42
44
protected:
43
45
let s, // stack (holding intermediate results and return address)
44
46
e, // environment (giving values to symbols)
@@ -113,12 +115,31 @@ inline namespace kernel
113
115
let const & expression)
114
116
: syntactic_environment { syntactic_environment }
115
117
, expression { expression }
116
- , identity { identify () }
118
+ , identity { syntactic_environment. as <environment>(). identify (expression, syntactic_environment. as <environment>(). scope () ) }
117
119
{}
118
120
119
- auto identify ( )
121
+ auto identify_with_offset (const_reference use_env_scope )
120
122
{
121
- return syntactic_environment.as <environment>().identify (expression, syntactic_environment.as <environment>().scope ());
123
+ if (identity.is <relative>())
124
+ {
125
+ let const & mac_env_scope = syntactic_environment.as <environment>().scope ();
126
+ auto offset = make<exact_integer>(length (use_env_scope) - length (mac_env_scope));
127
+ return make<relative>(car (identity),
128
+ cadr (identity).template as <exact_integer>() + offset,
129
+ cddr (identity));
130
+ }
131
+ else if (identity.is <variadic>())
132
+ {
133
+ let const & mac_env_scope = syntactic_environment.as <environment>().scope ();
134
+ auto offset = make<exact_integer>(length (use_env_scope) - length (mac_env_scope));
135
+ return make<variadic>(car (identity),
136
+ cadr (identity).template as <exact_integer>() + offset,
137
+ cddr (identity));
138
+ }
139
+ else
140
+ {
141
+ return identity;
142
+ }
122
143
}
123
144
124
145
friend auto operator ==(syntactic_closure const & x, syntactic_closure const & y) -> bool
@@ -197,12 +218,21 @@ inline namespace kernel
197
218
return cons (id.as <identity>().make_load_instruction (), id,
198
219
current_continuation);
199
220
}
200
- else
221
+ else // The syntactic-closure encloses procedure call.
201
222
{
223
+ let mac_env_scope = current_expression.as <syntactic_closure>().syntactic_environment .template as <environment>().scope ();
224
+
225
+ auto offset = length (current_scope) - length (mac_env_scope);
226
+
227
+ for (auto i = 0 ; i < offset; ++i)
228
+ {
229
+ mac_env_scope = cons (unit, mac_env_scope);
230
+ }
231
+
202
232
return compile (current_context,
203
233
current_expression.as <syntactic_closure>().syntactic_environment .template as <environment>(),
204
234
current_expression.as <syntactic_closure>().expression ,
205
- current_expression. as <syntactic_closure>(). syntactic_environment . template as <environment>(). scope () ,
235
+ mac_env_scope ,
206
236
current_continuation);
207
237
}
208
238
}
@@ -421,7 +451,7 @@ inline namespace kernel
421
451
*
422
452
* ------------------------------------------------------------------- */
423
453
assert (car (s).template is <closure>());
424
- cadr (c).template as <absolute>().load () = make<transformer>(car (s), static_cast <environment const &>(* this ). fork ());
454
+ cadr (c).template as <absolute>().load () = make<transformer>(car (s), fork ());
425
455
c = cddr (c);
426
456
goto decode;
427
457
@@ -438,10 +468,12 @@ inline namespace kernel
438
468
439
469
let const & f = environment (static_cast <environment const &>(*this )).execute (binding);
440
470
441
- binding = make<transformer>(f, static_cast <environment const &>(* this ). fork (cadr (c).template as <syntactic_continuation>().scope ()));
471
+ binding = make<transformer>(f, fork (cdr ( cadr (c).template as <syntactic_continuation>().scope () )));
442
472
}
443
473
}();
444
474
475
+ e = cons (unit, e); // dummy environment
476
+
445
477
std::swap (c.as <pair>(),
446
478
body (context::none,
447
479
static_cast <environment &>(*this ),
@@ -459,22 +491,21 @@ inline namespace kernel
459
491
* ------------------------------------------------------------------- */
460
492
[&]() // DIRTY HACK!!!
461
493
{
462
- let const syntactic_environment
463
- = static_cast <environment const &>(*this ).fork (cadr (c).template as <syntactic_continuation>().scope ());
494
+ let const expander = fork (cadr (c).template as <syntactic_continuation>().scope ());
464
495
465
496
auto const [transformer_specs, body] = unpair (cadr (c).template as <syntactic_continuation>().expression ());
466
497
467
498
for (let const & transformer_spec : transformer_specs)
468
499
{
469
- syntactic_environment .as <environment>().execute (compile (context::outermost,
470
- syntactic_environment .as <environment>(),
471
- cons (make<syntax>(" define-syntax" , define_syntax), transformer_spec),
472
- cadr (c).template as <syntactic_continuation>().scope ()));
500
+ expander .as <environment>().execute (compile (context::outermost,
501
+ expander .as <environment>(),
502
+ cons (make<syntax>(" define-syntax" , define_syntax), transformer_spec),
503
+ cadr (c).template as <syntactic_continuation>().scope ()));
473
504
}
474
505
475
506
std::swap (c.as <pair>(),
476
507
machine::body (context::outermost,
477
- syntactic_environment .as <environment>(),
508
+ expander .as <environment>(),
478
509
body,
479
510
cadr (c).template as <syntactic_continuation>().scope (),
480
511
cddr (c)
@@ -686,7 +717,7 @@ inline namespace kernel
686
717
}
687
718
}
688
719
689
- return variable.is <syntactic_closure>() ? variable.as <syntactic_closure>().identify ( ) : f;
720
+ return variable.is <syntactic_closure>() ? variable.as <syntactic_closure>().identify_with_offset (scope ) : f;
690
721
}
691
722
692
723
inline auto reset () -> void
0 commit comments