Skip to content

Commit

Permalink
Merge pull request #6 from fink-lang/new-features
Browse files Browse the repository at this point in the history
support new lang features
  • Loading branch information
kollhof authored Feb 23, 2020
2 parents 8f4c971 + 2b72271 commit ee3d53c
Show file tree
Hide file tree
Showing 54 changed files with 1,020 additions and 425 deletions.
248 changes: 126 additions & 122 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@babel/cli": "^7.2.3",
"@babel/plugin-proposal-pipeline-operator": "^7.3.2",
"@babel/preset-env": "^7.3.4",
"@fink/larix": "^1.1.0",
"@nearmap/eslint-config-base": "^1.1.0",
"babel-eslint": "^10.0.1",
"commitizen": "^4.0.3",
Expand All @@ -47,15 +48,11 @@
"eslint-plugin-babel": "^5.3.0",
"jest-cli": "^25.1.0",
"npx-run": "^2.1.2",
"semantic-release": "^17.0.0",
"@fink/larix": "^1.0.1"
"semantic-release": "^17.0.4"
},
"dependencies": {
"@babel/generator": "^7.8.4",
"@babel/traverse": "^7.8.4",
"@babel/types": "^7.8.3"
},
"peerDependencies": {
"@fink/larix": "^1.0.1"
}
}
84 changes: 56 additions & 28 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import babel_gen from '@babel/generator';
import traverse from '@babel/traverse';
import {identifier, numericLiteral} from '@babel/types';

import convert_source_map from 'convert-source-map';

import {other_token} from '@fink/prattler/symbols';

import {ident, expr_block} from './types';
import {ident, expr_block, wrap} from './types';
import {code_frame_err} from './errors';

import {transform_assign} from './transform/assign';
Expand All @@ -14,6 +15,8 @@ import {transform_attempt} from './transform/attempt';
import {transform_match} from './transform/match';
import {transform_map} from './transform/map';
import {transform_flat_map} from './transform/map';
import {transform_filter} from './transform/filter';
import {transform_while} from './transform/while';
import {transform_fold} from './transform/fold';
import {transform_unfold} from './transform/unfold';
import {transform_call} from './transform/call';
Expand All @@ -31,6 +34,9 @@ import {transform_group} from './transform/group';
import {transform_module} from './transform/module';
import {transform_unary} from './transform/unary';
import {transform_member} from './transform/member';
import {transform_inifx} from './transform/infix';
import {transform_other, escape_ident, var_prefix} from './transform/other';

