From 27ee5334da1eb1f3d15067f428219f9717946019 Mon Sep 17 00:00:00 2001 From: Artemy Date: Thu, 18 Aug 2022 17:00:47 +0300 Subject: [PATCH] doc: example of nested functions Which only work inside another function --- examples/calc.json5 | 60 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 examples/calc.json5 diff --git a/examples/calc.json5 b/examples/calc.json5 new file mode 100644 index 0000000..607bbac --- /dev/null +++ b/examples/calc.json5 @@ -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] }], +]