Skip to content

Commit

Permalink
Simplify the resolution of free names in syntactic_closure
Browse files Browse the repository at this point in the history
Signed-off-by: yamacir-kit <[email protected]>
  • Loading branch information
yamacir-kit committed Aug 5, 2024
1 parent 463fce9 commit c6badae
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 47 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ Procedures for each standard are provided by the following R7RS-style libraries:
cmake -B build -DCMAKE_BUILD_TYPE=Release
cd build
make package
sudo apt install build/meevax_0.5.215_amd64.deb
sudo apt install build/meevax_0.5.216_amd64.deb
```

or
Expand Down Expand Up @@ -122,9 +122,9 @@ sudo rm -rf /usr/local/share/meevax

| Target Name | Description
|-------------|-------------
| `all` | Build shared-library `libmeevax.0.5.215.so` and executable `meevax`
| `all` | Build shared-library `libmeevax.0.5.216.so` and executable `meevax`
| `test` | Test executable `meevax`
| `package` | Generate debian package `meevax_0.5.215_amd64.deb`
| `package` | Generate debian package `meevax_0.5.216_amd64.deb`
| `install` | Copy files into `/usr/local` directly

## Usage
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.5.215
0.5.216
81 changes: 38 additions & 43 deletions include/meevax/kernel/syntactic_environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,43 +32,57 @@ inline namespace kernel
{
struct syntactic_closure : public identifier
{
let macro_environment, free_names, expression;
let inner_environment, free_names, expression;

explicit syntactic_closure(let const& macro_environment,
explicit syntactic_closure(let const& inner_environment,
let const& free_names,
let const& expression)
: macro_environment { macro_environment }
: inner_environment { inner_environment }
, free_names { free_names }
, expression { expression }
{
assert(macro_environment.is<syntactic_environment>());
assert(inner_environment.is<syntactic_environment>());
}

template <typename... Ts>
auto compile(let const& use_environment, Ts&&... xs)
auto compile(let const& outer_environment, Ts&&... xs)
{
assert(use_environment.is<syntactic_environment>());
assert(outer_environment.is<syntactic_environment>());

let const bound_variables = unify(car(macro_environment),
car(use_environment));
let const bound_variables = unify(car(inner_environment),
car(outer_environment));

let const free_variables = map([&](let const& free_name)
{
return cons(free_name, use_environment);
},
free_names,
cdr(use_environment));
let const free_variables = unit;

return macro_environment.as<syntactic_environment>()
.generate(macro_environment.as<syntactic_environment>()
.expand(expression,
return inner_environment.as<syntactic_environment>()
.generate(inner_environment.as<syntactic_environment>()
.expand(inject(outer_environment, free_names, expression),
bound_variables,
free_variables),
bound_variables,
free_variables,
std::forward<decltype(xs)>(xs)...);
}

static auto inject(let const& outer_environment,
let const& free_names,
let const& form) -> object
{
if (form.is<pair>())
{
return cons(inject(outer_environment, free_names, car(form)),
inject(outer_environment, free_names, cdr(form)));
}
else if (let const& x = memq(form, free_names); x != f)
{
return make<syntactic_closure>(outer_environment, unit, form);
}
else
{
return form;
}
}

friend auto operator ==(syntactic_closure const& x, syntactic_closure const& y) -> bool
{
/*
Expand All @@ -82,13 +96,13 @@ inline namespace kernel
*/
return x.expression.template is_also<identifier>() and
y.expression.template is_also<identifier>() and
eqv(x.macro_environment.template as<syntactic_environment>()
eqv(x.inner_environment.template as<syntactic_environment>()
.identify(x.expression,
car(x.macro_environment),
car(x.inner_environment),
nullptr),
y.macro_environment.template as<syntactic_environment>()
y.inner_environment.template as<syntactic_environment>()
.identify(y.expression,
car(y.macro_environment),
car(y.inner_environment),
nullptr));
}

Expand Down Expand Up @@ -952,16 +966,6 @@ inline namespace kernel
{
return f;
}
else if (let const& x = assq(variable, free_variables); x != f)
{
/*
If a macro transformer inserts a free reference to an identifier,
the reference refers to the binding that was visible where the
transformer was specified, regardless of any local bindings that
surround the use of the macro.
*/
return cdr(x).as<syntactic_environment>().inject(car(x), bound_variables);
}
else
{
auto i = 0;
Expand Down Expand Up @@ -990,13 +994,13 @@ inline namespace kernel
}
}

if (variable.is<syntactic_closure>())
if (variable.is<syntactic_closure>()) // Resolve alias
{
return variable.as<syntactic_closure>()
.macro_environment
.inner_environment
.template as<syntactic_environment>()
.identify(variable.as<syntactic_closure>().expression,
unify(car(variable.as<syntactic_closure>().macro_environment),
unify(car(variable.as<syntactic_closure>().inner_environment),
bound_variables),
nullptr);
}
Expand Down Expand Up @@ -1042,15 +1046,6 @@ inline namespace kernel
}
}

inline auto inject(object const& free_variable,
object const& bound_variables) -> object
{
return identify(free_variable,
unify(first /* bound-variables */,
bound_variables),
second /* free-variables */);
}

static auto rename(std::string const& variable)
{
return make<syntactic_closure>(core(), unit, make_symbol(variable));
Expand Down

0 comments on commit c6badae

Please sign in to comment.