diff --git a/package-lock.json b/package-lock.json index 6295c4e..02c4a20 100644 --- a/package-lock.json +++ b/package-lock.json @@ -25,7 +25,6 @@ "version": "7.8.3", "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.8.3.tgz", "integrity": "sha512-a9gxpmdXtZEInkCSHUJDLHZVBgb1QS0jhss4cPP93EW7s+uC5bikET2twEF3KV+7rDblJcmNvTR7VJejqd2C2g==", - "dev": true, "requires": { "@babel/highlight": "^7.8.3" } @@ -85,7 +84,6 @@ "version": "7.8.4", "resolved": "https://registry.npmjs.org/@babel/generator/-/generator-7.8.4.tgz", "integrity": "sha512-PwhclGdRpNAf3IxZb0YVuITPZmmrXz9zf6fH8lT4XbrmfQKr6ryBzhv593P5C6poJRciFCL/eHGW2NuGrgEyxA==", - "dev": true, "requires": { "@babel/types": "^7.8.3", "jsesc": "^2.5.1", @@ -171,7 +169,6 @@ "version": "7.8.3", "resolved": "https://registry.npmjs.org/@babel/helper-function-name/-/helper-function-name-7.8.3.tgz", "integrity": "sha512-BCxgX1BC2hD/oBlIFUgOCQDOPV8nSINxCwM3o93xP4P9Fq6aV5sgv2cOOITDMtCfQ+3PvHp3l689XZvAM9QyOA==", - "dev": true, "requires": { "@babel/helper-get-function-arity": "^7.8.3", "@babel/template": "^7.8.3", @@ -182,7 +179,6 @@ "version": "7.8.3", "resolved": "https://registry.npmjs.org/@babel/helper-get-function-arity/-/helper-get-function-arity-7.8.3.tgz", "integrity": "sha512-FVDR+Gd9iLjUMY1fzE2SR0IuaJToR4RkCDARVfsBBPSP53GEqSFjD8gNyxg246VUyc/ALRxFaAK8rVG7UT7xRA==", - "dev": true, "requires": { "@babel/types": "^7.8.3" } @@ -291,7 +287,6 @@ "version": "7.8.3", "resolved": "https://registry.npmjs.org/@babel/helper-split-export-declaration/-/helper-split-export-declaration-7.8.3.tgz", "integrity": "sha512-3x3yOeyBhW851hroze7ElzdkeRXQYQbFIb7gLK1WQYsw2GWDay5gAJNw1sWJ0VFP6z5J1whqeXH/WCdCjZv6dA==", - "dev": true, "requires": { "@babel/types": "^7.8.3" } @@ -323,7 +318,6 @@ "version": "7.8.3", "resolved": "https://registry.npmjs.org/@babel/highlight/-/highlight-7.8.3.tgz", "integrity": "sha512-PX4y5xQUvy0fnEVHrYOarRPXVWafSjTW9T0Hab8gVIawpl2Sj0ORyrygANq+KjcNlSSTw0YCLSNA8OyZ1I4yEg==", - "dev": true, "requires": { "chalk": "^2.0.0", "esutils": "^2.0.2", @@ -333,8 +327,7 @@ "@babel/parser": { "version": "7.8.4", "resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.8.4.tgz", - "integrity": "sha512-0fKu/QqildpXmPVaRBoXOlyBb3MC+J0A66x97qEfLOMkn3u6nfY5esWogQwi/K0BjASYy4DbnsEWnpNL6qT5Mw==", - "dev": true + "integrity": "sha512-0fKu/QqildpXmPVaRBoXOlyBb3MC+J0A66x97qEfLOMkn3u6nfY5esWogQwi/K0BjASYy4DbnsEWnpNL6qT5Mw==" }, "@babel/plugin-proposal-async-generator-functions": { "version": "7.8.3", @@ -910,7 +903,6 @@ "version": "7.8.3", "resolved": "https://registry.npmjs.org/@babel/template/-/template-7.8.3.tgz", "integrity": "sha512-04m87AcQgAFdvuoyiQ2kgELr2tV8B4fP/xJAVUL3Yb3bkNdMedD3d0rlSQr3PegP0cms3eHjl1F7PWlvWbU8FQ==", - "dev": true, "requires": { "@babel/code-frame": "^7.8.3", "@babel/parser": "^7.8.3", @@ -921,7 +913,6 @@ "version": "7.8.4", "resolved": "https://registry.npmjs.org/@babel/traverse/-/traverse-7.8.4.tgz", "integrity": "sha512-NGLJPZwnVEyBPLI+bl9y9aSnxMhsKz42so7ApAv9D+b4vAFPpY013FTS9LdKxcABoIYFU52HcYga1pPlx454mg==", - "dev": true, "requires": { "@babel/code-frame": "^7.8.3", "@babel/generator": "^7.8.4", @@ -938,7 +929,6 @@ "version": "4.1.1", "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz", "integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==", - "dev": true, "requires": { "ms": "^2.1.1" } @@ -946,8 +936,7 @@ "ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", - "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", - "dev": true + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" } } }, @@ -955,7 +944,6 @@ "version": "7.8.3", "resolved": "https://registry.npmjs.org/@babel/types/-/types-7.8.3.tgz", "integrity": "sha512-jBD+G8+LWpMBBWvVcdr4QysjUE4mU/syrhN17o1u3gx0/WzJB1kwiVZAXRtWbsIPOwW8pF/YJV5+nmetPzepXg==", - "dev": true, "requires": { "esutils": "^2.0.2", "lodash": "^4.17.13", @@ -2421,7 +2409,6 @@ "version": "3.2.1", "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz", "integrity": "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==", - "dev": true, "requires": { "color-convert": "^1.9.0" } @@ -3072,7 +3059,6 @@ "version": "2.4.2", "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz", "integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==", - "dev": true, "requires": { "ansi-styles": "^3.2.1", "escape-string-regexp": "^1.0.5", @@ -3248,7 +3234,6 @@ "version": "1.9.3", "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.3.tgz", "integrity": "sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg==", - "dev": true, "requires": { "color-name": "1.1.3" } @@ -3256,8 +3241,7 @@ "color-name": { "version": "1.1.3", "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz", - "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=", - "dev": true + "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=" }, "colors": { "version": "1.0.3", @@ -4055,8 +4039,7 @@ "escape-string-regexp": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", - "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=", - "dev": true + "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=" }, "escodegen": { "version": "1.14.1", @@ -4374,8 +4357,7 @@ "esutils": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/esutils/-/esutils-2.0.3.tgz", - "integrity": "sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==", - "dev": true + "integrity": "sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==" }, "exec-sh": { "version": "0.3.4", @@ -5592,8 +5574,7 @@ "globals": { "version": "11.12.0", "resolved": "https://registry.npmjs.org/globals/-/globals-11.12.0.tgz", - "integrity": "sha512-WOBp/EEGUiIsJSp7wcv/y6MO+lV9UoncWqxuFfm8eBwzWNgyfBd6Gz+IeKQ9jCmyhoH99g15M3T+QaVHFjizVA==", - "dev": true + "integrity": "sha512-WOBp/EEGUiIsJSp7wcv/y6MO+lV9UoncWqxuFfm8eBwzWNgyfBd6Gz+IeKQ9jCmyhoH99g15M3T+QaVHFjizVA==" }, "globby": { "version": "11.0.0", @@ -5711,8 +5692,7 @@ "has-flag": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", - "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=", - "dev": true + "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=" }, "has-symbols": { "version": "1.0.1", @@ -7878,8 +7858,7 @@ "js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", - "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==", - "dev": true + "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==" }, "js-yaml": { "version": "3.13.1", @@ -7934,8 +7913,7 @@ "jsesc": { "version": "2.5.2", "resolved": "https://registry.npmjs.org/jsesc/-/jsesc-2.5.2.tgz", - "integrity": "sha512-OYu7XEzjkCQ3C5Ps3QIZsQfNpqoJyZZA99wd9aWd05NCtC5pWOkShK2mkL6HXQR6/Cy2lbNdPlZBpuQHXE63gA==", - "dev": true + "integrity": "sha512-OYu7XEzjkCQ3C5Ps3QIZsQfNpqoJyZZA99wd9aWd05NCtC5pWOkShK2mkL6HXQR6/Cy2lbNdPlZBpuQHXE63gA==" }, "json-parse-better-errors": { "version": "1.0.2", @@ -8291,8 +8269,7 @@ "lodash": { "version": "4.17.15", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz", - "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==", - "dev": true + "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==" }, "lodash.capitalize": { "version": "4.2.1", @@ -14177,8 +14154,7 @@ "source-map": { "version": "0.5.7", "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.5.7.tgz", - "integrity": "sha1-igOdLRAh0i0eoUyA2OpGi6LvP8w=", - "dev": true + "integrity": "sha1-igOdLRAh0i0eoUyA2OpGi6LvP8w=" }, "source-map-resolve": { "version": "0.5.3", @@ -14477,7 +14453,6 @@ "version": "5.5.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz", "integrity": "sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==", - "dev": true, "requires": { "has-flag": "^3.0.0" } @@ -14703,8 +14678,7 @@ "to-fast-properties": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/to-fast-properties/-/to-fast-properties-2.0.0.tgz", - "integrity": "sha1-3F5pjL0HkmW8c+A3doGk5Og/YW4=", - "dev": true + "integrity": "sha1-3F5pjL0HkmW8c+A3doGk5Og/YW4=" }, "to-object-path": { "version": "0.3.0", diff --git a/package.json b/package.json index a15e96e..cdbc625 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/src/index.js b/src/index.js index a39093d..be1a425 100644 --- a/src/index.js +++ b/src/index.js @@ -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); diff --git a/src/transform/__snapshots__/await.test.js.snap b/src/transform/__snapshots__/await.test.js.snap index 6e616a6..e761772 100644 --- a/src/transform/__snapshots__/await.test.js.snap +++ b/src/transform/__snapshots__/await.test.js.snap @@ -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; } }(); diff --git a/src/transform/__snapshots__/cond.test.js.snap b/src/transform/__snapshots__/cond.test.js.snap index 5e75464..bbc82c0 100644 --- a/src/transform/__snapshots__/cond.test.js.snap +++ b/src/transform/__snapshots__/cond.test.js.snap @@ -25,5 +25,12 @@ if (foo === true) { } const no_else = _do_result2; -_do_result2 = undefined;" +_do_result2 = undefined; +{ + if (foo) { + shrub; + } else { + null; + } +}" `; diff --git a/src/transform/__snapshots__/map.test.js.snap b/src/transform/__snapshots__/map.test.js.snap index b8d1cf1..0e9ee2d 100644 --- a/src/transform/__snapshots__/map.test.js.snap +++ b/src/transform/__snapshots__/map.test.js.snap @@ -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; } }); @@ -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; } });" `; diff --git a/src/transform/__snapshots__/unfold.test.js.snap b/src/transform/__snapshots__/unfold.test.js.snap index 5fd48e7..5bfb1ed 100644 --- a/src/transform/__snapshots__/unfold.test.js.snap +++ b/src/transform/__snapshots__/unfold.test.js.snap @@ -4,8 +4,7 @@ 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; } })(); @@ -13,8 +12,8 @@ exports[`compiles unfold 1`] = ` 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; } }();" `; diff --git a/src/transform/cond.test.js b/src/transform/cond.test.js index a4b552c..b8a1c2f 100644 --- a/src/transform/cond.test.js +++ b/src/transform/cond.test.js @@ -12,6 +12,8 @@ test('compiles conditional', ()=> { no_else = if: foo == true: spam shrub == 134: ni + + if: foo: shrub `) ).toMatchSnapshot(); }); diff --git a/src/transform/js/do-expression.js b/src/transform/js/do-expression.js index 33b3177..f9edee0 100644 --- a/src/transform/js/do-expression.js +++ b/src/transform/js/do-expression.js @@ -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); diff --git a/src/transform/unfold.js b/src/transform/unfold.js index 13e94c9..26a7b55 100644 --- a/src/transform/unfold.js +++ b/src/transform/unfold.js @@ -1,5 +1,6 @@ import { - assign, generator, for_of, split_last, ident, call, yield_or_stop, params + assign, generator, for_of, split_last, ident, call, yield_or_stop, + params, consts, yields } from '../types'; import {whileStatement, blockStatement} from '@babel/types'; import {block_statement} from './block'; @@ -29,11 +30,16 @@ export const transform_unfold = (node, {transform, unique_ident})=> { ? last_expr.exprs : [last_expr, false]; + const result_id = unique_ident('result'); + const gen = generator('unfold')(...acc_init)( loop( ...expressions.map(block_statement({transform})), - ...yield_or_stop(transform(result), unique_ident), - ...(next_value ? [assign(acc_id, transform(next_value))] : []) + consts(result_id, transform(result)), + yields(result_id), + next_value + ? assign(acc_id, transform(next_value)) + : assign(acc_id, result_id) ) ); diff --git a/src/types.js b/src/types.js index 4bb508c..9299be1 100644 --- a/src/types.js +++ b/src/types.js @@ -3,7 +3,7 @@ import { objectPattern, assignmentPattern, forOfStatement, yieldExpression, expressionStatement, callExpression, functionExpression, blockStatement, identifier, arrowFunctionExpression, - returnStatement, logicalExpression, binaryExpression, unaryExpression, + logicalExpression, binaryExpression, unaryExpression, stringLiteral, memberExpression, ifStatement, doExpression, assignmentExpression, isAssignmentExpression, isObjectExpression, isArrayExpression, isIdentifier @@ -96,9 +96,6 @@ export const assign = (left, right)=> ( ); -export const returns = (expr)=> returnStatement(expr); - - export const yields = (expr, delegate)=> ( expressionStatement(yieldExpression(expr, delegate)) ); @@ -107,17 +104,21 @@ export const yields = (expr, delegate)=> ( export const iff = (test)=> (consequent)=> ifStatement(test, consequent); +// export const returns = (expr)=> returnStatement(expr); + + export const yield_or_stop = (expr, unique_ident, delegate)=> { const result = unique_ident('result'); return [ consts(result, expr), - iff(eq(result, ident('stop')))( - returns() - ), - iff(neq(result, ident('skip')))( - yields(result, delegate) - ) + // iff(eq(result, ident('stop')))( + // returns() + // ), + // iff(neq(result, ident('skip')))( + // yields(result, delegate) + // ) + yields(result, delegate) ]; };