Skip to content

Commit

Permalink
feat: treat lists as tuples, inline numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
kollhof committed Oct 6, 2021
1 parent d91c5c7 commit 203ee57
Show file tree
Hide file tree
Showing 28 changed files with 652 additions and 553 deletions.
24 changes: 11 additions & 13 deletions src/generate.test.fnk.snap
Original file line number Diff line number Diff line change
Expand Up @@ -45,33 +45,31 @@ export default ni_0;"
`;

exports[`source maps compiles to mjs 1`] = `
"const nanu_0 = 345;
const foo_0 = {
"const foo_0 = {
bar: 12345,
\\"spam ni\\": nanu_0,
nanu: nanu_0
\\"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={nanu_0} /></Foo>;
const el_0 = <Foo>bar {123} <div foo={134} bar={bar_0} spam={345} /></Foo>;
const ni_0 = spam + fooᜭbar;
const item_1 = 1;
const item_0 = \`ni\`;
const a_0 = [item_1, item_0];
const out_0 = [a_0, item_1, item_0];
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 + item_1;
return x_0 + y_0 + 1;
};
const y_1 = func_0(bar_0, a_0, item_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 = nanu_0,
export const nanu = 345,
foo = foo_0,
bar = bar_0,
ham = ham_0,
Expand All @@ -88,9 +86,9 @@ export const nanu = nanu_0,
\\"test.fnk\\"
],
\\"names\\": [
\\"nanu\\",
\\"foo\\",
\\"bar\\",
\\"nanu\\",
\\"ham\\",
\\"el\\",
\\"div\\",
Expand All @@ -106,7 +104,7 @@ export const nanu = nanu_0,
\\"y\\",
\\"r1\\"
],
\\"mappings\\": \\"AAAAA,MAAAA,MAAI,GAAG,GAAPA;AACAC,MAAAA,KAAG,GAAiC;AAA7BC,EAAAA,GAA6B,EAAxB,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and Up @@ -16,7 +16,11 @@ transform_exprs = fn [expr, ...exprs], res_id, ctx, out=[]:


transform_block = fn expr, res_id, ctx:
transform_exprs expr.exprs, res_id, ctx
{scopes} = ctx
block_ctx = {...ctx, scopes: [{}, ...scopes]}
[out, next_ctx] = transform_exprs expr.exprs, res_id, block_ctx
[out, {...next_ctx, scopes}]



add_block = fn ctx:
Expand Down
7 changes: 5 additions & 2 deletions src/ir/conditionals/match.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,12 @@ match_expr = fn val, expr, gen_true, else_id, ctx:


match_res_exprs = fn exprs, name, ret_id, {loc}, ctx:
[block, res_id, ret_ctx] = transform_exprs exprs, ctx, []
{scopes} = ctx
cond_ctx = {...ctx, scopes: [{}, ...scopes]}
[block, res_id, ret_ctx] = transform_exprs exprs, cond_ctx, []
[ret, , next_ctx] = cc ret_id, res_id, , {loc}, ret_ctx
cn [], [...block, ...ret], name, {loc}, next_ctx
[a, b, todo] = cn [], [...block, ...ret], name, {loc}, next_ctx
[a, b, {...todo, scopes}]



Expand Down
12 changes: 11 additions & 1 deletion src/ir/literals/list.test.fnk
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{fink2lir, fink2js} = import '../../testing/generate.fnk'
{skip, describe, it, expect, to_match_snapshot} = import '@fink/jest/test.fnk'
{fink2lir, fink2js} = import '../../testing/generate.fnk'


describe 'list', fn:
Expand Down Expand Up @@ -124,3 +124,13 @@ describe 'optimizations', fn:
spam = [foo, ...ham]
', {optimize: {refs: true, tails: true, unused: true}}
to_match_snapshot


it 'removes tails', fn:
expect
fink2lir '
[bar, spam, ham] = [...shrub]
ni = bar + spam + ham
', {optimize: {refs: true, tails: true, unused: true}}
to_match_snapshot

63 changes: 34 additions & 29 deletions src/ir/literals/list.test.fnk.snap
Original file line number Diff line number Diff line change
Expand Up @@ -87,53 +87,58 @@ rec_e fn exports_0:
exports[`optimizations ignores empty concats 1`] = `
"
rec_e fn exports_0:
lst_e fn lst_0:
tpl, fn lst_0:
lst_c lst_0, b, fn foo_0:
str 'foo', fn key_0:
rec_s exports_0, key_0, foo_0, fn exports_1:
lst_e fn ham_0:
tpl, fn ham_0:
str 'ham', fn key_1:
rec_s exports_1, key_1, ham_0, fn exports_2:
lst_e fn lst_2:
lst_a lst_2, foo_0, fn lst_1:
str 'spam', fn key_2:
rec_s exports_2, key_2, lst_1, fn exports_3:
lst_e fn drctvs_0:
mod exports_3, drctvs_0, fn mod_0:"
tpl foo_0, fn spam_0:
str 'spam', fn key_2:
rec_s exports_2, key_2, spam_0, fn exports_3:
tpl, fn drctvs_0:
mod exports_3, drctvs_0, fn mod_0:"
`;

exports[`optimizations optimizes spreads 1`] = `
"const y_0 = [1, 2];
const item_4 = 1;
log(item_4, y_0, lala, lala, [item_4, 2], 3);
log(1, y_0, lala, lala, [1, 2], 3);
export const y = y_0;"
`;

exports[`optimizations removes tails 1`] = `
"
rec_e fn exports_0:
tpl, fn lst_0:
lst_c lst_0, shrub, fn dlst_0:
tpl_i dlst_0, 0, fn bar_0:
tpl_i dlst_0, 1, fn spam_0:
tpl_i dlst_0, 2, fn ham_0:
add bar_0, spam_0, fn left_0:
add left_0, ham_0, fn ni_0:
str 'ni', fn key_0:
rec_s exports_0, key_0, ni_0, fn exports_1:
tpl, fn drctvs_0:
mod exports_1, drctvs_0, fn mod_0:"
`;

exports[`optimizations reuses item refs 1`] = `
"
rec_e fn exports_0:
int '1', fn item_3:
int '2', fn item_2:
int '3', fn item_1:
lst_e fn lst_5:
lst_a lst_5, item_2, fn lst_4:
lst_a lst_4, item_3, fn foo_0:
str 'foo', fn key_0:
rec_s exports_0, key_0, foo_0, fn exports_1:
int '1', fn left_1:
int '2', fn right_1:
add left_1, right_1, fn item_7:
lst_e fn cargs_0:
lst_a cargs_0, item_3, fn cargs_1:
lst_a cargs_1, item_2, fn cargs_2:
lst_a cargs_2, item_1, fn cargs_3:
lst_a cargs_3, foo_0, fn cargs_4:
lst_a cargs_4, item_7, fn cargs_5:
lst_a cargs_5, item_2, fn cargs_6:
lst_a cargs_6, item_3, fn cargs_7:
af log, cargs_7, fn mex_3:
lst_e fn drctvs_0:
mod exports_1, drctvs_0, fn mod_0:"
tpl item_2, item_3, fn foo_0:
str 'foo', fn key_0:
rec_s exports_0, key_0, foo_0, fn exports_1:
int '1', fn left_1:
int '2', fn right_1:
add left_1, right_1, fn item_7:
tpl item_3, item_2, item_1, foo_0, item_7, item_2, item_3, fn cargs_7:
af log, cargs_7, fn mex_3:
tpl, fn drctvs_0:
mod exports_1, drctvs_0, fn mod_0:"
`;

exports[`unpacking list destructures recs 1`] = `
Expand Down
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 @@ -12,7 +12,7 @@ rec_e fn exports_0:
rec_s rec_0, key_0, foo-bar, fn foo_0:
str 'foo', fn key_3:
rec_s exports_0, key_3, foo_0, fn exports_1:
lst_e fn drctvs_0:
tpl, fn drctvs_0:
mod exports_1, drctvs_0, fn mod_0:"
`;

Expand Down
5 changes: 4 additions & 1 deletion src/ir/serialize.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ a2s = fn [arg=false, ...args], out=[]:
false: match out:
[?]: ' ${out | join ', '}'
else: ''
else: a2s args, [...out, arg.i]
{i: ?}:
a2s args, [...out, arg.i]
else:
a2s args, [...out, arg]



Expand Down
10 changes: 4 additions & 6 deletions src/js/assignment/init.test.fnk.snap
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`assignment compiles 1`] = `
"const assign_0 = 1;
const no_clash_with_await_0 = 13;
const multi_line_assign_0 = 123 + 234 + (-567 - 1111);
export const assign = assign_0,
assign2 = assign_0,
no_clash_with_await = no_clash_with_await_0,
"const multi_line_assign_0 = 123 + 234 + (-567 - 1111);
export const assign = 1,
assign2 = 1,
no_clash_with_await = 13,
assign_lang_const1 = true,
assign_lang_const2 = false,
multi_line_assign = multi_line_assign_0,
Expand Down
1 change: 1 addition & 0 deletions src/js/conditionals/init.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ transform_is_rec = fn expr, ctx:
set_js2 expr, js, ctx



transform_is_lst = fn expr, ctx:
[{args: [val_id]}] = expr
val = get_js val_id, ctx
Expand Down
14 changes: 14 additions & 0 deletions src/js/conditionals/match.test.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,20 @@ describe 'match', fn:
to_match_snapshot


it 'handles scoping', fn:
expect
fink2js '
foo = nanu

test = fn foo, bar:
match foo:
bar:
foo
spam:
foo = bar * 2
[foo, bar, foo]
'
to_match_snapshot

describe 'match iterables', fn:
it 'matches values', fn:
Expand Down
Loading

0 comments on commit 203ee57

Please sign in to comment.