Skip to content

Commit

Permalink
Merge pull request #134 from fink-lang/ir
Browse files Browse the repository at this point in the history
AST -> IR -> Optimizer -> JS
  • Loading branch information
kollhof authored Oct 6, 2021
2 parents 4be16ce + 203ee57 commit 21a21be
Show file tree
Hide file tree
Showing 166 changed files with 13,016 additions and 4,675 deletions.
2,491 changes: 1,453 additions & 1,038 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 5 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@
"test": "run jest",
"jest": "jest --verbose --runInBand --no-cache ",
"cd": "run clean test build release",
"release": "semantic-release --debug",
"sm": "run sm:*",
"sm:build": "fnk --src ../foo.fnk --out-dir ../ --source-maps inline --module-type 'mjs'",
"sm:view": "npx source-map-visualize ../foo.js"
"release": "semantic-release --debug"
},
"devDependencies": {
"@fink/cli": "^8.0.0",
Expand All @@ -53,7 +50,7 @@
"cz-conventional-changelog": "^3.1.0",
"jest-cli": "^27.0.0",
"npx-run": "^2.1.2",
"semantic-release": "^17.2.1"
"semantic-release": "^18.0.0"
},
"peerDependencies": {
"@fink/js-interop": ">=2.5"
Expand All @@ -62,9 +59,10 @@
"@babel/core": "^7.10.5",
"@babel/traverse": "^7.10.5",
"@babel/types": "^7.10.5",
"@fink/js-interop": ">=3.1.1",
"@fink/js-interop": ">=2.5",
"@fink/snippet": "^2.2.0",
"@fink/std-lib": "^8.5.0"
"@fink/std-lib": "^8.5.0",
"hamt": "^2.2.2"
},
"config": {
"commitizen": {
Expand Down
29 changes: 17 additions & 12 deletions src/generate.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,27 @@ babel_traverse = import '@babel/traverse'
{transformFromAstSync} = import '@babel/core'
{try_catch} = import '@fink/js-interop/errors.fnk'

{transform} = import './js/transform.fnk'
{init_ctx} = import './js/init.fnk'
{transform} = import './ir/transform.fnk'
{init_ctx} = import './ir/init.fnk'
{optimize} = import './optimize/init.fnk'
{transform_ir} = import './js/init.fnk'
{module_transforms} = import './js/module/init.fnk'
{transform_async} = import './js/async/init.fnk'

{transform_do_expr} = import './js/do-expression.fnk'
{transform_async} = import './js/async.fnk'
{module_transforms} = import './js/module.fnk'
{transform_tail_call} = import './js/tail-call.fnk'

# try_catch_ = fn f: [false, f _]


transform_file = fn fink_ast, code, filename, options:
ctx = init_ctx code, filename, options
transform_file = fn fink_ast, code, filename, {optimize: optim, ...options}:
opts = {...options, optimize: {refs: true, tails: true, unused: true, ...optim}}

ctx = init_ctx code, filename, opts

[error, [js_ast]=[]] = try_catch fn:
transform fink_ast, ctx
[lir] = transform fink_ast, 'mod', ctx
[olir] = optimize lir, opts
{js} = transform_ir olir, opts
[js]

extras = match options:
{module_type: 'cjs'}: module_transforms
Expand All @@ -27,9 +33,7 @@ transform_file = fn fink_ast, code, filename, options:
match error:
false:
traverse js_ast, rec:
DoExpression: transform_do_expr
AwaitExpression: {enter: transform_async}
ArrowFunctionExpression: {exit: transform_tail_call}
...extras

{...js_ast, errors: []}
Expand Down Expand Up @@ -63,7 +67,8 @@ generate = fn ast, filename, source, options={}:
js_ast = transform_file ast, source, filename, options

match js_ast:
{errors: [{}]}:
{errors: [?]}:
{code: '', source_map: '', errors: js_ast.errors}
else:
babel_generate js_ast, filename, source, options

34 changes: 31 additions & 3 deletions src/generate.test.fnk
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{describe, it, expect, to_equal, to_match_snapshot} = import '@fink/jest/test.fnk'
{skip, describe, it, expect, to_equal, to_match_snapshot} = import '@fink/jest/test.fnk'
{slice} = import '@fink/std-lib/str.fnk'

{generate} = import './generate.fnk'
{fink2js} = import './testing/generate.fnk'
{fink2js, fink2js_sm} = import './testing/generate.fnk'



Expand All @@ -20,6 +20,7 @@ describe 'module types', fn:
case = ni

default = shrub
out = [dflt, if, spam, ni, foo]
"

it 'compiles to mjs', fn:
Expand All @@ -35,6 +36,33 @@ describe 'module types', fn:



describe 'source maps', fn:
code = "
nanu = 345
foo = {bar: 12345, 'spam ni': nanu, nanu}
bar = 'foo'
ham = 'foo: \${foo} bar:\${bar}'
el = <Foo>bar {123} <div foo=134 bar spam=nanu></div></Foo>
ni = spam + foo-bar
a = [1, 'ni']
[b, c] = a
out = [a, b, c]
func = fn x, y:
x + y + b
y = func bar, a, b
spam ham
r1 = {foo, bar, spam: 123}
"


it 'compiles to mjs', fn:
js = fink2js_sm code
expect
js
to_match_snapshot



describe 'errors', fn:
it 'handles parse errors', fn:
{errors: [{error}]} = fink2js '
Expand All @@ -50,7 +78,7 @@ describe 'errors', fn:
Expected `,` or indented(>=1) new line or `]`.
'

it 'errors with code snippet', fn:
skip.it 'errors with code snippet', fn:
{errors: [{message}]} = fink2js '
foo = bar
123 = foo
Expand Down
113 changes: 93 additions & 20 deletions src/generate.test.fnk.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,108 @@
exports[`module types compiles to cjs 1`] = `
"require(\\"foobar\\");
const foo = require(\\"shrub\\");
const foo_0 = require(\\"shrub\\");
const {
bar: spam,
ni
bar: spam_0
} = require(\\"ni\\");
// reserved words import
const {
default: dflt,
ˆif
ni: ni_0
} = require(\\"ni\\");
const {
ˆdefault: dflt_0
} = require(\\"es6-module\\");
const {
ˆif: if_0
} = require(\\"es6-module\\");
const shrub = ni;
exports.shrub = shrub;
// reserved export
const ˆcase = ni;
exports.ˆcase = ˆcase;
exports.default = shrub;"
const out_0 = [dflt_0, if_0, spam_0, ni_0, foo_0];
module.exports = ni_0;
exports.shrub = ni_0;
exports.ˆcase = ni_0;
exports.ˆdefault = ni_0;
exports.out = out_0;"
`;

exports[`module types compiles to mjs 1`] = `
"import \\"foobar\\";
import foo from \\"shrub\\";
import { bar as spam, ni } from \\"ni\\";
// reserved words import
import { default as dflt, ˆif } from \\"es6-module\\";
export const shrub = ni;
// reserved export
export const ˆcase = ni;
export default shrub;"
import foo_0 from \\"shrub\\";
import { bar as spam_0 } from \\"ni\\";
import { ni as ni_0 } from \\"ni\\";
import { ˆdefault as dflt_0 } from \\"es6-module\\";
import { ˆif as if_0 } from \\"es6-module\\";
const out_0 = [dflt_0, if_0, spam_0, ni_0, foo_0];
export const shrub = ni_0,
ˆcase = ni_0,
ˆdefault = ni_0,
out = out_0;
export default ni_0;"
`;

exports[`source maps compiles to mjs 1`] = `
"const foo_0 = {
bar: 12345,
\\"spam ni\\": 345,
nanu: 345
};
const bar_0 = \`foo\`;
const ham_0 = \`foo: \${foo_0} bar:\${bar_0}\`;
const el_0 = <Foo>bar {123} <div foo={134} bar={bar_0} spam={345} /></Foo>;
const ni_0 = spam + fooᜭbar;
const item_0 = \`ni\`;
const a_0 = [1, item_0];
const out_0 = [a_0, 1, item_0];
const func_0 = (x_0, y_0) => {
return x_0 + y_0 + 1;
};
const y_1 = func_0(bar_0, a_0, 1);
spam(ham_0);
const r1_0 = {
foo: foo_0,
bar: bar_0,
spam: 123
};
export const nanu = 345,
foo = foo_0,
bar = bar_0,
ham = ham_0,
el = el_0,
ni = ni_0,
a = a_0,
out = out_0,
func = func_0,
y = y_1,
r1 = r1_0;
{
\\"version\\": 3,
\\"sources\\": [
\\"test.fnk\\"
],
\\"names\\": [
\\"foo\\",
\\"bar\\",
\\"nanu\\",
\\"ham\\",
\\"el\\",
\\"div\\",
\\"spam\\",
\\"ni\\",
\\"foo-bar\\",
\\"a\\",
\\"out\\",
\\"c\\",
\\"b\\",
\\"func\\",
\\"x\\",
\\"y\\",
\\"r1\\"
],
\\"mappings\\": \\"AACAA,MAAAA,KAAG,GAAiC;AAA7BC,EAAAA,GAA6B,EAAxB,KAAwB;AAAjB,WAAiB,EAANC,GAAM;AAAAA,EAAAA,IAAI,EAAJA;AAAA,CAApCF;AACAC,MAAAA,KAAG,GAAI,KAAPA;AACAE,MAAAA,KAAG,GAAI,QAAOH,KAAI,QAAOC,KAAG,EAA5BE;AACAC,MAAAA,IAAE,GAAG,KAAK,IAAI,KAAK,CAAC,CAACC,GAAD,CAAKL,GAAL,CAAS,KAAT,CAAaC,GAAb,CAAaA,CAAAA,KAAG,CAAhB,CAAiBK,IAAjB,CAAsBJ,KAAtB,GAAf,MAALE;AACAG,MAAAA,IAAE,GAAGD,IAAI,GAAGE,OAAZD;AACQ,MAAA,MAAI,GAAH,IAAD;AAARE,MAAAA,GAAC,GAAO,CAAH,CAAG,EAAA,MAAI,CAAZA;AAEAC,MAAAA,KAAG,GAAUC,CAANF,GAAME,EAAHC,CAAGD,EAAAA,MAAC,CAAdD;;AACAG,MAAAA,MAAI,GAAG,CAAGC,GAAH,EAAMC,GAAN;AACL,SAAAD,GAAC,GAAGC,GAAJ,GAAQH,CAAR;AADK,CAAPC;;AAEAE,MAAAA,GAAC,GAAGF,MAAI,CAACZ,KAAD,EAAMQ,GAAN,EAASG,CAAT,CAARG;AACAT,IAAI,CAACH,KAAD,CAAJ;AACAa,MAAAA,IAAE,GAAc;AAAVhB,EAAAA,GAAU,EAAVA,KAAU;AAALC,EAAAA,GAAK,EAALA,KAAK;AAAAK,EAAAA,IAAI,EAAE;AAAN,CAAhBU;aAbAd,IAAI,GAAJA,G;MACAF,GAAG,GAAHA,K;MACAC,GAAG,GAAHA,K;MACAE,GAAG,GAAHA,K;MACAC,EAAE,GAAFA,I;MACAG,EAAE,GAAFA,I;MACAE,CAAC,GAADA,G;MAEAC,GAAG,GAAHA,K;MACAG,IAAI,GAAJA,M;MAEAE,CAAC,GAADA,G;MAEAC,EAAE,GAAFA,I\\"
}
"
`;
21 changes: 21 additions & 0 deletions src/ir/arithmitic/init.fnk
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{add, any} = import '../context.fnk'
{transform_binary, transform_unary} = import '../transform.fnk'



transform_arithmitic = fn node, res_id, ctx:
transform_binary node.op, node.left, node.right, res_id, {loc: node.loc}, ctx



transform_prefix = fn node, res_id, ctx:
transform_unary node.op, node.right, res_id, {loc: node.loc}, ctx



add_arithmitic = fn ctx:
pipe ctx:
add 'arithm', any, transform_arithmitic
add 'arithm:right', any, transform_arithmitic
add 'arithm:prefix', any, transform_prefix

44 changes: 44 additions & 0 deletions src/ir/arithmitic/init.test.fnk
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{fink2lir} = import '../../testing/generate.fnk'
{describe, it, expect, to_match_snapshot} = import '@fink/jest/test.fnk'


describe 'binary', fn:
it 'compiles simple', fn:
expect
fink2lir '
r = foo + bar
'
to_match_snapshot


it 'compiles precedence', fn:
expect
fink2lir '
r = (1 + 2) * ni
'
to_match_snapshot


it 'compiles nested', fn:
expect
fink2lir '
foo * bar + ni
'
to_match_snapshot



describe 'unary', fn:
it 'compiles simple', fn:
expect
fink2lir '
-foo
'
to_match_snapshot

it 'compiles nested', fn:
expect
fink2lir '
foo * -bar
'
to_match_snapshot
Loading

0 comments on commit 21a21be

Please sign in to comment.