Skip to content

Commit

Permalink
feat(runtime): use @fink/js-interop/runtime.js by default for iterables
Browse files Browse the repository at this point in the history
BREAKING CHANGE: requires @fink/js-interop as peer dep for iterables
BREAKING CHANGE: async iterables must use await afterthe block name e.g. `map await item, ...:`  This allows for easier destructuring.
  • Loading branch information
kollhof committed Nov 8, 2020
1 parent 961f47c commit b418292
Show file tree
Hide file tree
Showing 25 changed files with 452 additions and 338 deletions.
28 changes: 16 additions & 12 deletions package-lock.json

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

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
"npx-run": "^2.1.2",
"semantic-release": "^17.2.1"
},
"peerDependencies": {
"@fink/js-interop": ">2.1"
},
"dependencies": {
"@babel/core": "^7.10.5",
"@babel/traverse": "^7.10.5",
Expand Down
64 changes: 15 additions & 49 deletions src/lang/async/init.test.fnk.snap
Original file line number Diff line number Diff line change
@@ -1,73 +1,39 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`await compiles 1`] = `
"export const task1 = async (foo) => -(await foo);
"import { _unfold_ } from \\"@fink/js-interop/runtime.js\\";
export const task1 = async (foo) => -(await foo);
export const task2 = async (foo) => await (foo + 4);
export const task3 = async (foo) => {
const bar = await foo();
const spam = await foo();
return bar + 123;
};
export const a_gen = function unfold(ˆinitial_5) {
return {
async *[Symbol.asyncIterator]() {
let ˆitem_1 = ˆinitial_5;
export const a_gen = _unfold_(async (curr = 0) => {
const ˆvalue_1 = shrub;
while (true) {
const curr = ˆitem_1 === undefined ? 0 : ˆitem_1;
let _do_result;
ˆmatch_4: {
const ˆvalue_3 = shrub;
ifvalue_3 === spam) {
_do_result = await ni(curr);
break ˆmatch_4;
}
{
_do_result = curr + 1;
break ˆmatch_4;
}
}
const ˆresult_2 = _do_result;
_do_result = undefined;
yield ˆresult_2;
ˆitem_1 = ˆresult_2;
}
}
ifvalue_1 === spam) {
return await ni(curr);
}
};
};
{
return curr + 1;
}
}, 1, false, false);
await ni;"
`;

exports[`await handles awaiting async iterables 1`] = `
"let _do_result;
"import { _unfold_ } from \\"@fink/js-interop/runtime.js\\";
let _do_result;
{
let _do_result2;
{
let ˆpipe_result_3 = undefined;
_do_result2 = ˆpipe_result_3 = function unfold(ˆinitial_6) {
return {
async *[Symbol.asyncIterator]() {
let ˆitem_4 = ˆinitial_6;
while (true) {
const cntr = ˆitem_4 === undefined ? 0 : ˆitem_4;
const ˆresult_5 = (await cntr) + 1;
yield ˆresult_5;
ˆitem_4 = ˆresult_5;
}
}
};
}(ˆpipe_result_3);
_do_result2 = ˆpipe_result_3 = _unfold_(async (cntr = 0) => (await cntr) + 1, 1, false, false)(ˆpipe_result_3);
}
const ˆitems_1 = _do_result2;
_do_result2 = undefined;
Expand Down
17 changes: 3 additions & 14 deletions src/lang/call/pipe.test.fnk.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`pipe compiles 1`] = `
"{
"import { _map_ } from \\"@fink/js-interop/runtime.js\\";
{
let ˆpipe_result_1 = undefined;
ˆpipe_result_1 = foopipe_result_1);
Expand All @@ -16,19 +17,7 @@ exports[`pipe compiles 1`] = `
}
{
let ˆpipe_result_2 = [1, 2, 3];
ˆpipe_result_2 = function map(ˆitems_5) {
return {
*[Symbol.iterator]() {
for (const ˆitem_3 of ˆitems_5) {
const item = ˆitem_3;
const ˆresult_4 = item * 2;
yield ˆresult_4;
}
}
};
}(ˆpipe_result_2);
ˆpipe_result_2 = _map_((item) => item * 2, 1, false, false)(ˆpipe_result_2);
}"
`;

Expand Down
2 changes: 1 addition & 1 deletion src/lang/context.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ add = fn type, op, transformer: fn {transformers, ...ctx}:



