Example program
Here is a program showing virtually all features:
// IMPORTANT
// In this file, next to print() calls, the supposed output will be shown in a comment along with a commentary if needed
//Importing the standard library for print and other functions
import stdlib;
//Global variables go first thing. Accessible from functions
global glob = ["h", "e", "l", "l", "o"];
//Export globals can be accessed from any import, but only set from its owner
export global other = "today";
//Normal body variables
tab f = "world";
//Concatenation operator +: table concatenation
tab greeting = glob + [" ", f];
print(greeting); //hello world
//+= concatenation operator: concatenates itself and sets
greeting += " stranger";
print(greeting); //hello world stranger
//String literals are handled like length 1 tables:
tab eq = "abcd" == ["abcd"];
//Dollar strings with full expressions (syntax highlighter migh break because of this)
tab doll = $"The message of the day is '{greeting + "!"}'";
print(doll); //The message of the day is 'hello world stranger!'
//True and false handled with length: <0 or 0 is false, >0 is true
tab true = ["anything"];
tab false = [];
//If statements
if true{
print("true"); //true
}
if false{
print("false");
}
//Ternary operator ?:
print(eq ? "equal" : "not equal"); //equal
//negation operator !: returns [] (false) when the table has length greater than zero and [""] (true) when length is 0. This is the general concept of truthiness
tab t = !["h"];
//difference operator -: removes elements
tab h = ["a", "a", "b"] - ["a"];
print(h); //ab
//-= difference operator also available
h -= "a";
print(h); //b
//product operator *: cartesian product
tab p = ["a", "b"] * ["c", "d"];
print(p); //acbcadbd
p = ["a", "b"] * 3;
print(p); //ababab
p = ["a", "b"];
p *= 3; //*= also available
print(p); //ababab
p = 3 * ["a", "b"];
print(p); //aaabbb
//Equality operators == and !=
print(["a", "b"] != ["ab"] ? "true" : "false"); //true
print(["a", "b"] == ["a", "b"] ? "true" : "false"); //true
print(["a", "b"] == "ab" ? "true" : "false"); //false
//Reduction operator ^: joins all elements into a single string
print(["a", "b", "c"]^ == "abc" ? "true" : "false"); //true
//Decomposition operator %: separates all charachters of all elements into their own elements
print(["a", "b", "c"] == "abc"% ? "true" : "false"); //true
//containment operator @: checks if all elements are contained. !@ variant
print(["a", "b"] @ ["c", "d", "a", "n", "b"] ? "true" : "false"); //true
print(["a", "b", "m"] @ ["c", "d", "a", "n", "b"] ? "true" : "false"); //false
print("a" !@ ["b", "c"] ? "true" : "false"); //true
//Comparison operators <, <=, >, >=: they compare lengths of tables
print(["1"] > ["a", "b"] ? "true" : "false"); //false
print(4 > ["a", "b"] ? "true" : "false"); //true
//Numbers are tables as well: 2 is the same as ["", ""], with empty strings and length 2.
print(3); //Nothing will be seen (empty strings)
print(3 == ["", "", ""] ? "true" : "false"); //true
//length: retuns a number table of the length: ["a", "b"].length => ["", ""] (2);
print(2 == ["a", "b"].length ? "true" : "false"); //true
//Element access
tab abc = ["a", "b", "c", "d", "e", "f"];
print(abc.0); //a
print(abc.1); //b
//Negative index: last elements
print(abc.-1); //f
print(abc.-2); //e
print(abc.random); //Random element
//Expression index access with []: full expressions
print(abc[0 + 1]); //b
tab oob = abc.100; //Obviously out of bounds, returns []
print(oob); //Nothing will be seen (empty table)
print(abc.-100); //Nothing will be seen (empty table) (Obviously out of bounds, returns [])
//Element setting
tab nums = ["0", "2", "3"];
nums.0 = "1";
print(nums); //123
nums[1] = "a";
print(nums); //1a3
//Expression index setting with []: full expressions
nums[1 + 2] = "g";
print(nums); //1a3g
//Range access: [index, length]. Handles negative indexes, negative lengths and expressions
print(abc[0, 3]); //abc
print(abc[1, 3]); //bcd
print(abc[1, 300]); //Out of bounds, auto crops: bcdef
print(abc[-1, 1]); //f
print(abc[-1, -1]); //e
print(abc[random, 2]); //random consecutive elements
print(abc[1 + ["n", "t"], 1]); //Expression: d
//Grouping with ()
tab g = (abc + "k")%.length > (16 - (4 + 2));
print(g ? "true" : "false"); //false
//Literal building: flattening of other tables
tab all = [abc, "+", nums, ["j", ["k", "l", abc], "k"]];
print(all); //abcdef+1a3gjklabcdefk
//While statements with else clauses(if no break was invoked)
tab i = 0;
while i < 5{
print("elem");
i += 1;
}else{
print("finish");
}
//Do statements with optionally else clause
i = 0;
do{
print("do");
i += 1;
}while i < 6;
//Visual separator
print("next loop"); //next loop
i = 0;
do{
print("do");
i += 1;
}while i < 6 else{
print("finish do");
}
//Foreach
foreach letter @ abc + nums{
print(letter);
}
//Lexical scoping
tab k = "out";
if 1{
print(k); //out
tab k = "in";
print(k); //in
}
print(k); //out
//Function call
print(plusA("h")); //ha
//Implicit return
print(retNone() == [] ? "true" : "false"); //true
//Global var access
tab glob = "hi";
print(greet()); //hi
//Self function call
print(abc.decorate()); //eeeeee
//Alternate self funcion call
print(decorate(abc)); //eeeeee
//Function overloading
print(fibonacci(10).decorate()); //eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
print(fibonacci(20, 3)); //text here. lorem ipsum
//stdlib includes a function called 'upper' to turn tables into uppercase. However, we declared a function called the same. See what happens when declaring the import:
print("j".upper()); //oops
print("j".stdlib::upper()); //J
print("j".local::upper()); //oops
//Complex funcion usage + library functions
print(join([getDate()[0, 3].reverse().join("/"), getDate()[3, 3].join(":")], " ")); //Todays date
//Exiting the script compeltely(not needed at the end, used for early exit, maybe even in a function)
exit;
//Functions go AFTER all of the program
function plusA(t){
return t + "a";
}
function retNone(){
print("hello!");
//Implicitly returns []
}
function greet(){
return glob;
}
function fibonacci(n){
if n <= 1{
return n;
}
//Recursion
return fibonacci(n - 1) + fibonacci(n - 2);
}
function fibonacci(n, k){
return "text here. lorem ipsum";
}
//Self function
function decorate(self){
tab i = 0;
tab r;
while i < self{
r += "e";
i += 1;
}
return r;
}
//Local function
function upper(self){
return "oops";
}