Skip to content

Commit

Permalink
refactor: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kollhof committed Sep 26, 2021
1 parent a92295a commit 33985ab
Show file tree
Hide file tree
Showing 22 changed files with 276 additions and 242 deletions.
20 changes: 6 additions & 14 deletions src/generate.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,23 @@ babel_traverse = import '@babel/traverse'

{transform} = import './ir/transform.fnk'
{init_ctx} = import './ir/init.fnk'
{serialize} = import './ir/serialize.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'

{timed, log_timings} = import './perf.fnk'

try_catch_ = fn f:
r = f _
[false, r]
transform_file = fn fink_ast, code, filename, {optimize: optim={}, ...options}:
opts = {...options, optimize: {refs: true, tails: true, unused: true, ...optim}}

transform_file = fn fink_ast, code, filename, options:
ctx = init_ctx code, filename, options
ctx = init_ctx code, filename, opts

[error, [js_ast]=[]] = try_catch fn:
[lir] = (timed transform) fink_ast, 'mod', ctx
[olir] = (timed optimize) lir, options
{js} = (timed transform_ir) olir, options
# console.log serialize lir
# console.log serialize olir
[lir] = transform fink_ast, 'mod', ctx
[olir] = optimize lir, opts
{js} = transform_ir olir, opts
[js]

# log_timings 'transform ${filename}'

extras = match options:
{module_type: 'cjs'}: module_transforms
else: {}
Expand Down
29 changes: 16 additions & 13 deletions src/ir/call/call.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,18 @@ transform_arg = fn expr, args_id, ctx:



transform_spread_arg = fn expr, args_id, {partial_foo=false, has_partial, ...ctx}:
[prlt, p_ctx] = match partial_foo:
false: unique_name 'prtl', expr, ctx
transform_spread_arg = fn expr, args_id, {partial_foo=false, has_partial=false, partial_ident=false, ...ctx}:
[prtl, p_ctx] = match partial_foo:
false: unique_name 'sprtl', expr, ctx
else: [partial_foo, ctx]

foo_ctx = {...p_ctx, partial_ident: prlt}
[arg, arg_id, {partial_ident:_, ...next_ctx}] = transform expr.right, 'sprd', foo_ctx
spread_arg args_id, arg, arg_id, {loc: expr.loc}, {...next_ctx, has_partial, partial_foo: prlt}

foo_ctx = {...p_ctx, partial_ident: prtl}
[arg, arg_id, {has_partial: hp=false, partial_ident:_, ...next_ctx}] = transform expr.right, 'sprd', foo_ctx
match hp:
false:
spread_arg args_id, arg, arg_id, {loc: expr.loc}, {...next_ctx, partial_ident, has_partial, partial_foo}
else:
spread_arg args_id, arg, arg_id, {loc: expr.loc}, {...next_ctx, partial_ident, has_partial: has_partial or hp, partial_foo: prtl}



Expand Down Expand Up @@ -158,17 +161,17 @@ transform_callee = fn expr, ctx:

transform_call = fn node, result, ctx:
[callee, c_id, args_ctx] = transform_callee node.callee, ctx
[args, args_id, {partial_foo, ...next_ctx}] = transform_args node, args_ctx
[args, args_id, {partial_foo=false, ...next_ctx}] = transform_args node, args_ctx

match partial_foo:
?:
[appl_fn, res_id, foo_ctx] = af c_id, args_id, 'pfn', node, next_ctx
call = [...callee, ...args, ...appl_fn]
func partial_foo, call, res_id, result, node, foo_ctx
else:
false:
[appl_fn, , end_ctx] = af c_id, args_id, result, node, next_ctx
call = [...callee, ...args, ...appl_fn]
[call, end_ctx]
else:
[appl_fn, res_id, foo_ctx] = af c_id, args_id, 'pfn', node, next_ctx
call = [...callee, ...args, ...appl_fn]
func partial_foo, call, res_id, result, node, foo_ctx