add_with_builtin = fn type, op, impl_uri, transformer: fn ctx:
add_with_runtime = fn type, op, transformer, impl_uri: fn ctx:
built_in_name = '_${op}_'
pipe ctx:
add_runtime_fn built_in_name, ?
Expand Down
4 changes: 2 additions & 2 deletions src/lang/init.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
{add_arithmitic} = import './arithmitic/init.fnk'
{add_block} = import './block/init.fnk'
{add_partial} = import './partial/init.fnk'
{init_builtins} = import './runtime.fnk'
{init_runtimes} = import './runtime.fnk'



Expand Down Expand Up @@ -60,7 +60,7 @@ init_ctx = fn code, filename, options:
ignoreable_imports: []

pipe ctx:
init_builtins
init_runtimes
add_transformers


34 changes: 26 additions & 8 deletions src/lang/iterable/common.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,32 @@ babe_types = import '@babel/types'
transform_with_runtime_lib = fn iter_fn, node, ctx:
args_len = length node.args

[use_spread, exprs] = match node.exprs:
[..., {type: 'spread'}]:
[use_spread, exprs] = match node:
{op: not ? in ['map', 'unfold']}:
[[], node.exprs]
{exprs: [..., {type: 'spread'}]}:
spread = {type: 'literal', value: 'true', loc: node.loc}
[...exprs, last] = node.exprs
[spread, [...exprs, last.right]]
[[spread], [...exprs, last.right]]
else:
spread = {type: 'literal', value: 'false', loc: node.loc}
[spread, node.exprs]
[[spread], node.exprs]

[is_async, args] = match node.args:
[{op: 'await'}]:
async = {type: 'literal', value: 'true', loc: node.loc}
[{right}, ...rest] = node.args
[async, [right, ...rest]]
else:
async = {type: 'literal', value: 'false', loc: node.loc}
[async, node.args]

[fold_initial, final_args] = match node:
{op: 'fold', args: [, {type: 'assign'}]}:
[first, {left, right}, ...rest] = args
[[right], [first, left, ...rest]]
else:
[[], args]

[result, end_ctx] = transform
rec:
Expand All @@ -29,13 +47,13 @@ transform_with_runtime_lib = fn iter_fn, node, ctx:
args: seq:
rec:
type: 'fn'
args: node.args
args: final_args
exprs: exprs
loc: node.loc
{type: 'number', value: '${args_len}', loc: node.loc}
{type: 'literal', value: 'false', loc: node.loc}
use_spread

is_async
...use_spread
...fold_initial
loc: node.loc
ctx
[result, end_ctx]
Expand Down
30 changes: 25 additions & 5 deletions src/lang/iterable/filter.test.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,46 @@



describe 'gen 2', fn:
it 'compiles', fn:
describe 'filter', fn:
it 'compiles with default runtime', fn:
expect
fink2js '
{_filter_} = import "@fink/js-interop/iter.js"
filter item:
item % 2 == 0
'
to_match_snapshot

expect
fink2js '
{_filter_} = import "@fink/js-interop/iter.js"
filter item, cntr=0:
[item < 1 and cntr % 2 == 0, cntr + 1]
'
to_match_snapshot


it 'compiles with custom runtime', fn:
expect
fink2js '
{_filter_} = import "./filter.fnk"
filter item:
item % 2 == 0
'
to_match_snapshot

describe 'filter', fn:
expect
fink2js '
filter item, cntr=0:
[item < 1 and cntr % 2 == 0, cntr + 1]
'
to_match_snapshot



describe 'filter legacy', fn:
it 'compiles', fn:
expect
fink2js '
{_filter_} = import "@fink/loxia:iter:legacy"
filter item:
item % 2 == 0

Expand All @@ -43,20 +59,23 @@ describe 'filter', fn:
it 'compiles with accu', fn:
expect
fink2js '
{_filter_} = import "@fink/loxia:iter:legacy"
filter item, cntr=0:
[item < 1 and cntr % 2 == 0, cntr + 1]
'
to_match_snapshot

expect
fink2js '
{_filter_} = import "@fink/loxia:iter:legacy"
filter _, is_even=false:
[is_even, not is_even]
'
to_match_snapshot

expect
fink2js '
{_filter_} = import "@fink/loxia:iter:legacy"
filter , is_even=false:
[is_even, not is_even]
'
Expand All @@ -68,6 +87,7 @@ describe 'filter async', fn:
it 'compiles', fn:
expect
fink2js '
{_filter_} = import "@fink/loxia:iter:legacy"
filter item:
await item % 2 == 0
'
Expand Down
Loading

0 comments on commit b418292

Please sign in to comment.