Skip to content

Commit

Permalink
Merge pull request #46 from fink-lang/features
Browse files Browse the repository at this point in the history
features
  • Loading branch information
kollhof authored May 5, 2020
2 parents bf96a33 + fb02c9b commit ea2fc16
Show file tree
Hide file tree
Showing 39 changed files with 357 additions and 173 deletions.
4 changes: 2 additions & 2 deletions src/js/do-expression.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ last_expressions = fn path:
handler = path.get 'handler'
body = get_body handler
list:
...last_expressions(block)
...last_expressions(body)
...last_expressions block
...last_expressions body


replace_with_return = fn path:
Expand Down
2 changes: 1 addition & 1 deletion src/js/identifier.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ replace_chars = fn name, idx=0:
match check_ident.exec prefixed_name:
null:
[char] = name.slice idx, idx + 1
char_replacement = name.charCodeAt(idx).toString(36)
char_replacement = name.charCodeAt(idx).toString 36

start = name.slice 0, idx
end = name.slice idx + 1
Expand Down
1 change: 0 additions & 1 deletion src/lang/generic/left.fnk → src/js/left.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ transform_left = fn val:
... pipe val.elements:
map item: transform_left item


isObjectExpression ?:
objectPattern list:
... pipe val.properties:
Expand Down
24 changes: 20 additions & 4 deletions src/lang/arithmitic/index.fnk
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
{binaryExpression, unaryExpression} = import '@babel/types'
{add, any} = import '../context'
{transform_binary} = import '../generic/binary'
{transform_unary} = import '../generic/unary'


transform_op = dict:
'^': '**'


transform_arithmitic = fn {op, left, right}, {transform}:
{(op): operator=op} = transform_op

binaryExpression operator,
transform left
transform right


transform_unary = fn {op, right}, {transform}:
unaryExpression op, transform right



add_arithmitic = fn ctx:
pipe ctx:
add 'arithm', any, transform_binary
add 'arithm:right', any, transform_binary
add 'arithm', any, transform_arithmitic
add 'arithm:right', any, transform_arithmitic
add 'arithm:prefix', any, transform_unary

File renamed without changes.
File renamed without changes.
10 changes: 5 additions & 5 deletions src/lang/assignment/index.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
} = import '@babel/types'

{consts} = import '../../js/types'
{transform_left} = import '../../js/left'
{add, any} = import '../context'
{transform_left} = import '../generic/left'
{transform_value} = import '../partial'


Expand Down Expand Up @@ -84,13 +84,13 @@ transform_spread_right = fn {left, right}, ctx:

result_items = match idx:
0: list:
slice(items, idx, idx - len)
slice(items, idx - len)
slice items, idx, idx - len
slice items, idx - len

else: list:
items
slice(items, idx, idx - len)
slice(items, idx - len)
slice items, idx, idx - len
slice items, idx - len


result = expressionStatement
Expand Down
2 changes: 1 addition & 1 deletion src/lang/async/index.test.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe 'await', fn:

a_gen = unfold curr=0:
match shrub:
spam: await ni(curr)
spam: await ni curr
else: curr + 1