Expand Down
2 changes: 0 additions & 2 deletions src/ir/call/call.test.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,3 @@ describe 'call', fn:
map ? % 2 == 0
'
to_match_snapshot


68 changes: 28 additions & 40 deletions src/ir/call/call.test.fnk.snap
Original file line number Diff line number Diff line change
Expand Up @@ -38,39 +38,27 @@ rec >> exports_0:
= spam >> arg_5:
lst_a cargs_10, arg_5 >> cargs_11:
af callee_4, cargs_11 >> mex_4:
fn args_1, ret_1:
lst_h args_1 >> prtl_7:
= foo >> callee_5:
lst >> cargs_12:
= bar >> arg_6:
lst_a cargs_12, arg_6 >> cargs_13:
= spam >> sprd_0:
lst_c cargs_13, sprd_0 >> cargs_14:
af callee_5, cargs_14 >> pfn_0:
cc ret_1, pfn_0
>> mex_5:
fn args_2, ret_2:
lst_h args_2 >> prtl_8:
= foo >> callee_6:
lst >> cargs_15:
= bar >> sprd_1:
lst_c cargs_15, sprd_1 >> cargs_16:
af callee_6, cargs_16 >> pfn_1:
cc ret_2, pfn_1
>> mex_6:
fn args_3, ret_3:
lst_h args_3 >> prtl_9:
= foo >> callee_7:
lst >> cargs_17:
= bar >> sprd_2:
lst_c cargs_17, sprd_2 >> cargs_18:
= spam >> arg_7:
lst_a cargs_18, arg_7 >> cargs_19:
af callee_7, cargs_19 >> pfn_2:
cc ret_3, pfn_2
>> mex_7:
lst >> drctvs_0:
mod exports_0, drctvs_0 >> mod_0:"
= foo >> callee_5:
lst >> cargs_12:
= bar >> arg_6:
lst_a cargs_12, arg_6 >> cargs_13:
= spam >> sprd_0:
lst_c cargs_13, sprd_0 >> cargs_14:
af callee_5, cargs_14 >> mex_5:
= foo >> callee_6:
lst >> cargs_15:
= bar >> sprd_1:
lst_c cargs_15, sprd_1 >> cargs_16:
af callee_6, cargs_16 >> mex_6:
= foo >> callee_7:
lst >> cargs_17:
= bar >> sprd_2:
lst_c cargs_17, sprd_2 >> cargs_18:
= spam >> arg_7:
lst_a cargs_18, arg_7 >> cargs_19:
af callee_7, cargs_19 >> mex_7:
lst >> drctvs_0:
mod exports_0, drctvs_0 >> mod_0:"
`;

exports[`call compiles as partial 1`] = `
Expand All @@ -97,32 +85,32 @@ rec >> exports_0:
cc ret_1, pfn_1
>> mex_1:
fn args_2, ret_2:
lst_h args_2 >> prtl_3:
lst_h args_2 >> sprtl_0:
= foo >> callee_2:
lst >> cargs_5:
= prtl_3 >> sprd_0:
= sprtl_0 >> sprd_0:
lst_c cargs_5, sprd_0 >> cargs_6:
af callee_2, cargs_6 >> pfn_2:
cc ret_2, pfn_2
>> mex_2:
fn args_3, ret_3:
lst_h args_3 >> prtl_4:
lst_h args_3 >> sprtl_1:
= foo >> callee_3:
lst >> cargs_7:
= prtl_4 >> left_0:
= sprtl_1 >> left_0:
str 'bar' >> key_0
rec_g left_0, key_0 >> sprd_1:
lst_c cargs_7, sprd_1 >> cargs_8:
af callee_3, cargs_8 >> pfn_3:
cc ret_3, pfn_3
>> mex_3:
fn args_4, ret_4:
lst_h args_4 >> prtl_5:
lst_h args_4 >> prtl_3:
= foo >> callee_4:
lst >> cargs_9:
= prtl_5 >> arg_3:
= prtl_3 >> arg_3:
lst_a cargs_9, arg_3 >> cargs_10:
= prtl_5 >> left_1:
= prtl_3 >> left_1:
str 'bar' >> key_1
rec_g left_1, key_1 >> sprd_2:
lst_c cargs_10, sprd_2 >> cargs_11:
Expand Down
6 changes: 3 additions & 3 deletions src/ir/conditionals/match.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ transform_exprs = fn [expr=false, ...rest], ctx, out, last_id:



