-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(find): implement
find item: ...
- Loading branch information
Showing
6 changed files
with
127 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`compiles fold compiles complex find 1`] = ` | ||
"ˆitems_2 => { | ||
for (const ˆitem_1 of ˆitems_2) { | ||
const { | ||
\\"item\\": item | ||
} = ˆitem_1; | ||
let _do_result; | ||
ˆmatch_5: { | ||
const ˆvalue_4 = item; | ||
if (ˆvalue_4 !== undefined && ˆvalue_4 !== null) { | ||
const { | ||
\\"spam\\": ˆp_6 | ||
} = ˆvalue_4; | ||
if (ˆp_6 === spam) { | ||
_do_result = shrub; | ||
break ˆmatch_5; | ||
} | ||
} | ||
{ | ||
_do_result = ni; | ||
break ˆmatch_5; | ||
} | ||
} | ||
const ˆfound_3 = _do_result; | ||
_do_result = undefined; | ||
if (ˆfound_3) return ˆitem_1; | ||
} | ||
return undefined; | ||
}; | ||
Object.assign(module.exports, {});" | ||
`; | ||
|
||
exports[`compiles fold compiles simple find 1`] = ` | ||
"ˆitems_2 => { | ||
for (const ˆitem_1 of ˆitems_2) { | ||
const item = ˆitem_1; | ||
const ˆfound_3 = item > 3; | ||
if (ˆfound_3) return ˆitem_1; | ||
} | ||
return undefined; | ||
}; | ||
Object.assign(module.exports, {});" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { | ||
params, for_of, split_last, func, returns, iff, consts, undef | ||
} from '../types'; | ||
import {block_statement} from './block'; | ||
|
||
|
||
export const transform_find = (node, {transform, unique_ident})=> { | ||
const [item_param] = node.args.map(transform); | ||
const [init] = params([item_param]); | ||
|
||
const item = unique_ident('item'); | ||
const items = unique_ident('items'); | ||
const found = unique_ident('found'); | ||
|
||
const [expressions, last_expr] = split_last(node.exprs); | ||
|
||
return func(items)( | ||
|
||
for_of(item, items)( | ||
consts(init, item), | ||
|
||
...expressions.map(block_statement({transform})), | ||
|
||
consts(found, transform(last_expr)), | ||
|
||
iff(found)( | ||
returns(item) | ||
) | ||
), | ||
|
||
undef() | ||
); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import {fink2js} from '../testing'; | ||
|
||
|
||
describe('compiles fold', ()=> { | ||
it('compiles simple find', ()=> { | ||
expect( | ||
fink2js(` | ||
find item: | ||
item > 3 | ||
`) | ||
).toMatchSnapshot(); | ||
}); | ||
|
||
it('compiles complex find', ()=> { | ||
expect( | ||
fink2js(` | ||
find {item}: | ||
match item: | ||
{spam}: shrub | ||
else: ni | ||
`) | ||
).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
|