mirror of
https://github.com/artegoser/ONLang
synced 2024-12-23 09:33:44 +03:00
120 lines
2.9 KiB
Text
120 lines
2.9 KiB
Text
[
|
|
{
|
|
fn: {
|
|
name: "fact_loop",
|
|
args: ["n"],
|
|
body: [
|
|
{ let: { result: { var: "n" } } },
|
|
{
|
|
loop: [
|
|
//using loop because recursion is not supported*
|
|
//* - already supported
|
|
{
|
|
if: {
|
|
condition: { comp: [{ var: "n" }, ">", 1] },
|
|
body: [
|
|
{
|
|
assign: {
|
|
n: {
|
|
calc: [{ var: "n" }, "-", 1],
|
|
},
|
|
},
|
|
},
|
|
{
|
|
assign: {
|
|
result: {
|
|
calc: [{ var: "result" }, "*", { var: "n" }],
|
|
},
|
|
},
|
|
},
|
|
],
|
|
else: ["break"],
|
|
},
|
|
},
|
|
],
|
|
},
|
|
{ return: { var: "result" } },
|
|
],
|
|
},
|
|
},
|
|
|
|
{
|
|
fn: {
|
|
name: "fact",
|
|
args: ["n"],
|
|
body: [
|
|
{
|
|
if: {
|
|
condition: { comp: [{ var: "n" }, "==", 1] },
|
|
body: [{ return: 1 }],
|
|
else: [
|
|
{
|
|
return: {
|
|
calc: [
|
|
{ var: "n" },
|
|
"*",
|
|
{ fact: [{ calc: [{ var: "n" }, "-", 1] }] },
|
|
],
|
|
},
|
|
},
|
|
],
|
|
},
|
|
},
|
|
],
|
|
},
|
|
},
|
|
|
|
{ import: { path: "examples/assertions.onla", as: "assertions" } },
|
|
|
|
["10! == 3 628 800: ", { "assertions.eq": [{ fact: [10] }, 3628800] }],
|
|
["11! == 39 916 800: ", { "assertions.eq": [{ fact: [11] }, 39916800] }],
|
|
|
|
["12! == 479 001 600: ", { "assertions.eq": [{ fact: [12] }, 479001600] }],
|
|
["13! == 6 227 020 800: ", { "assertions.eq": [{ fact: [13] }, 6227020800] }],
|
|
|
|
[
|
|
"14! == 87 178 291 200: ",
|
|
{ "assertions.eq": [{ fact: [14] }, 87178291200] },
|
|
],
|
|
[
|
|
"15! == 1 307 674 368 000: ",
|
|
{ "assertions.eq": [{ fact: [15] }, 1307674368000] },
|
|
],
|
|
|
|
[
|
|
"16! == 20 922 789 888 000: ",
|
|
{ "assertions.eq": [{ fact: [16] }, 20922789888000] },
|
|
],
|
|
[
|
|
"17! == 355 687 428 096 000: ",
|
|
{ "assertions.eq": [{ fact: [17] }, 355687428096000] },
|
|
],
|
|
|
|
[
|
|
"18! == 6 402 373 705 728 000: ",
|
|
{ "assertions.eq": [{ fact: [18] }, 6402373705728000] },
|
|
],
|
|
[
|
|
"19! == 121 645 100 408 832 000: ",
|
|
{ "assertions.eq": [{ fact: [19] }, 121645100408832000] },
|
|
],
|
|
|
|
[
|
|
"20! == 2 432 902 008 176 640 000: ",
|
|
{ "assertions.eq": [{ fact: [20] }, 2432902008176640000] },
|
|
],
|
|
"",
|
|
[
|
|
"(loop) 10! == 3 628 800: ",
|
|
{ "assertions.eq": [{ fact_loop: [10] }, 3628800] },
|
|
],
|
|
[
|
|
"(loop) 11! == 39 916 800: ",
|
|
{ "assertions.eq": [{ fact_loop: [11] }, 39916800] },
|
|
],
|
|
|
|
// [
|
|
// "21! == 51 090 942 171 709 440 000: ",
|
|
// { _eq: [{ fact: [21] }, 51090942171709440000] },
|
|
// ], some json and yaml troubles with number `51090942171709440000`
|
|
]
|