Skip to content

Latest commit

 

History

History
491 lines (382 loc) · 9.05 KB

Syntax.md

File metadata and controls

491 lines (382 loc) · 9.05 KB

Syntax

Content

Variables

A variable can store a specified type of value e.g. int, string, bool. This type cannot be changed later in the program.

Back to top

Array variables

An array can be declared from every base data type e.g. int, string, bool.

Syntax

dataType[] variableName = value;

Example

str[] strs = [];       # Empty string array
int[] ints = [43, 44]; # Integer array with values
ints[0] = 42;          # Change value at index 0 to 42
ints[] = 45;           # Append array with new value 45

Back to top

Boolean variables

A boolean variable can only store the values true and false.

Example

bool b = false;
b = true;

Back to top

Integer variables

An integer variable can store values between -2^63 and 2^63-1 on 64-bit computers.

Example

int i = 42;
i = 48 / 2;

Back to top

String variables

A string variable can store a string value. The limit of long a string can be depends on the environment where the bash script will be executed.

Example

str s = "Hello ";
s += "World";

Back to top

Comparisons

Two values can be compared with a boolean as return value. This way the result of the comparison can be used in a condition of an if or while control structure.

Back to top

Comparing Booleans

The comparison of boolean values allows the equal and unequal operation.

Example

bool b = true;

# Equal
if (b == false) {
}
# Unequal
if (b != true) {
}

Back to top

Comparing Integers

The comparison of integer values allows the equal, unequal, greater (or equal) and smaller (or equal) operation.

Example

int i = 42;
# Equal
if (i == 48) {
}
# Unequal
if (i != 48) {
}
# Smaller
if (i < 48) {
}
# Smaller or equal
if (i <= 48) {
}
# Greater
if (i > 48) {
}
# Greater or equal
if (i >= 48) {
}

Back to top

Comparing Strings

The comparison of string values allows the equal, unequal, greater and smaller operation.

Example

str s = "abc";
# Equal
if (s == "abc") {
}
# Unequal
if (s != "abc") {
}
# Smaller
if (s < "bcd") {
}
# Greater
if (s > "bcd") {
}

Back to top

Control structures

Control structures allow to change a purely linear program flow e.g. to "branches" (if) depending on a condition or repeat a code block multiple times until a condition is true (while).

Back to top

For

The for loop executes the block of code for each array entry.

Syntax

for (type variableName in array) {
    # block of code that is executed for each array entry
}

Example

for (int i in [1, 2, 3]) {
    printLn(i);
}
int[] ints = [4, 5, 6];
for (int i in ints) {
    printLn(i);
}

Back to top

If

Use the if statement to execute a specified block of code if a condition is true.

Syntax

if (condition) {
    # block of code that is executed if condition is true
}

Examples

if (true) {
    printLn("true");
} else if (true && true) {
    printLn("true");
}

if (42 > 13) {
    printLn("true");
} else {
    printLn("false");
}

bool b = true;
if (b) {
    printLn("true");
}

Back to top

While

The while loop executes the block of code until the given condition is true.

Syntax

while (condition) {
    # block of code that is executed repeatedly while the condition is true
}

Example

while (true && true) {
    printLn("true");
}

int i = 0;
while (i < 10) {
    i += 1;
}

bool b = true;
while (b) {
    printLn("true");
}

Back to top

Native functions

The following functions are provided for use in your scripts. These are directly transpiled into bash or represented by a function implemented in bash.

Back to top

Exec

The native function exec allows to directly add bash code into the transpilat. The output from the given command is returned.

Syntax

exec(str command) str

Example

exec("touch test.txt");

str cmd = "touch test.txt";
exec(cmd);

Back to top

Exit

The native function exit exits the current script with a status code.

Syntax

exit(int code) void

Example

exit(0); # Success
exit(1); # Error

Back to top

Input

The native function input waits for the user of the script to input a string and returns it.

Syntax

input(str prompt) str

Example

str userInput = input("Pleas enter your name:");

Back to top

Print

The native functions print and printLn write the given values to terminal. The difference between print and printLn is that printLn adds new line.

Syntax

print(str|int|bool value, ...) void
printLn(str|int|bool value, ...) void

Example

print("Hello ");
printLn("World");

printLn("str", 42, false, null);

int i = 43;
printLn("i =", i);

Back to top

Sleep

The native function sleep waits for the given amount of seconds. After that the program flow continues.

Syntax

sleep(int seconds) void

Example

# Sleep for 10 seconds
sleep(10);

Back to top

StrContains

The native function strContains checks if the given string contains the given substring.

Syntax

strContains(str value, str substring) bool

Example

strContains("cow, pig, chicken", "pig"); # true
strContains("cow, pig, chicken", "bird"); # false

Back to top

StrIsBool

The native function strIsBool checks if the given string is a boolean so that it could be converted to a string e.g. with strToBool.

Syntax

strIsBool(str value) bool

Example

strIsBool("true"); # true
strIsBool("str"); # false

StrIsInt

The native function strIsInt checks if the given string is a number so that it could be converted to a string e.g. with strToInt.

Syntax

strIsInt(str value) bool

Example

strIsInt("123"); # true
strIsInt("str"); # false

Back to top

StrSplit

The native function strSplit splits the given string at the given seperator and returns the string parts as a string array.

Syntax

strSplit(str value, str separator) str[]

Example

str[] strs = strSplit("a,b,c,d", ","); # ["a", "b", "c", "d"]
strs = strSplit(exec("docker ps"), " "); # ["CONTAINER", "ID", "IMAGE", "COMMAND", "CREATED", "STATUS", "PORTS", "NAMES"]

Back to top

StrToBool

The native function strToBool takes a given string and tries to convert it into a boolean value.

Syntax

strToBool(str value) bool

Example

str s = "123";
bool b1 = strToBool(s); # b = false
bool b2 = strTobool("true") # b = true
bool b3 = strTobool("false") # b = false

Back to top

StrToInt

The native function strToInt takes a given string and tries to convert it into an integer value.

Syntax

strToInt(str value) int

Example

str s = "123";
int i = strToInt(s); # i = 123

Back to top

User defined functions

A function can be used to reuse code and make it easier to read.

Syntax

func functionName(type param) type {
    # block of code executed when the function is called
}

Back to top

Without parameters

A function can be defined without parameters.

Example

func printHelloWorld() void {
    printLn("Hello World!");
}

printHelloWorld();

Back to top

With parameters

The parameters must have a fixed type e.g. int, str, bool.

Example

func printGivenString(str s) void {
    printLn("Given string: '" + s + "'");
}

printGivenString("Hello World");

Back to top

With return value

A function can return a value. The type of the value must be fixed e.g. int, str, bool.

Example

func add(int a, int b) int {
    return a + b;
}

int c = add(123, 456);

Back to top