doc: example of nested functions

Which only work inside another function
This commit is contained in:
Artemy 2022-08-18 17:00:47 +03:00
parent d57d67a960
commit 27ee5334da

60
examples/calc.json5 Normal file
View file

@ -0,0 +1,60 @@
[
{
fn: {
name: "myCalc",
args: ["a", "o", "b"],
body: [
{
fn: {
name: "sum",
args: ["a", "b"],
body: [{ return: { calc: [{ var: "a" }, "+", { var: "b" }] } }],
},
},
{
fn: {
name: "sub",
args: ["a", "b"],
body: [{ return: { calc: [{ var: "a" }, "-", { var: "b" }] } }],
},
},
{
fn: {
name: "mul",
args: ["a", "b"],
body: [{ return: { calc: [{ var: "a" }, "*", { var: "b" }] } }],
},
},
{
if: {
condition: { comp: [{ var: "o" }, "==", "*"] },
body: [{ return: { mul: [{ var: "a" }, { var: "b" }] } }],
else: [
{
if: {
condition: { comp: [{ var: "o" }, "==", "-"] },
body: [{ return: { sub: [{ var: "a" }, { var: "b" }] } }],
else: [
{
if: {
condition: { comp: [{ var: "o" }, "==", "+"] },
body: [
{ return: { sum: [{ var: "a" }, { var: "b" }] } },
],
else: [{ return: "Wrong operator!" }],
},
},
],
},
},
],
},
},
],
},
},
[{ myCalc: [3, "*", 2] }],
[{ myCalc: [3, "/", 2] }],
[{ myCalc: [3, "+", 2] }],
[{ myCalc: [3, "-", 2] }],
]