Skip to content

Commit

Permalink
fix(iterables): make sure ctx changes are not lost during module expr…
Browse files Browse the repository at this point in the history
…. compilation
  • Loading branch information
kollhof committed Nov 5, 2020
1 parent 0f88339 commit 2d0675d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 26 deletions.
28 changes: 13 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/lang/builtins.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,5 @@ init_builtins = fn ctx:
builtins: rec:
names: []
used: []
provided: []
overrides: {}
defaults: {}
3 changes: 2 additions & 1 deletion src/lang/iterable/map.test.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ describe 'gen 2', fn:
it 'uses multiple custom iterable impl.', fn:
expect
fink2js '
{_map_, _filter_} = import "@fink/js-interop/iter.js"
{_map_} = import "./iter.fnk"
{_filter_} = import "./iter-filter.fnk"
pipe [1, 2, 3]:
map item: item * 2
map item: item / 2
Expand Down
3 changes: 2 additions & 1 deletion src/lang/iterable/map.test.fnk.snap
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ exports[`gen 2 uses legacy iterables 1`] = `
`;

exports[`gen 2 uses multiple custom iterable impl. 1`] = `
"import { _map_, _filter_ } from \\"@fink/js-interop/iter.js\\";
"import { _map_ } from \\"./iter.js\\";
import { _filter_ } from \\"./iter-filter.js\\";
{
let ˆpipe_result_1 = [1, 2, 3];
ˆpipe_result_1 = _map_((item) => item * 2, false, false)(ˆpipe_result_1);
Expand Down
22 changes: 14 additions & 8 deletions src/lang/module/init.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,17 @@ ignorable_import = fn [expr, ctx]:
false


# TODO: std-lib
with_ctx = fn ctx:
map item: [item, ctx]

# TODO: std-lib
map_acc = fn map_fn:
foo = {}
# TODO: should be map [item, initial_acc], acc=initial_acc:
map [item, initial_acc], acc=foo:
ctx = match acc:
foo: initial_acc
else: acc

[result, next_ctx] = map_fn item, ctx
[[result, next_ctx], next_ctx]

Expand All @@ -67,7 +66,6 @@ add_builtin_overrides = fn expr, ctx:
filter {right}: is_builtin right.value, ctx
fold {right: {value: name}}, override_ctx=ctx:
override_builtin name, uri, override_ctx

[expr, next_ctx]
else:
[expr, ctx]
Expand Down Expand Up @@ -169,11 +167,19 @@ transform_module = fn node, ctx:
[interpreter, first] = get_hashbang maybe_shebang_or_pragmas

[body, end_ctx] = pipe [first, ...rest]:
with_ctx ctx
map_acc add_builtin_overrides
drop_if ignorable_import
map_acc transform_body_expr
collect_with_ctx ctx
map expr, expr_ctx=ctx:
[body_expr, body_ctx] = add_builtin_overrides expr, expr_ctx
[js_expr, next_ctx] = match body_expr:
ignorable_import [?, body_ctx]:
[false, body_ctx]
else:
transform_body_expr body_expr, body_ctx
[[js_expr, next_ctx], next_ctx]

drop_if fn [js]: js == false

fold [js, ctx], [jss]=[[]]:
[[...jss, js], ctx]
insert_imports

js = file
Expand Down

0 comments on commit 2d0675d

Please sign in to comment.