Skip to content

Commit

Permalink
fix: partial
Browse files Browse the repository at this point in the history
  • Loading branch information
kollhof committed Sep 30, 2021
1 parent 2cb1961 commit a2793bb
Show file tree
Hide file tree
Showing 17 changed files with 207 additions and 205 deletions.
2 changes: 2 additions & 0 deletions src/generate.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
115 changes: 36 additions & 79 deletions src/ir/call/call.fnk
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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



Expand All @@ -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]


Expand All @@ -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}]


84 changes: 42 additions & 42 deletions src/ir/call/call.test.fnk.snap
Original file line number Diff line number Diff line change
Expand Up @@ -68,83 +68,83 @@ 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:
id (fn args_1, ret_1: #fn
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:
af callee_3, cargs_8, fn pfn_3:
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:
af callee_4, cargs_11, fn pfn_4:
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:
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down
12 changes: 6 additions & 6 deletions src/ir/call/pipe.fnk
Original file line number Diff line number Diff line change
@@ -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'



Expand All @@ -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]

Expand Down
Loading

0 comments on commit a2793bb

Please sign in to comment.