Skip to content

Commit

Permalink
Merge pull request #4 from fink-lang/unfold-accumulator
Browse files Browse the repository at this point in the history
feat(unfold): have unfold use the last result as accumulator for next  iteration
  • Loading branch information
kollhof authored Feb 11, 2020
2 parents ca9bc3d + cda1acf commit ff49af2
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 74 deletions.
50 changes: 12 additions & 38 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 @@ -50,6 +50,9 @@
}
},
"dependencies": {
"@babel/generator": "^7.8.4",
"@babel/traverse": "^7.8.4",
"@babel/types": "^7.8.3",
"@fink/larix": "^1.0.0"
}
}
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ export const generate = (ast, filename, code)=> {
filename,
sourceMaps: true,
sourceFileName: filename
// shouldPrintComment: ()=> true,
};

const output = babel_gen(new_ast, options, code);
Expand Down
4 changes: 2 additions & 2 deletions src/transform/__snapshots__/await.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const a_gen = async function* unfold(curr = 0) {
const $result1 = _do_result;
_do_result = undefined;
if ($result1 === stop) return;
if ($result1 !== skip) yield $result1;
yield $result1;
curr = $result1;
}
}();
Expand Down
9 changes: 8 additions & 1 deletion src/transform/__snapshots__/cond.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,12 @@ if (foo === true) {
}
const no_else = _do_result2;
_do_result2 = undefined;"
_do_result2 = undefined;
{
if (foo) {
shrub;
} else {
null;
}
}"
`;
18 changes: 6 additions & 12 deletions src/transform/__snapshots__/map.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@ exports[`compiles map 1`] = `
"(function* map($items1) {
for (const item of $items1) {
const $result2 = item * 2;
if ($result2 === stop) return;
if ($result2 !== skip) yield $result2;
yield $result2;
}
});
(function* map($items3) {
for (const item = 123 of $items3) {
const $result4 = item * 2;
if ($result4 === stop) return;
if ($result4 !== skip) yield $result4;
yield $result4;
}
});
Expand All @@ -22,32 +20,28 @@ exports[`compiles map 1`] = `
item
} of $items5) {
const $result6 = item * 2;
if ($result6 === stop) return;
if ($result6 !== skip) yield $result6;
yield $result6;
}
});
(function* map($items7) {
for (const [x, y] of $items7) {
const $result8 = x + y;
if ($result8 === stop) return;
if ($result8 !== skip) yield $result8;
yield $result8;
}
});
(function* map($items9) {
for (const item of $items9) {
const $result10 = [spam + item];
if ($result10 === stop) return;
if ($result10 !== skip) yield* $result10;
yield* $result10;
}
});
(function* map($items11) {
for (const item of $items11) {
const $result12 = [spam];
if ($result12 === stop) return;
if ($result12 !== skip) yield* $result12;
yield* $result12;
}
});"
`;
7 changes: 3 additions & 4 deletions src/transform/__snapshots__/unfold.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ exports[`compiles unfold 1`] = `
"(function* unfold(curr = start) {
while (true) {
const $result1 = curr + inc;
if ($result1 === stop) return;
if ($result1 !== skip) yield $result1;
yield $result1;
curr = curr * 2;
}
})();
const count = (start = 0, inc = 1) => function* unfold(curr = start) {
while (true) {
const $result2 = curr + inc;
if ($result2 === stop) return;
if ($result2 !== skip) yield $result2;
yield $result2;
curr = $result2;
}
}();"
`;
2 changes: 2 additions & 0 deletions src/transform/cond.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ test('compiles conditional', ()=> {
no_else = if:
foo == true: spam
shrub == 134: ni
if: foo: shrub
`)
).toMatchSnapshot();
});
Expand Down
9 changes: 5 additions & 4 deletions src/transform/js/do-expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,15 @@ const transform_do_expr = (path)=> {
parent.node.body = simple(path.get('body'), true);
replace_with_return(path);

} else if (parent.isExpressionStatement()) {
// this is used e.g. for conditional at the module level
parent.replaceWith(path.get('body').node);

} else {
throw parent.buildCodeFrameError(`Can't convert do-expression`);
}

// else if (parent.isExpressionStatement()) {
// parent.replaceWith(path.get('body').node);

// } else if (parent.isReturnStatement()) {
// else if (parent.isReturnStatement()) {
// parent.replaceWith(simple(path.get('body'), true));
// replace_with_return(path);

Expand Down
Loading

0 comments on commit ff49af2

Please sign in to comment.