ht = fn [items_id, items=[]], expr, ctx:
ht = fn [items_id, items], expr, ctx:
[head, head_id, tail_ctx] = lst_h items_id, 'itm', expr, ctx
[tail, tail_id, next_ctx] = lst_t items_id, expr, tail_ctx

Expand All @@ -67,7 +67,7 @@ transform_with_partial = fn expr, name, val_id, {partial_ident, ...ctx}:



match_any = fn [val_id, val=[]], expr, gen_true, else_id, ctx:
match_any = fn [val_id, val], expr, gen_true, else_id, ctx:
[match_id, cond_ctx] = unique_name 'match', expr, ctx

[val_cond, cond_id, true_ctx] = is_val val_id, 'cond', expr, cond_ctx
Expand All @@ -84,7 +84,7 @@ match_any = fn [val_id, val=[]], expr, gen_true, else_id, ctx:



match_eq = fn [val_id, val=[]], expr, gen_true, else_id, ctx:
match_eq = fn [val_id, val], expr, gen_true, else_id, ctx:
[match_id, right_ctx] = unique_name 'match', expr, ctx
[right, right_id, {has_partial, ...cond_ctx}] = transform_with_partial expr, 'val', val_id, right_ctx

Expand Down
3 changes: 2 additions & 1 deletion src/ir/context.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,12 @@ init_ref_counts = fn [expr=false, ...rest], ctx:

init_ctx = fn exprs, options={}:
ctx = rec:
...options
transformers: {}
runtime: {}
unique_ids: {}
values: empty
...options


pipe ctx:
init_ref_counts exprs, ?
Expand Down
22 changes: 9 additions & 13 deletions src/ir/func/init.test.fnk.snap
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,15 @@ rec >> exports_0:
lst_t tail_0 >> tail_1:
lst_h tail_1 >> c_0:
lst_t tail_1 >> tail_2:
fn args_1, ret_1:
lst_h args_1 >> prtl_0:
= shrub >> callee_0:
lst >> cargs_0:
= a_0 >> sprd_0:
lst_c cargs_0, sprd_0 >> cargs_1:
af callee_0, cargs_1 >> pfn_0:
cc ret_1, pfn_0
>> result_0:
= bar >> callee_1:
lst >> cargs_2:
af callee_1, cargs_2 >> result_1:
cc ret_0, result_1
= shrub >> callee_0:
lst >> cargs_0:
= a_0 >> sprd_0:
lst_c cargs_0, sprd_0 >> cargs_1:
af callee_0, cargs_1 >> result_0:
= bar >> callee_1:
lst >> cargs_2:
af callee_1, cargs_2 >> result_1:
cc ret_0, result_1
>> fun_0:
str 'fun' >> key_0
rec_s exports_0, key_0, fun_0 >> exports_1:
Expand Down
11 changes: 6 additions & 5 deletions src/ir/literals/list.test.fnk
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{fink2lir, fink2lir_o} = import '../../testing/generate.fnk'
{fink2lir} = import '../../testing/generate.fnk'
{skip, describe, it, expect, to_match_snapshot} = import '@fink/jest/test.fnk'


Expand Down Expand Up @@ -90,23 +90,24 @@ describe 'unpacking list', fn:


describe 'optimizations', fn:

