From a2793bb11fe2417ebdf8c31de6a0acb39507bb4f Mon Sep 17 00:00:00 2001 From: Jan Klaas Kollhof Date: Thu, 30 Sep 2021 11:26:44 +0200 Subject: [PATCH] fix: partial --- src/generate.fnk | 2 + src/ir/call/call.fnk | 115 ++++++++---------------- src/ir/call/call.test.fnk.snap | 84 ++++++++--------- src/ir/call/pipe.fnk | 12 +-- src/ir/call/pipe.test.fnk.snap | 24 ++--- src/ir/conditionals/match.fnk | 8 +- src/ir/conditionals/match.test.fnk.snap | 28 +++--- src/ir/context.fnk | 2 +- src/ir/partial/init.fnk | 13 ++- src/js/call/call.test.fnk | 3 +- src/js/call/call.test.fnk.snap | 56 ++++++------ src/js/call/pipe.test.fnk | 11 +++ src/js/call/pipe.test.fnk.snap | 25 ++++-- src/js/context.fnk | 2 +- src/js/init.fnk | 16 ++++ src/js/module/init.fnk | 5 +- src/optimize/refs.fnk | 6 +- 17 files changed, 207 insertions(+), 205 deletions(-) diff --git a/src/generate.fnk b/src/generate.fnk index 78760c3..1e59e74 100644 --- a/src/generate.fnk +++ b/src/generate.fnk @@ -12,6 +12,8 @@ babel_traverse = import '@babel/traverse' {transform_async} = import './js/async/init.fnk' +# try_catch_ = fn f: [false, f _] + transform_file = fn fink_ast, code, filename, {optimize: optim, ...options}: opts = {...options, optimize: {refs: true, tails: true, unused: true, ...optim}} diff --git a/src/ir/call/call.fnk b/src/ir/call/call.fnk index b24203d..ca28097 100644 --- a/src/ir/call/call.fnk +++ b/src/ir/call/call.fnk @@ -1,7 +1,7 @@ {is_empty} = import '@fink/std-lib/iter.fnk' -{transform} = import '../transform.fnk' {unique_or_id, ir_fn} = import '../context.fnk' +{transform} = import '../transform.fnk' {lst, lst_a, lst_c} = import '../literals/list.fnk' {lst_h} = import '../assignment/init.fnk' {fnc} = import '../func/init.fnk' @@ -33,70 +33,40 @@ spread_arg = fn args_id, arg, arg_id, {loc}, ctx: -set_partial_ident = fn name, {loc}, ctx: - [partial_ident, next_ctx] = unique_or_id name, {loc}, ctx - [partial_ident, {...next_ctx, partial_ident}] - - +transform_with_partial = fn expr, name, ctx: + {partial_ident: outer_prtl=false} = ctx -maybe_set_partial_ident = fn name, {loc}, {has_partial: _, partial_ident=false, ...ctx}: - match partial_ident: + [arg, arg_id, next_ctx] = match outer_prtl: false: - [true, ...set_partial_ident name, {loc}, ctx] + transform expr, name, ctx else: - [false, partial_ident, {...ctx, partial_ident}] - - - -transform_with_partial = fn expr, name, ctx: - [foo, prtl, foo_ctx] = maybe_set_partial_ident 'prtl', expr, ctx + transform expr, name, {...ctx, partial_ident: false} - [arg, arg_id, {has_partial, ...next_ctx}] = transform expr, name, foo_ctx + {partial_ident: arg_prtl=false} = next_ctx - match true: - foo and has_partial: - prtl_func prtl, arg, arg_id, 'pfn', {loc: expr.loc}, next_ctx + match [outer_prtl, arg_prtl]: + [false, ? != false]: + prtl_func arg_prtl, arg, arg_id, 'pfn', expr, {...next_ctx, partial_ident: false} else: - [arg, arg_id, next_ctx] + [arg, arg_id, {...next_ctx, partial_ident: outer_prtl}] transform_arg = fn expr, args_id, ctx: - [arg, arg_id, {has_partial: _, ...next_ctx}] = transform_with_partial expr, 'arg', ctx - add_arg args_id, arg, arg_id, {loc: expr.loc}, next_ctx + [arg, arg_id, next_ctx] = transform_with_partial expr, 'arg', ctx + add_arg args_id, arg, arg_id, expr, next_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_or_id 'sprtl', expr, ctx - else: [partial_foo, ctx] - - 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} +transform_spread_arg = fn expr, args_id, ctx: + [arg, arg_id, next_ctx] = transform expr.right, 'sprd', ctx + spread_arg args_id, arg, arg_id, expr, next_ctx -transform_partial_arg = fn expr, args_id, {partial_foo=false, has_partial, partial_ident=false, ...ctx}: - [prlt, p_ctx] = match partial_foo: - false: unique_or_id 'prtl', expr, ctx - else: [partial_foo, ctx] - - match partial_ident: - false: - foo_ctx = {...p_ctx, partial_ident: prlt} - [arg, arg_id, {partial_ident: _, ...next_ctx}] = transform expr, 'arg', foo_ctx - add_arg args_id, arg, arg_id, {loc: expr.loc}, {...next_ctx, has_partial, partial_ident, partial_foo: prlt} - - else: - [a, b, c] = transform_arg expr, args_id, {...p_ctx, partial_ident, has_partial} - [a, b, {...c, has_partial: true}] +transform_partial_arg = fn expr, args_id, ctx: + [arg, arg_id, next_ctx] = transform expr, 'parg', ctx + add_arg args_id, arg, arg_id, expr, next_ctx @@ -109,21 +79,17 @@ transform_all_args = fn [expr=false, ...exprs], args_id, ctx, out=[]: [arg, next_args_id, next_ctx] = transform_spread_arg expr, args_id, ctx transform_all_args exprs, next_args_id, next_ctx, [...out, ...arg] + {type: 'partial'}: + [arg, next_args_id, next_ctx] = transform_partial_arg expr, args_id, ctx + transform_all_args exprs, next_args_id, next_ctx, [...out, ...arg] + {type: 'empty'}: # TODO: have a transform for empty [arg, next_args_id, next_ctx] = add_arg args_id, [], {i: '_', loc: expr.loc}, expr, ctx transform_all_args exprs, next_args_id, next_ctx, [...out, ...arg] - {type: 'partial'}: - [arg, next_args_id, next_ctx] = transform_partial_arg expr, args_id, ctx - transform_all_args exprs, next_args_id, next_ctx, [...out, ...arg] - else: - [arg, next_args_id, arg_ctx] = transform_arg expr, args_id, ctx - next_ctx = rec: - ...arg_ctx - partial_ident: ctx.partial_ident - has_partial: ctx.has_partial or arg_ctx.has_partial + [arg, next_args_id, next_ctx] = transform_arg expr, args_id, ctx transform_all_args exprs, next_args_id, next_ctx, [...out, ...arg] @@ -140,32 +106,23 @@ transform_args = fn expr, ctx: -transform_callee = fn expr, ctx: - [foo, prtl, foo_ctx] = maybe_set_partial_ident 'prtlc', expr, ctx - [callee, callee_id, {has_partial: hp, ...next_ctx}] = transform expr, 'callee', foo_ctx - - match true: - foo and hp: - [callee, callee_id, {...next_ctx, partial_foo: prtl, has_partial: true, partial_ident: ctx.partial_ident}] - else: - [callee, callee_id, {...next_ctx, has_partial: ctx.has_partial or hp, partial_ident: ctx.partial_ident}] - +transform_call = fn node, result, ctx: + {partial_ident: outer_prtl=false} = ctx + [callee, c_id, args_ctx] = transform node.callee, 'callee', ctx + [args, args_id, next_ctx] = transform_args node, args_ctx -transform_call = fn node, result, ctx: - [callee, c_id, args_ctx] = transform_callee node.callee, ctx - [args, args_id, {partial_foo=false, ...next_ctx}] = transform_args node, args_ctx + {partial_ident: call_prtl=false} = next_ctx - match partial_foo: - false: - [appl_fn, , end_ctx] = af c_id, args_id, result, node, next_ctx + match [outer_prtl, call_prtl]: + [false, ? != false]: + [appl_fn, res_id, fn_ctx] = af c_id, args_id, 'pfn', node, next_ctx call = [...callee, ...args, ...appl_fn] - [call, end_ctx] + [fun, , end_ctx] = prtl_func call_prtl, call, res_id, result, node, fn_ctx + [fun, {...end_ctx, partial_ident: outer_prtl}] else: - [appl_fn, res_id, foo_ctx] = af c_id, args_id, 'pfn', node, next_ctx + [appl_fn, , end_ctx] = af c_id, args_id, result, node, next_ctx call = [...callee, ...args, ...appl_fn] - [fun, , end_ctx] = prtl_func partial_foo, call, res_id, result, node, foo_ctx - [fun, end_ctx] - + [call, {...end_ctx, partial_ident: outer_prtl}] diff --git a/src/ir/call/call.test.fnk.snap b/src/ir/call/call.test.fnk.snap index 4ae4818..cb83308 100644 --- a/src/ir/call/call.test.fnk.snap +++ b/src/ir/call/call.test.fnk.snap @@ -68,8 +68,8 @@ rec_e fn exports_0: lst_h args_0, fn prtl_0: id foo, fn callee_0: lst_e fn cargs_0: - id prtl_0, fn arg_0: - lst_a cargs_0, arg_0, fn cargs_1: + id prtl_0, fn parg_0: + lst_a cargs_0, parg_0, fn cargs_1: af callee_0, cargs_1, fn pfn_0: cc ret_0, pfn_0 ), fn mex_0: @@ -77,27 +77,27 @@ rec_e fn exports_0: lst_h args_1, fn prtl_1: id foo, fn callee_1: lst_e fn cargs_2: - id prtl_1, fn arg_1: - lst_a cargs_2, arg_1, fn cargs_3: - int '123', fn arg_2: - lst_a cargs_3, arg_2, fn cargs_4: + id prtl_1, fn parg_1: + lst_a cargs_2, parg_1, fn cargs_3: + int '123', fn arg_0: + lst_a cargs_3, arg_0, fn cargs_4: af callee_1, cargs_4, fn pfn_1: cc ret_1, pfn_1 ), fn mex_1: id (fn args_2, ret_2: #fn - lst_h args_2, fn sprtl_0: + lst_h args_2, fn prtl_2: id foo, fn callee_2: lst_e fn cargs_5: - id sprtl_0, fn sprd_0: + id prtl_2, fn sprd_0: lst_c cargs_5, sprd_0, fn cargs_6: af callee_2, cargs_6, fn pfn_2: cc ret_2, pfn_2 ), fn mex_2: id (fn args_3, ret_3: #fn - lst_h args_3, fn sprtl_1: + lst_h args_3, fn prtl_3: id foo, fn callee_3: lst_e fn cargs_7: - id sprtl_1, fn left_0: + id prtl_3, fn left_0: str 'bar', fn key_0: rec_g left_0, key_0, fn sprd_1: lst_c cargs_7, sprd_1, fn cargs_8: @@ -105,12 +105,12 @@ rec_e fn exports_0: cc ret_3, pfn_3 ), fn mex_3: id (fn args_4, ret_4: #fn - lst_h args_4, fn prtl_3: + lst_h args_4, fn prtl_4: id foo, fn callee_4: lst_e fn cargs_9: - id prtl_3, fn arg_3: - lst_a cargs_9, arg_3, fn cargs_10: - id prtl_3, fn left_1: + id prtl_4, fn parg_2: + lst_a cargs_9, parg_2, fn cargs_10: + id prtl_4, fn left_1: str 'bar', fn key_1: rec_g left_1, key_1, fn sprd_2: lst_c cargs_10, sprd_2, fn cargs_11: @@ -118,33 +118,33 @@ rec_e fn exports_0: cc ret_4, pfn_4 ), fn mex_4: id (fn args_5, ret_5: #fn - lst_h args_5, fn prtlc_5: - id prtlc_5, fn callee_5: + lst_h args_5, fn prtl_5: + id prtl_5, fn callee_5: lst_e fn cargs_12: - int '123', fn arg_4: - lst_a cargs_12, arg_4, fn cargs_13: + int '123', fn arg_1: + lst_a cargs_12, arg_1, fn cargs_13: af callee_5, cargs_13, fn pfn_5: cc ret_5, pfn_5 ), fn mex_5: id (fn args_6, ret_6: #fn - lst_h args_6, fn prtlc_6: - id prtlc_6, fn callee_6: + lst_h args_6, fn prtl_6: + id prtl_6, fn callee_6: lst_e fn cargs_14: - id bar, fn arg_5: - lst_a cargs_14, arg_5, fn cargs_15: - id spam, fn arg_6: - lst_a cargs_15, arg_6, fn cargs_16: + id bar, fn arg_2: + lst_a cargs_14, arg_2, fn cargs_15: + id spam, fn arg_3: + lst_a cargs_15, arg_3, fn cargs_16: af callee_6, cargs_16, fn pfn_6: cc ret_6, pfn_6 ), fn mex_6: id (fn args_7, ret_7: #fn - lst_h args_7, fn prtlc_7: - id prtlc_7, fn left_2: + lst_h args_7, fn prtl_7: + id prtl_7, fn left_2: str 'bar', fn key_2: rec_g left_2, key_2, fn callee_7: lst_e fn cargs_17: - id spam, fn arg_7: - lst_a cargs_17, arg_7, fn cargs_18: + id spam, fn arg_4: + lst_a cargs_17, arg_4, fn cargs_18: af callee_7, cargs_18, fn pfn_7: cc ret_7, pfn_7 ), fn mex_7: @@ -173,8 +173,8 @@ rec_e fn exports_0: id prtl_1, fn left_1: id foo, fn callee_2: lst_e fn cargs_3: - id prtl_1, fn arg_2: - lst_a cargs_3, arg_2, fn cargs_4: + id prtl_1, fn parg_0: + lst_a cargs_3, parg_0, fn cargs_4: af callee_2, cargs_4, fn right_1: or left_1, right_1, fn arg_1: cc ret_1, arg_1 @@ -184,34 +184,34 @@ rec_e fn exports_0: id filter, fn callee_3: lst_e fn cargs_6: id (fn args_2, ret_2: #fn - lst_h args_2, fn prtl_3: - id prtl_3, fn right_2: - not right_2, fn arg_3: - cc ret_2, arg_3 + lst_h args_2, fn prtl_2: + id prtl_2, fn right_2: + not right_2, fn arg_2: + cc ret_2, arg_2 ), fn pfn_2: lst_a cargs_6, pfn_2, fn cargs_7: af callee_3, cargs_7, fn mex_2: id map, fn callee_4: lst_e fn cargs_8: id (fn args_3, ret_3: #fn - lst_h args_3, fn prtl_4: - id prtl_4, fn left_2: + lst_h args_3, fn prtl_3: + id prtl_3, fn left_2: str 'foo', fn key_0: - rec_g left_2, key_0, fn arg_4: - cc ret_3, arg_4 + rec_g left_2, key_0, fn arg_3: + cc ret_3, arg_3 ), fn pfn_3: lst_a cargs_8, pfn_3, fn cargs_9: af callee_4, cargs_9, fn mex_3: id map, fn callee_5: lst_e fn cargs_10: id (fn args_4, ret_4: #fn - lst_h args_4, fn prtl_5: - id prtl_5, fn left_4: + lst_h args_4, fn prtl_4: + id prtl_4, fn left_4: int '2', fn right_3: rem left_4, right_3, fn left_3: int '0', fn right_4: - eq left_3, right_4, fn arg_5: - cc ret_4, arg_5 + eq left_3, right_4, fn arg_4: + cc ret_4, arg_4 ), fn pfn_4: lst_a cargs_10, pfn_4, fn cargs_11: af callee_5, cargs_11, fn mex_4: diff --git a/src/ir/call/pipe.fnk b/src/ir/call/pipe.fnk index 486b911..7fef0e6 100644 --- a/src/ir/call/pipe.fnk +++ b/src/ir/call/pipe.fnk @@ -1,7 +1,7 @@ {transform} = import '../transform.fnk' -{transform_with_partial, af} = import './call.fnk' {lst, lst_a} = import '../literals/list.fnk' {let} = import '../identifier/init.fnk' +{transform_with_partial, af} = import './call.fnk' @@ -25,14 +25,14 @@ pipe_all = fn [expr=false, ...exprs], prev_res_id, ctx, out=[]: false: [out, prev_res_id, ctx] - {type: ? in ['ident', 'call']}: - [callee, callee_id, next_ctx] = transform expr, 'pfn', ctx - [call, res_id, end_ctx] = apply_step callee_id, prev_res_id, expr, next_ctx - pipe_all exprs, res_id, end_ctx, [...out, ...callee, ...call] + # {type: ? in ['ident', 'call']}: + # [callee, callee_id, next_ctx] = transform expr, 'pfn', ctx + # [call, res_id, end_ctx] = apply_step callee_id, prev_res_id, expr, next_ctx + # pipe_all exprs, res_id, end_ctx, [...out, ...callee, ...call] else: [callee, callee_id, step_ctx] = transform_with_partial expr, 'pfn', ctx - {has_partial: _, partial_ident: _, ...next_ctx} = step_ctx + {partial_ident: _, ...next_ctx} = step_ctx [call, res_id, end_ctx] = apply_step callee_id, prev_res_id, expr, next_ctx pipe_all exprs, res_id, end_ctx, [...out, ...callee, ...call] diff --git a/src/ir/call/pipe.test.fnk.snap b/src/ir/call/pipe.test.fnk.snap index bf237c6..232bbdf 100644 --- a/src/ir/call/pipe.test.fnk.snap +++ b/src/ir/call/pipe.test.fnk.snap @@ -64,10 +64,10 @@ rec_e fn exports_0: lst_h args_0, fn prtl_0: id bar, fn callee_0: lst_e fn cargs_0: - id prtl_0, fn arg_0: - lst_a cargs_0, arg_0, fn cargs_1: - int '123', fn arg_1: - lst_a cargs_1, arg_1, fn cargs_2: + id prtl_0, fn parg_0: + lst_a cargs_0, parg_0, fn cargs_1: + int '123', fn arg_0: + lst_a cargs_1, arg_0, fn cargs_2: af callee_0, cargs_2, fn pfn_1: cc ret_0, pfn_1 ), fn pfn_0: @@ -75,20 +75,20 @@ rec_e fn exports_0: af pfn_0, args_1, fn ppr_0: id spam, fn callee_1: lst_e fn cargs_3: - id ni, fn arg_2: - lst_a cargs_3, arg_2, fn cargs_4: + id ni, fn arg_1: + lst_a cargs_3, arg_1, fn cargs_4: af callee_1, cargs_4, fn pfn_2: lst_e fn args_2: lst_a args_2, ppr_0, fn args_3: af pfn_2, args_3, fn ppr_1: id (fn args_4, ret_1: #fn - lst_h args_4, fn prtl_3: + lst_h args_4, fn prtl_1: lst_e fn lst_2: int '4', fn item_1: lst_a lst_2, item_1, fn lst_1: int '5', fn item_0: lst_a lst_1, item_0, fn lst_0: - id prtl_3, fn items_0: + id prtl_1, fn items_0: lst_c lst_0, items_0, fn pfn_3: cc ret_1, pfn_3 ), fn pfn_4: @@ -98,11 +98,11 @@ rec_e fn exports_0: id shrub, fn callee_2: lst_e fn cargs_5: id (fn args_7, ret_2: #fn - lst_h args_7, fn prtl_4: - id prtl_4, fn left_0: + lst_h args_7, fn prtl_2: + id prtl_2, fn left_0: int '1', fn right_0: - gt left_0, right_0, fn arg_3: - cc ret_2, arg_3 + gt left_0, right_0, fn arg_2: + cc ret_2, arg_2 ), fn pfn_6: lst_a cargs_5, pfn_6, fn cargs_6: af callee_2, cargs_6, fn pfn_5: diff --git a/src/ir/conditionals/match.fnk b/src/ir/conditionals/match.fnk index 8c6775b..07af853 100644 --- a/src/ir/conditionals/match.fnk +++ b/src/ir/conditionals/match.fnk @@ -2,7 +2,7 @@ {reverse} = import '@fink/std-lib/iter.fnk' {transform} = import '../transform.fnk' -{unique_or_id, ir_fn} = import '../context.fnk' +{unique_or_id, ir_fn, get_refs} = import '../context.fnk' {transform_key, members_as_rec} = import '../literals/record.fnk' {cc} = import '../func/init.fnk' @@ -83,10 +83,10 @@ match_any = fn [val_id, val], expr, gen_true, else_id, ctx: match_eq = fn [val_id, val], expr, gen_true, else_id, ctx: - [right, right_id, {has_partial, ...cond_ctx}] = transform_with_partial expr, 'val', val_id, ctx + [right, right_id, cond_ctx] = transform_with_partial expr, 'val', val_id, ctx - left_id = match has_partial: - true: {i: 'true'} + left_id = match get_refs val_id, cond_ctx: + ? > 0: {i: 'true'} else: val_id [eq_cond, cond_id, true_ctx] = is_eq left_id, right_id, 'cond', expr, cond_ctx diff --git a/src/ir/conditionals/match.test.fnk.snap b/src/ir/conditionals/match.test.fnk.snap index 1ebad53..a9a7918 100644 --- a/src/ir/conditionals/match.test.fnk.snap +++ b/src/ir/conditionals/match.test.fnk.snap @@ -402,8 +402,8 @@ rec_e fn exports_0: int '1', fn left_0: id length, fn callee_0: lst_e fn cargs_0: - id tail_0, fn arg_0: - lst_a cargs_0, arg_0, fn cargs_1: + id tail_0, fn parg_0: + lst_a cargs_0, parg_0, fn cargs_1: af callee_0, cargs_1, fn right_0: lt left_0, right_0, fn val_1: eq true, val_1, fn cond_1: @@ -426,8 +426,8 @@ rec_e fn exports_0: lst_t value_0, fn tail_1: id is_empty, fn callee_1: lst_e fn cargs_2: - id tail_1, fn arg_1: - lst_a cargs_2, arg_1, fn cargs_3: + id tail_1, fn parg_1: + lst_a cargs_2, parg_1, fn cargs_3: af callee_1, cargs_3, fn val_3: eq true, val_3, fn cond_3: id (fn: #cn @@ -445,10 +445,10 @@ rec_e fn exports_0: lst_h value_0, fn itm_2: id is_foo, fn callee_2: lst_e fn cargs_4: - id itm_2, fn arg_2: - lst_a cargs_4, arg_2, fn cargs_5: - id bar, fn arg_3: - lst_a cargs_5, arg_3, fn cargs_6: + id itm_2, fn parg_2: + lst_a cargs_4, parg_2, fn cargs_5: + id bar, fn arg_0: + lst_a cargs_5, arg_0, fn cargs_6: af callee_2, cargs_6, fn val_4: eq true, val_4, fn cond_4: id (fn: #cn @@ -651,8 +651,8 @@ rec_e fn exports_0: ), fn match_0: id shrub, fn callee_0: lst_e fn cargs_0: - id value_0, fn arg_0: - lst_a cargs_0, arg_0, fn cargs_1: + id value_0, fn parg_0: + lst_a cargs_0, parg_0, fn cargs_1: af callee_0, cargs_1, fn val_0: eq true, val_0, fn cond_1: id (fn: #cn @@ -1113,8 +1113,8 @@ rec_e fn exports_0: rec_d value_0, key_2, fn spread_1: id is_empty, fn callee_0: lst_e fn cargs_0: - id spread_1, fn arg_0: - lst_a cargs_0, arg_0, fn cargs_1: + id spread_1, fn parg_0: + lst_a cargs_0, parg_0, fn cargs_1: af callee_0, cargs_1, fn val_3: eq true, val_3, fn cond_4: id (fn: #cn @@ -1133,8 +1133,8 @@ rec_e fn exports_0: rec_g value_0, key_3, fn itm_3: id is_foo, fn callee_1: lst_e fn cargs_2: - id itm_3, fn arg_1: - lst_a cargs_2, arg_1, fn cargs_3: + id itm_3, fn parg_1: + lst_a cargs_2, parg_1, fn cargs_3: af callee_1, cargs_3, fn val_4: eq true, val_4, fn cond_5: id (fn: #cn diff --git a/src/ir/context.fnk b/src/ir/context.fnk index f42da9d..728d71d 100644 --- a/src/ir/context.fnk +++ b/src/ir/context.fnk @@ -81,7 +81,7 @@ update_value = fn id, updates, ctx: get_refs = fn id, ctx: - {refs} = get_value id, ctx + {refs=0} = get_value id, ctx refs diff --git a/src/ir/partial/init.fnk b/src/ir/partial/init.fnk index 01e015e..b7b0902 100644 --- a/src/ir/partial/init.fnk +++ b/src/ir/partial/init.fnk @@ -1,12 +1,19 @@ -{add} = import '../context.fnk' +{add, inc_ref, unique_or_id} = import '../context.fnk' {let} = import '../identifier/init.fnk' transform_partial = fn expr, result, ctx: - {partial_ident} = ctx - [prt, , end_ctx] = let partial_ident, result, expr, {...ctx, has_partial: true} + {partial_ident=false} = ctx + + [prtl, prtl_ctx] = match partial_ident: + false: unique_or_id 'prtl', expr, ctx + else: [partial_ident, ctx] + + next_ctx = inc_ref prtl, prtl_ctx + + [prt, , end_ctx] = let prtl, result, expr, {...next_ctx, partial_ident: prtl} [prt, end_ctx] diff --git a/src/js/call/call.test.fnk b/src/js/call/call.test.fnk index 52d5ddc..060b84c 100644 --- a/src/js/call/call.test.fnk +++ b/src/js/call/call.test.fnk @@ -33,7 +33,8 @@ describe 'call', fn: p1 = foo ...? p2 = foo ...?.bar p3 = foo ...?.bar.spam - p4= foo ...bar ? + # TODO p4 = foo ...bar ? + p4 = fn prtl: foo ...bar prtl p7 = ? 123 p8 = ? bar, spam p9 = ?.bar spam diff --git a/src/js/call/call.test.fnk.snap b/src/js/call/call.test.fnk.snap index 65d4602..9fc1d92 100644 --- a/src/js/call/call.test.fnk.snap +++ b/src/js/call/call.test.fnk.snap @@ -33,16 +33,16 @@ export const p1 = p1_0, `; exports[`call compiles with partial callee 1`] = ` -"const p1_0 = prtlc_0 => { - return prtlc_0(123); +"const p1_0 = prtl_0 => { + return prtl_0(123); }; -const p2_0 = prtlc_1 => { - return prtlc_1(bar, spam); +const p2_0 = prtl_1 => { + return prtl_1(bar, spam); }; -const p3_0 = prtlc_2 => { - return prtlc_2.bar(spam); +const p3_0 = prtl_2 => { + return prtl_2.bar(spam); }; export const p1 = p1_0, @@ -57,17 +57,17 @@ exports[`call compiles with partial exprs as args 1`] = ` const pa2_0 = filter(prtl_1 => { return prtl_1 || foo(prtl_1); }); -const pa3_0 = filter(prtl_3 => { - return !prtl_3; +const pa3_0 = filter(prtl_2 => { + return !prtl_2; }); -const pa4_0 = map(prtl_4 => { - return prtl_4.foo; +const pa4_0 = map(prtl_3 => { + return prtl_3.foo; }); -const pa5_0 = map(prtl_5 => { - return prtl_5 % 2 === 0; +const pa5_0 = map(prtl_4 => { + return prtl_4 % 2 === 0; }); -const pa6_0 = foo(bar, prtl_7 => { - return spam === prtl_7; +const pa6_0 = foo(bar, prtl_5 => { + return spam === prtl_5; }); export const pa1 = pa1_0, pa2 = pa2_0, @@ -78,32 +78,32 @@ export const pa1 = pa1_0, `; exports[`call compiles with partial spread arg 1`] = ` -"const p1_0 = sprtl_0 => { - return foo(...sprtl_0); +"const p1_0 = prtl_0 => { + return foo(...prtl_0); }; -const p2_0 = sprtl_1 => { - return foo(...sprtl_1.bar); +const p2_0 = prtl_1 => { + return foo(...prtl_1.bar); }; -const p3_0 = sprtl_2 => { - return foo(...sprtl_2.bar.spam); +const p3_0 = prtl_2 => { + return foo(...prtl_2.bar.spam); }; -const p4_0 = sprtl_3 => { - return foo(...bar(sprtl_3)); +const p4_0 = prtl_3 => { + return foo(...bar(prtl_3)); }; -const p7_0 = prtlc_4 => { - return prtlc_4(123); +const p7_0 = prtl_4 => { + return prtl_4(123); }; -const p8_0 = prtlc_5 => { - return prtlc_5(bar, spam); +const p8_0 = prtl_5 => { + return prtl_5(bar, spam); }; -const p9_0 = prtlc_6 => { - return prtlc_6.bar(spam); +const p9_0 = prtl_6 => { + return prtl_6.bar(spam); }; export const p1 = p1_0, diff --git a/src/js/call/pipe.test.fnk b/src/js/call/pipe.test.fnk index 0b7b885..b877a71 100644 --- a/src/js/call/pipe.test.fnk +++ b/src/js/call/pipe.test.fnk @@ -21,6 +21,17 @@ describe 'pipe', fn: ' to_match_snapshot +# TODO: move or copy to ir/ +describe 'TODO', fn: + it 'compiles', fn: + expect + fink2js ' + foo = bar fn: + pipe: + spam + ?.shrub 123 + ' + to_match_snapshot describe 'small pipe |', fn: diff --git a/src/js/call/pipe.test.fnk.snap b/src/js/call/pipe.test.fnk.snap index a94bfa3..bb65d8c 100644 --- a/src/js/call/pipe.test.fnk.snap +++ b/src/js/call/pipe.test.fnk.snap @@ -1,18 +1,27 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`TODO compiles 1`] = ` +"const foo_0 = bar(() => { + return (prtl_0 => { + return prtl_0.shrub(123); + })(spam()); +}); +export const foo = foo_0;" +`; + exports[`pipe compiles 1`] = ` -"const ppr_5 = (prtl_4 => { - return [4, 5, ...prtl_4]; -})((prtl_3 => { - return prtl_3.nini; -})((prtlc_1 => { - return prtlc_1.spam(456); +"const ppr_5 = (prtl_3 => { + return [4, 5, ...prtl_3]; +})((prtl_2 => { + return prtl_2.nini; +})((prtl_1 => { + return prtl_1.spam(456); })(spam((prtl_0 => { return bar(prtl_0, 123); })(foo()))))); -const ppr_7 = (prtl_6 => { - return [...prtl_6]; +const ppr_7 = (prtl_4 => { + return [...prtl_4]; })(map(item_5 => { return item_5 * 2; })([1, 2, 3])); diff --git a/src/js/context.fnk b/src/js/context.fnk index e5186e6..913b0ed 100644 --- a/src/js/context.fnk +++ b/src/js/context.fnk @@ -19,6 +19,7 @@ get_js = fn id, ctx: val = get_value id, ctx match val: {ignore_refs: true}: with_loc id, val.js + # {inline: false}: ident id {refs: ? > 1}: ident id {js: ?}: with_loc id, val.js else: ident id @@ -35,7 +36,6 @@ get_js_literal = fn id, ctx: - add = fn name, transformer: fn {transformers, ...ctx}: next_transformers = rec: ...transformers diff --git a/src/js/init.fnk b/src/js/init.fnk index 117425b..dc180d1 100644 --- a/src/js/init.fnk +++ b/src/js/init.fnk @@ -101,6 +101,21 @@ prepare_imports = fn [expr=false, ...exprs], ctx: +prepare_calls = fn [expr=false, ...exprs], ctx: + match expr: + false: + ctx + + [{f: 'af'}]: + [, [res_id]] = expr + next_ctx = update_value res_id, {inline: false}, ctx + prepare_calls exprs, next_ctx + + else: + prepare_calls exprs, ctx + + + transform_ir = fn input_exprs, options: exprs = replace_idents input_exprs rev_expr = reverse exprs @@ -110,6 +125,7 @@ transform_ir = fn input_exprs, options: prepare_rets rev_expr, ? prepare_imports rev_expr, ? prepare_exports rev_expr, ? + prepare_calls rev_expr, ? add_transformers ? transfrom_module exprs, ctx diff --git a/src/js/module/init.fnk b/src/js/module/init.fnk index d0d6c5a..550a318 100644 --- a/src/js/module/init.fnk +++ b/src/js/module/init.fnk @@ -7,11 +7,11 @@ types = import '@babel/types' isImportDefaultSpecifier, isImportSpecifier, interpreterDirective } = types -{map, fold} = import '@fink/std-lib/iter.fnk' +{map} = import '@fink/std-lib/iter.fnk' {slice} = import '@fink/std-lib/str.fnk' -{add, get_js, set_js2, get_js_literal, with_loc} = import '../context.fnk' +{add, get_js, get_js_literal, with_loc} = import '../context.fnk' {transform_exprs} = import '../transform.fnk' {escape_ident} = import '../identifier/init.fnk' {get_runtime_imports} = import './import.fnk' @@ -137,7 +137,6 @@ transform_mod_exports = fn expr, ctx: - transfrom_module = fn exprs, ctx: [body, next_ctx] = transform_exprs exprs, ctx [..., last] = exprs diff --git a/src/optimize/refs.fnk b/src/optimize/refs.fnk index 0811dcf..d320a64 100644 --- a/src/optimize/refs.fnk +++ b/src/optimize/refs.fnk @@ -122,9 +122,9 @@ create_lst = fn [id=false, ...ids], lst_id, idx=0, out=[]: 0: lst_id else: {i: '${lst_id.i}_${idx}', loc: lst_id.loc} - foo_id = {i: '${lst_id.i}_${idx + 1}'} - foo = [{f: 'lst_a', args: [foo_id, id], loc: id.loc}, [res_id]] - create_lst ids, lst_id, idx + 1, [foo, ...out] + next_lst_id = {i: '${lst_id.i}_${idx + 1}'} + lst = [{f: 'lst_a', args: [next_lst_id, id], loc: id.loc}, [res_id]] + create_lst ids, lst_id, idx + 1, [lst, ...out]