await ni
Expand Down
18 changes: 10 additions & 8 deletions src/lang/async/index.test.fnk.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,32 @@ const task3 = async (foo) => {
return bar + 123;
};
const a_gen = async function* unfold(ˆaccu_4 = 0) {
const a_gen = async function* unfold(ˆinitial_2) {
let ˆaccu_3 = 0;
while (true) {
const curr = ˆaccu_4;
const curr = ˆaccu_3;
let _do_result;
ˆmatch_3: {
const ˆvalue_2 = shrub;
ˆmatch_5: {
const ˆvalue_4 = shrub;
if (ˆvalue_2 === spam) {
if (ˆvalue_4 === spam) {
_do_result = await ni(curr);
break ˆmatch_3;
break ˆmatch_5;
}
{
_do_result = curr + 1;
break ˆmatch_3;
break ˆmatch_5;
}
}
const ˆresult_1 = _do_result;
_do_result = undefined;
yield ˆresult_1;
ˆaccu_4 = ˆresult_1;
ˆaccu_3 = ˆresult_1;
}
};
Expand Down
6 changes: 6 additions & 0 deletions src/lang/block/index.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
} = import '@babel/types'

{consts} = import '../../js/types'
{add, any} = import '../context'


block_statement = fn expr, {transform}:
Expand Down Expand Up @@ -30,3 +31,8 @@ transform_block = fn node, ctx:
...pipe exprs:
map expr: block_statement expr, ctx


add_block = fn ctx:
pipe ctx:
add 'block', any, transform_block

3 changes: 2 additions & 1 deletion src/lang/call/pipe.test.fnk.snap
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ exports[`pipe compiles 1`] = `
ˆpipe_result_2 = function* map(ˆitems_4) {
for (const ˆitem_3 of ˆitems_4) {
const item = ˆitem_3;
yield item * 2;
const ˆresult_5 = item * 2;
yield ˆresult_5;
}
}(ˆpipe_result_2);
}
Expand Down
18 changes: 16 additions & 2 deletions src/lang/comparison/index.fnk
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
{binaryExpression} = import '@babel/types'

{add, any} = import '../context'
{transform_binary} = import '../generic/binary'

transform_op = dict:
'==': '==='
'!=': '!=='


transform_comp = fn {op, left, right}, {transform}:
{(op): operator=op} = transform_op

bin_left = transform left
bin_right = transform right

binaryExpression operator, bin_left, bin_right


add_comparison = fn ctx:
pipe ctx:
add 'comp', any, transform_binary
add 'comp', any, transform_comp

2 changes: 1 addition & 1 deletion src/lang/conditionals/match.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ get_array_decl = fn arr, id_elems, right, ctx:
right: {type: 'ident', value: right.name, loc: arr.loc}
loc: arr.loc

consts(decl.left, decl.right)
consts decl.left, decl.right


match_array = fn value, arr, emit_result, ctx, cond:
Expand Down
2 changes: 1 addition & 1 deletion src/lang/errors.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ transform_error = fn {message}, node, {code, filename}:
# TODO: add filename and line to constructor?
err = new Error
`${filename}:${line}:${column}\n${
highlight_code_loc(code, node.loc)
highlight_code_loc code, node.loc
}\n\nUnable to transform '${type_op}'.\n\n${message}`

Object.assign err, {is_transform_err: true}
Expand Down
2 changes: 1 addition & 1 deletion src/lang/func/index.fnk
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{arrowFunctionExpression} = import '@babel/types'

{transform_left} = import '../../js/left'
{add, any} = import '../context'
{transform_left} = import '../generic/left'
{transform_block} = import '../block'


Expand Down
2 changes: 1 addition & 1 deletion src/lang/func/index.test.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe 'func', fn:
c + 1: d + 1

fun7 = fn a, b=12, c:
shrub(...a)
shrub ...a
foo()
bar()
`
Expand Down
16 changes: 0 additions & 16 deletions src/lang/generic/binary.fnk

This file was deleted.

8 changes: 0 additions & 8 deletions src/lang/generic/index.fnk

This file was deleted.

6 changes: 0 additions & 6 deletions src/lang/generic/unary.fnk

This file was deleted.

4 changes: 2 additions & 2 deletions src/lang/index.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
{add_jsx} = import './jsx'
{add_js_compat} = import './js-compat'
{add_arithmitic} = import './arithmitic'
{add_generic} = import './generic'
{add_block} = import './block'
{add_partial} = import './partial'
{wrap_with_comment_loc} = import './comments'

Expand All @@ -46,7 +46,7 @@ add_transformers = fn ctx:
add_call
add_async
add_jsx
add_generic
add_block
add_js_compat


Expand Down
25 changes: 25 additions & 0 deletions src/lang/iterable/common.fnk
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{consts, yields} = import '../../js/types'


transform_init = fn left, right, {transform}:

item_init = transform dict:
type: 'assign'
op: '='
left
right: {type: 'ident', value: right.name, loc: left.loc}
loc: left.loc

consts item_init.left, item_init.right


yield_expr = fn result, expr, {transform}:
[yield_value, is_spread] = match expr:
{type: 'spread'}: [expr.right, true]
else: [expr, false]

list:
consts result, transform yield_value
yields
result
is_spread
4 changes: 2 additions & 2 deletions src/lang/iterable/filter.fnk
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
{generator, for_of, yields, consts} = import '../../js/types'

{block_statement} = import '../block'
{transform_init} = import './init'
{transform_init} = import './common'


transform_filter = fn node, ctx:
{transform, unique_ident} = ctx

item = unique_ident 'item'
[item_val] = node.args;
[item_val] = node.args
item_init = transform_init item_val, item, ctx

items = unique_ident 'items'
Expand Down
3 changes: 2 additions & 1 deletion src/lang/iterable/find.fnk
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{returnStatement, ifStatement} = import'@babel/types'

{for_of, func, consts, undef} = import '../../js/types'

{block_statement} = import '../block'
{transform_init} = import './init'
{transform_init} = import './common'


transform_find = fn node, ctx:
Expand Down
15 changes: 5 additions & 10 deletions src/lang/iterable/fold.fnk
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
{expressionStatement} = import '@babel/types'

{assign, params, for_of, func, consts, lets} = import '../../js/types'
{assign, params, for_of, func, lets} = import '../../js/types'
{block_statement} = import '../block'
{transform_init} = import './init'
{transform_init} = import './common'


transform_fold = fn node, ctx:
{transform, unique_ident} = ctx

[item_arg, acc_arg] = node.args;
[item_arg, acc_arg] = node.args

item = unique_ident 'item'
item_init = transform_init item_arg, item, ctx

acc = unique_ident 'acc'
acc_assign = transform dict:
type: 'assign'
op:'='
left: acc_arg.left
right: {type: 'ident', value: acc.name}
loc: acc_arg.left.loc
acc_assign = transform_init acc_arg.left, acc, ctx

items = unique_ident 'items'
[...expressions, last_expr] = node.exprs
Expand All @@ -28,7 +23,7 @@ transform_fold = fn node, ctx:
lets acc, transform acc_arg.right

for_of [item, items],
consts acc_assign.left, acc_assign.right
acc_assign
item_init

...pipe expressions:
Expand Down
13 changes: 0 additions & 13 deletions src/lang/iterable/init.fnk

This file was deleted.

Loading

0 comments on commit ea2fc16

Please sign in to comment.