import {
transform_jsx_elem, transform_jsx_attr, transform_jsx_str,
transform_jsx_expr_container, transform_jsx_text
Expand All @@ -40,14 +46,6 @@ import transform_do_expr from './transform/js/do-expression';
import {transform_async} from './transform/js/async';


const transform_other = (node)=> {
if (node.value.match(/^[0-9].*/)) {
return numericLiteral(parseInt(node.value));
}
return identifier(node.value);
};


const jsx = {
'jsx-elem': transform_jsx_elem,
'jsx-attr': transform_jsx_attr,
Expand All @@ -73,7 +71,8 @@ const literals = {
const unary_ops = {
arithm_prefix: transform_unary,
await: transform_await,
'...': transform_spread
'...': transform_spread,
'!': transform_unary
};

const binary_ops = {
Expand All @@ -89,27 +88,29 @@ const binary_ops = {

'.': transform_member,

call: transform_call,
'|': transform_pipe
call: transform_call
};

const block_like = {
block: transform_block,
func: transform_func,
fn: transform_func,
module: transform_module
};

const control_flow = {
if: transform_cond,
match: transform_match,
attempt: transform_attempt
attempt: transform_attempt,
pipe: transform_pipe
};

const iterables = {
fold: transform_fold,
unfold: transform_unfold,
map: transform_map,
flat_map: transform_flat_map
flat_map: transform_flat_map,
filter: transform_filter,
while: transform_while
};


Expand All @@ -120,7 +121,8 @@ const transformers = {
...block_like,
...control_flow,
...iterables,
...jsx
...jsx,
infix: transform_inifx
};


Expand All @@ -134,17 +136,34 @@ const get_ctx = (transform, ctx)=> {
};


// eslint-disable-next-line prefer-reflect
const obj_has = (obj, key)=> Object.prototype.hasOwnProperty.call(obj, key);


const get_transformer = (op, type)=> {
if (obj_has(transformers, op)) {
return transformers[op];
}

if (obj_has(transformers, type)) {
return transformers[type];
}
};


const transform_expr = (node, ctx)=> {
const transform = transformers[node.op] || transformers[node.type];
const transform = get_transformer(node.op, node.type);

if (transform === undefined) {
throw code_frame_err(new Error('Unknown expression'), node, ctx.code);
} else {
try {
return transform(node, get_ctx(transform_expr, ctx));
} catch (err) {
throw code_frame_err(err, node, ctx.code);
}
}

try {
const foo = transform(node, get_ctx(transform_expr, ctx));
// TODO: some nodes have location data
return wrap(node, foo);
} catch (err) {
throw code_frame_err(err, node, ctx.code);
}
};

Expand All @@ -155,7 +174,7 @@ const transform = (node, code)=> {
code,
unique_ident: (name)=> {
id_ctr += 1;
return ident(`$${name}${id_ctr}`);
return ident(`${var_prefix}${name}_${id_ctr}`);
}
});

Expand All @@ -174,12 +193,21 @@ export const generate = (ast, filename, code)=> {
const options = {
// retainLines: true,
filename,
sourceMaps: true,
sourceMaps: 'both',
sourceFileName: filename
// shouldPrintComment: ()=> true,
};

const output = babel_gen(new_ast, options, code);
return {...output, ast: new_ast};
const generated = babel_gen(new_ast, options, code);

const source_map = convert_source_map
.fromObject(generated.map)
.toComment();

// TODO: embedd source-map?
// const final_code = `${generated.code}\n\n${source_map}\n`;
const final_code = generated.code;

return {...generated, code: final_code, ast: new_ast};
};

8 changes: 7 additions & 1 deletion src/transform/__snapshots__/array.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@ exports[`compiles array 1`] = `
"const array1 = [];
const array2 = [1];
const array3 = [1, 2, 4, a(), ...b];
const array4 = [a + 1 + 45 + b + c, [1, 2], (3 + 3) * 2];"
const array4 = [a + 1 + 45 + b + c, [1, 2], (3 + 3) * 2];
Object.assign(module.exports, {
array1,
array2,
array3,
array4
});"
`;
10 changes: 9 additions & 1 deletion src/transform/__snapshots__/assign.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,13 @@ const assign2 = assign;
const no_clash_with_await = 13;
const assign_lang_const1 = true;
const assign_lang_const2 = false;
const multi_line_assign = 123 + 234 + (-567 - 1111);"
const multi_line_assign = 123 + 234 + (-567 - 1111);
Object.assign(module.exports, {
assign,
assign2,
no_clash_with_await,
assign_lang_const1,
assign_lang_const2,
multi_line_assign
});"
`;
15 changes: 10 additions & 5 deletions src/transform/__snapshots__/attempt.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ exports[`compiles attempt 1`] = `
try {
const bar = shrub();
_do_result = [null, bar + ni()];
} catch ($error1) {
_do_result = [$error1, null];
} catch (ˆerror_1) {
_do_result = [ˆerror_1, null];
}
const [item, err] = _do_result;
Expand All @@ -18,7 +18,12 @@ exports[`compiles attempt 1`] = `
const foo2 = () => try {
return [null, 1 / 0];
} catch ($error2) {
return [$error2, null];
};"
} catch (ˆerror_2) {
returnerror_2, null];
};
Object.assign(module.exports, {
foo1,
foo2
});"
`;
16 changes: 11 additions & 5 deletions src/transform/__snapshots__/await.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@ const a_gen = async function* unfold(curr = 0) {
_do_result = curr + 1;
}
const $result1 = _do_result;
const ˆresult_1 = _do_result;
_do_result = undefined;
yield $result1;
curr = $result1;
yield ˆresult_1;
curr = ˆresult_1;
}
}();
};
await ni;"
await ni;
Object.assign(module.exports, {
task1,
task2,
task3,
a_gen
});"
`;
9 changes: 8 additions & 1 deletion src/transform/__snapshots__/binary.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,12 @@ exports[`binary 1`] = `
const math_precedence2 = -1 + 0 + (1 + 2) * (3 / 2);
const multi_line_assign = 123 + 234 + (-567 - 1111);
const group1 = (1 + 2) * 3;
const group2 = 34234 ** -34234 + 1;"
const group2 = 34234 ** -34234 + 1;
Object.assign(module.exports, {
math_precedence1,
math_precedence2,
multi_line_assign,
group1,
group2
});"
`;
8 changes: 7 additions & 1 deletion src/transform/__snapshots__/call.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@ exports[`compiles call 1`] = `
"const call1 = a(ni, x = 123, ...x);
const call2 = a(ni);
const call3 = a();
const call4 = a(x => x * 2);"
const call4 = a(x => x * 2);
Object.assign(module.exports, {
call1,
call2,
call3,
call4
});"
`;
6 changes: 5 additions & 1 deletion src/transform/__snapshots__/cond.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,9 @@ _do_result2 = undefined;
} else {
null;
}
}"
}
Object.assign(module.exports, {
with_else,
no_else
});"
`;
13 changes: 13 additions & 0 deletions src/transform/__snapshots__/filter.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`filter 1`] = `
"(function* filter(ˆitems_2) {
for (const ˆitem_1 of ˆitems_2) {
const item = ˆitem_1;
const ˆresult_3 = item % 2 === 0;
if (ˆresult_3) yield ˆitem_1;
}
});
Object.assign(module.exports, {});"
`;
16 changes: 9 additions & 7 deletions src/transform/__snapshots__/fold.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`compiles fold 1`] = `
"$items1 => {
let $acc2 = 0;
"ˆitems_1 => {
let ˆacc_2 = 0;
for (const item of $items1) {
const acc = $acc2;
for (const item of ˆitems_1) {
const acc = ˆacc_2;
const ni = item + acc;
$acc2 = item * acc;
ˆacc_2 = item * acc;
}
return $acc2;
};"
return ˆacc_2;
};
Object.assign(module.exports, {});"
`;
Loading

0 comments on commit ee3d53c

Please sign in to comment.