it 'reuses item refs', fn:
expect
fink2lir_o '
fink2lir '
[a, [b, c]] = [1, [2, 3]]
foo = [b, a]
[z] = [1 + 2, 3 * 4]
[x, y] = foo
log a, b, c, foo, z, x, y
'
', {optimize: {refs: true, tails: true, unused: true}}
to_match_snapshot


it 'ignores empty concats', fn:
expect
fink2lir_o '
fink2lir '
foo = [...b]
ham = []
spam = [foo, ...ham]
'
', {optimize: {refs: true, tails: true, unused: true}}
to_match_snapshot
4 changes: 2 additions & 2 deletions src/ir/literals/list.test.fnk.snap
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ rec >> exports_0:
str 'spam' >> key_2
rec_s exports_2, key_2, lst_1 >> exports_3:
lst >> drctvs_0:
mod exports_3, drctvs_0 >> result_0:"
mod exports_3, drctvs_0 >> mod_0:"
`;

exports[`optimizations reuses item refs 1`] = `
Expand All @@ -126,7 +126,7 @@ rec >> exports_0:
lst_a cargs_6, item_3 >> cargs_7:
af log, cargs_7 >> mex_3:
lst >> drctvs_0:
mod exports_1, drctvs_0 >> result_0:"
mod exports_1, drctvs_0 >> mod_0:"
`;

exports[`unpacking list destructures recs 1`] = `
Expand Down
6 changes: 4 additions & 2 deletions src/ir/literals/record.test.fnk
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{describe, it, expect, to_match_snapshot} = import '@fink/jest/test.fnk'
{fink2lir, fink2lir_o} = import '../../testing/generate.fnk'
{fink2lir} = import '../../testing/generate.fnk'


describe 'record', fn:
Expand Down Expand Up @@ -122,6 +122,8 @@ describe 'unpacking record', fn:
describe 'rec optimizations', fn:
it 'optimizes key-refs', fn:
expect
fink2lir_o 'foo = {π, ƒ, foo-bar}'
fink2lir
'foo = {π, ƒ, foo-bar}'
{optimize: {refs: true, tails: true, unused: true}}
to_match_snapshot

2 changes: 1 addition & 1 deletion src/ir/literals/record.test.fnk.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ rec >> exports_0:
str 'foo' >> key_3
rec_s exports_0, key_3, foo_0 >> exports_1:
lst >> drctvs_0:
mod exports_1, drctvs_0 >> result_0:"
mod exports_1, drctvs_0 >> mod_0:"
`;

exports[`record compiles calculaged props 1`] = `
Expand Down
2 changes: 1 addition & 1 deletion src/ir/partial/init.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


transform_partial = fn expr, result, ctx:
{partial_ident=false} = ctx
{partial_ident} = ctx
[prt, , end_ctx] = let partial_ident, result, expr, {...ctx, has_partial: true}
[prt, end_ctx]

Expand Down
6 changes: 3 additions & 3 deletions src/js/call/call.fnk
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
types = import '@babel/types'
{
callExpression, breakStatement, assignmentExpression
expressionStatement, returnStatement, spreadElement
expressionStatement, returnStatement
identifier, continueStatement, variableDeclaration, variableDeclarator
} = types
{null} = import '@fink/js-interop/nullish.fnk'
Expand All @@ -22,8 +22,8 @@ as_call_args = fn args:
null: identifier 'undefined'
else: arg
[...?]
else:
[spreadElement args]
# else:
# [spreadElement args]



Expand Down
3 changes: 3 additions & 0 deletions src/js/call/call.test.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ describe 'call', fn:




describe 'tail call elimination', fn:
it 'compiles to while loop', fn:
expect
Expand All @@ -85,3 +86,5 @@ describe 'tail call elimination', fn:
else: b
'
to_match_snapshot


Loading

0 comments on commit 33985ab

Please sign in to comment.