Skip to content

Commit

Permalink
fix: add missing support for member expr in records
Browse files Browse the repository at this point in the history
  • Loading branch information
kollhof committed Sep 19, 2021
1 parent 7e45109 commit 12d3369
Show file tree
Hide file tree
Showing 26 changed files with 638 additions and 594 deletions.
7 changes: 6 additions & 1 deletion src/generate.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ babel_traverse = import '@babel/traverse'
{transform_async} = import './js/async/init.fnk'


try_catch_ = fn func:
r = func _
[false, r]


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

[error, [js_ast]=[]] = try_catch fn:
[error, [js_ast]=[]] = try_catch_ fn:
[lir] = transform fink_ast, 'mod', ctx
[olir] = optimize lir, options
{js} = transform_ir olir, options
Expand Down
4 changes: 2 additions & 2 deletions src/generate.test.fnk
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{only, skip, 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'
Expand Down Expand Up @@ -35,7 +35,7 @@ describe 'module types', fn:



only.describe 'source maps', fn:
describe 'source maps', fn:
code = "
foo = 12345
bar = 'foo'
Expand Down
6 changes: 5 additions & 1 deletion src/generate.test.fnk.snap
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ const {
const {
ˆif: if_0
} = require(\\"es6-module\\");"
} = require(\\"es6-module\\");
exports.ˆdefault = ni_0;
exports.ˆcase = ni_0;
exports.shrub = ni_0;"
`;

exports[`module types compiles to mjs 1`] = `
Expand Down
6 changes: 5 additions & 1 deletion src/ir/assignment/init.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

{add, any, unique_ident} = import '../context.fnk'
{transform} = import '../transform.fnk'

{members_as_rec} = import '../literals/record.fnk'


bind = fn {value: src_id}, ctx:
Expand Down Expand Up @@ -85,6 +85,10 @@ transform_dr = fn [expr=false, ...exprs], rec_id, ctx, keys=[], prev=[], fallbac
[{...expr, right: left}, ...exprs]
rec_id, dl_ctx, keys, [...prev, ...flbk], flbk_id

{left.type: 'member'}:
{exprs: rx} = members_as_rec expr.left, expr.right
transform_dr [...rx, ...exprs], rec_id, ctx, keys, prev, fallback

{right.type: ? in ['ident', 'empty']}:
[key, key_id, val_id, next_ctx] = get_key_val expr, ctx
[val, end_ctx] = rec_get rec_id, key_id, val_id, {loc: expr.loc}, next_ctx, fallback
Expand Down
9 changes: 7 additions & 2 deletions src/ir/conditionals/match.fnk
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

{reverse} = import '@fink/std-lib/iter.fnk'

{transform_key} = import '../literals/record.fnk'
{transform_key, members_as_rec} = import '../literals/record.fnk'
{transform} = import '../transform.fnk'
{unique_ident} = import '../context.fnk'

Expand Down Expand Up @@ -130,6 +130,10 @@ match_rec = fn [expr=false, ...rest], rec_id, true_cont, true_id, else_ids, ret_
[x, y, z] = match_expr val_id, expr.right, rest_cont, rest_cont_id, else_ids, ret_id, rest_ctx
[[['rec_d', [rec_id, ...keys], [val_id], {loc: expr.loc}], ...x], y, z]

{type: 'rec:kv', left.type: 'member'}:
{exprs} = members_as_rec expr.left, expr.right
match_rec [...exprs, ...rest], rec_id, true_cont, true_id, else_ids, ret_id, ctx, keys

{type: 'rec:kv'}:
[val_id, key_ctx] = unique_ident 'itm', ctx
[key, key_id, next_ctx] = transform_key expr.left, key_ctx
Expand All @@ -142,7 +146,7 @@ match_rec = fn [expr=false, ...rest], rec_id, true_cont, true_id, else_ids, ret_
else: expr.right

[x, y, z] = match_expr val_id, val_expr, rest_cont, rest_cont_id, else_ids, ret_id, rest_ctx
[[...key, ['rec_g', [rec_id, key_id], [val_id], {loc: expr.loc}], ...x], y, z]
[[...key, ['rec_go', [rec_id, key_id], [val_id], {loc: expr.loc}], ...x], y, z]



Expand All @@ -157,6 +161,7 @@ match_expr = fn val_id, expr, true_cont, true_cont_id, else_ids, ret_id, ctx:
{type: 'rec'}:
match_rec expr.exprs, val_id, true_cont, true_cont_id, else_ids, ret_id, ctx


{type: 'partial'}:
match_any val_id, true_cont, true_cont_id, else_ids, {loc: expr.loc}, ctx

Expand Down
3 changes: 2 additions & 1 deletion src/ir/conditionals/match.test.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ describe 'match records', fn:
{foo: 4, ni: {na, nu}}: spam + ni
{foo: 1, foo: {bar: 'spam'}, shrub: {na: 'nu'}}: ni
{ni: {len: 1}, na: {len: 1}}: na
{ni: [na, 123]}: na
"
to_match_snapshot

Expand All @@ -188,7 +189,7 @@ describe 'match records', fn:
to_match_snapshot


skip.it 'matches member expr keys', fn:
it 'matches member expr keys', fn:
expect
fink2lir '
match foo:
Expand Down
Loading

0 comments on commit 12d3369

Please sign in to comment.