ONLang/examples/example.json5
2022-08-15 16:12:58 +03:00

324 lines
6.7 KiB
Text

// Now the file with the extension .json5, but then it will be .onla
[
{
print: [
"Test calc and recursion\n",
"(2+2*4) + 2 = ",
{
calc: [{ calc: [2, "+", { calc: [2, "*", 4] }] }, "+", 2],
},
"\n",
],
},
{
print: [
"-----All operators-----\n\n\tNORMAL",
"\n 2 * 3 = ",
{ calc: [2, "*", 3] },
"\n 6 / 2 = ",
{ calc: [6, "/", 2.5] },
"\n 2 + 3 = ",
{ calc: [2, "+", 3] },
"\n 2 - 3 = ",
{ calc: [2, "-", 3] },
"\n 2 % 2 = ",
{ calc: [2, "%", 2] },
"\n 3 % 2 = ",
{ calc: [3, "%", 2] },
"\n\n\tBit operators",
"\n 4 & 4 = ",
{ calc: [4, "&", 4] },
"\n 4 & 3 = ",
{ calc: [4, "&", 3] },
"\n 4 | 4 = ",
{ calc: [4, "|", 4] },
"\n 4 | 3 = ",
{ calc: [4, "|", 3] },
"\n -8 >> 3 = ",
{ calc: [-8, ">>", 3] },
"\n 5 << 3 = ",
{ calc: [5, "<<", 3] },
"\n\n--End of All operators--",
],
},
{
print: [
"-----All comparisons-----\n\n\tBool",
"\n true != false: ",
{ comp: [true, "!=", false] },
"\n true == false: ",
{ comp: [true, "==", false] },
"\n true && false: ",
{ comp: [true, "&&", false] },
"\n true || false: ",
{ comp: [true, "||", false] },
"\n true > false: ",
{ comp: [true, ">", false] },
"\n true >= false: ",
{ comp: [true, ">=", false] },
"\n true <= false: ",
{ comp: [true, "<=", false] },
"\n\n\tNumbers",
"\n 1 != 2: ",
{ comp: [1, "!=", 2] },
"\n 4 == 2+2: ",
{ comp: [4, "==", { calc: [2, "+", 2] }] },
"\n 4 > 2+2: ",
{ comp: [4, ">", { calc: [2, "+", 2] }] },
"\n 4 < 2+2: ",
{ comp: [4, "<", { calc: [2, "+", 2] }] },
"\n 4 >= 2+2: ",
{ comp: [4, ">=", { calc: [2, "+", 2] }] },
"\n 8 <= 2+2: ",
{ comp: [8, "<=", { calc: [2, "+", 2] }] },
"\n 8 <= 4+4: ",
{ comp: [8, "<=", { calc: [4, "+", 4] }] },
"\n\n\tOther",
"\n [0,1] != [1,0]: ",
{ comp: [[0, 1], "!=", [1, 0]] },
"\n [0,1] == [0,1]: ",
{ comp: [[0, 1], "==", [0, 1]] },
'\n "What" != "Not What": ',
{ comp: ["What", "!=", "Not What"] },
'\n "What" == "Not What": ',
{ comp: ["What", "==", "Not What"] },
"\n\n\tMore complex",
"\n (1 + 1 > 3) == false: ",
{ comp: [{ comp: [{ calc: [1, "+", 1] }, ">", 3] }, "==", false] },
"\n (( 1 + 1 > 3 ) == false) && (( 1 + 1 > 3 ) == true): ",
{
comp: [
{ comp: [{ comp: [{ calc: [1, "+", 1] }, ">", 3] }, "==", false] },
"&&",
{ comp: [{ comp: [{ calc: [1, "+", 1] }, ">", 3] }, "==", true] },
],
},
"\n (( 1 + 1 > 3 ) == false) || (( 1 + 1 > 3 ) == true): ",
{
comp: [
{ comp: [{ comp: [{ calc: [1, "+", 1] }, ">", 3] }, "==", false] },
"||",
{ comp: [{ comp: [{ calc: [1, "+", 1] }, ">", 3] }, "==", true] },
],
},
"\n\n-End of All comparisons-",
],
},
"\nCreating variables",
{
let: {
str: "A",
num: 2,
arr: ["Array", "in", "variable"],
obj: {
obj: {
arr: ["i'm array", "!"],
string: "I'm string!",
calculated: { calc: [2, "*", 2] },
},
},
},
},
{
let: {
calcArr: {
arr: ["Some str", { var: "num" }],
},
},
},
{
let: {
calculated: { calc: [{ var: "num" }, "*", 4] },
},
},
[
"Created 6 variables with values:\n",
"str = ",
{ var: "str" },
"\nnum = ",
{ var: "num" },
"\narr = ",
{ var: "arr" },
"\nobj = ",
{ toString: { var: "obj" } },
"\ncalculated (num * 4) = ",
{ var: "calculated" },
'\ncalculated array ["Some str", num] = ',
{ var: "calcArr" },
],
"Execute calculated = calculated + 1",
{
assign: {
calculated: { calc: [{ var: "calculated" }, "+", 1] },
},
},
["After executing calculated = ", { var: "calculated" }],
"\nRun loop",
{
loop: [
{
assign: {
calculated: { calc: [{ var: "calculated" }, "+", 1] },
},
},
{
loop: [
{
if: {
condition: { comp: [{ var: "num" }, ">=", 10000] },
body: ["break"],
},
},
{
assign: {
num: { calc: [{ var: "num" }, "+", 1] },
},
},
{
print: [
"\rcalculated = ",
{ var: "calculated" },
"; num = ",
{ var: "num" },
],
},
],
},
{
print: [
"\rcalculated = ",
{ var: "calculated" },
"; num = ",
{ var: "num" },
],
},
{
if: {
condition: { comp: [{ var: "calculated" }, ">=", 10000] },
body: ["\n", "break"],
},
},
],
},
{
if: {
condition: { comp: [{ var: "calculated" }, ">=", 9999] },
body: [["сalculated is >= ", 9999]],
else: [["сalculated is < ", 9999]],
},
},
{
if: {
condition: { comp: [{ var: "calculated" }, ">", 10000] },
body: [["сalculated is > ", 10000]],
else: [["сalculated is <= ", 10000]],
},
},
{ let: { i: 0 } },
"\nLoop with sleep",
{
loop: [
{
if: {
condition: { comp: [{ var: "i" }, ">=", 10] },
body: ["break"],
},
},
{ assign: { i: { calc: [{ var: "i" }, "+", 1] } } },
{ print: ["\ri = ", { var: "i" }] },
{ sleep: 500 },
],
},
"\n\nCheck scopes of visibility",
{
scope: [
{ let: { scoped: "Value" } },
["scoped = ", { var: "scoped" }],
['isExist("scoped"): ', { isExist: "scoped" }],
],
},
"After scope",
['isExist("scoped"): ', { isExist: "scoped" }],
"Creating function `sum`",
{
fn: {
name: "sum",
args: ["first", "second"],
body: [
{
return: {
calc: [{ var: "sum.first" }, "+", { var: "sum.second" }],
},
},
],
},
},
"Run function `sum`",
["fn sum(first, second){ return first+second}; sum(2,2) = ", { sum: [2, 2] }],
"\nInput a var",
{
let: {
name: { input: "Your name: " },
},
},
["Hey, ", { var: "name" }, "!"],
"Check a var references",
'\nCreate variable refToName with reference variable "name"',
{
let: {
refToName: { ref: "name" },
},
},
["refToName = ", { var: "refToName" }],
"Try assign name with new value",
{
assign: {
name: { input: "Any name: " },
},
},
"",
["name = ", { var: "name" }],
["refToName = ", { var: "refToName" }],
]