Created
October 15, 2017 09:46
-
-
Save guiprav/488b00cb8cb6d864fe6e2a7d2689d07e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // https://github.com/n2liquid/shift-builder/blob/master/test.js | |
| let n = require('./node_builders'); | |
| let compile_fn = require('./compile_fn'); | |
| let n_factorial = n.fn_expr('factorial', ['n'], [ | |
| n.if_stmt(n.binary_expr('===', [ | |
| n.id('n'), n.num(0), | |
| ]), [ | |
| n.return_stmt(n.num(1)), | |
| ]), | |
| n.return_stmt(n.binary_expr('*', [ | |
| n.id('n'), n.call_expr('factorial', [ | |
| n.binary_expr('-', [ | |
| n.id('n'), n.num(1), | |
| ]), | |
| ]), | |
| ])), | |
| ]); | |
| let factorial = compile_fn(n_factorial); | |
| console.log('3! is', factorial(3)); // 3! is 6 | |
| console.log('4! is', factorial(4)); // 4! is 24 | |
| console.log('5! is', factorial(5)); // 5! is 120 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment