Skip to content

Latest commit

 

History

History
89 lines (74 loc) · 1.81 KB

BashTranspilat.md

File metadata and controls

89 lines (74 loc) · 1.81 KB

Bash Transpilat

This document contains technical details how the structures have been solved in bash syntax.

Content

Bool

There are no native bool types in bash. So boolean expressions are represented as strings.

Example:

# ScriLa
if (true && false) {
	printLn("true");
}
# Bash transpilat
if [[ "true" == "true" ]] && [[ "false" == "true" ]]; then
	echo "true"
fi

Bool - Assign comparison

There is no native way to assign the result of a comparision as bool to a variable in bash. So this assignment will be replaced with a if statement that uses the temporary variable $tmpBool and sets it to true or false (in the else block).

Example:

# ScriLa
bool b = 42 > 13;
# Bash transpilat
if [[ 42 -gt 13 ]]
then
	tmpBools[0]="true"
else
	tmpBools[0]="false"
fi
b="${tmpBools[0]}"

Function Return values

Bash functions can return a status code between 0 and 255. Zero stands for success.
Bash Example:

# Bash
function fn() {
    return 42
}

The bash transpilat for a ScriLa function uses a temporary variable of type array to return the value:

Return type Variable
bool $tmpBools
int $tmpInts
string $tmpStrs

The index of the array is dynamic so that it can be set from outside of the function. This allows passing multiple function calls as arguments for a function call.

Example:

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

int result = add(13, 42);
# Bash transpilat
add () {
	local a=$1
	local b=$2
	tmpInts[${tmpIndex}]=$((${a} + ${b}))
	return
}

tmpIndex=0
add 13 42
result=${tmpInts[0]}