Uartix Programming Language

/wor·tiks/

Strange dynamic programming and scripting language that performs mathematical computations on a connected Raspberry Pi Pico (RP2040) through UART connectivity.




Language Constructs

This documentation provides a comprehensive guide to the expression and statement constructs in Uartix, based on its Backus-Naur Form (BNF) grammar definition. Each construct is explained in detail with examples to illustrate their usage.


Expressions

Expressions in Uartix are the building blocks for creating and manipulating data. They can range from simple literals to complex operations involving multiple constructs.


Type Expression

Type expressions are used to specify the type of an expression. This can be useful for type checking and ensuring that the data conforms to expected types.

example_01.utx

# Get the type of variable x.

x = type "Hello!";
render x + "\r\n";

y = type 3.14;
render y + "\r\n";

Block Expressions

Block expressions group multiple statements together within braces. They are used to create a scope for variables and control flow.

example_02.utx

# Block example as function body.

add = func(x, y) {
    ret x + y;
};

render add(5, 10);

example_03.utx

# Block example as variable value.

message = {
    hello = "Hello";
    hello + ", world!";
};

render message + "\r\n";

Render Expression

Render expressions are used to output the value of an expression. This is similar to a print statement in other languages.

example_04.utx

# Render expression examples.

render "Hello, world!\r\n";
render 3.14;

Catch Expression

Catch expressions handle exceptions that may occur during the evaluation of an expression. Catch-handle expression is the equivalent of try-catch in other programming languages.

example_05.utx

# Throw and catch example

catch {
    throw "This is an error.";
}
handle e {
    render "Error: " + e + "\r\n";
}
then {
    render "Error was caught.\r\n";
};

Do Expression

Do expressions execute a block of code repeatedly while a condition is true. However, unlike a while expression, it first executes the block before the condition.

example_06.utx

# Increment count variable using do

count = 0;
do {
    render count + "\r\n";
    count = count + 1;
} while(count < 5);

While Expression

While expressions execute a block of code as long as a specified condition remains true.

example_07.utx

# Increment i variable using while

i = 0;
while(i < 10) {
    render i + "\r\n";
    i = i + 1;
};

If-else Expression

If expressions evaluate a condition and execute a block of code if the condition is true. Optionally, an else block can be provided for when the condition is false.

example_08.utx

# Checking x if positive or negative number with if-else

x = 10;
if(x > 0)
    render "x is a positive number"
else render "x is a negative number";

Random Expression

Random expressions execute a block of code randomly, with an optional else block for when the condition is not met.

example_09.utx

# Execute a render expression randomly.

random
    render "Randomly rendered string.\r\n"
else render "Executing the else-clause.\r\n";

Loop Expression

Loop expressions provide a traditional for-loop structure for iterating over a range of values or conditions.

example_10.utx

# Loop expression example equivalent to for-loop in other languages.

loop(i = 0; i < 10; i = i + 1)
    render i + "\r\n";

Unless Expression

Unless expressions execute a block of code if a condition is false, with an optional else block for when the condition is true.

example_11.utx

# Execute unless expression if x is not 0.

x = 3.14;
unless(x == 0)
    render "x is not zero.\r\n"
else render "x is zero\r\n";

When Expression

When expressions are used for pattern matching and conditional execution based on multiple conditions. The concept for this expression is same as the switch statement on other programming languages such as C/C++, Java, or C#.

example_12.utx

# When expression to render equivalent day for a number.

day = 1;
when(day) {
    if(0) render "Monday",
    if(1) render "Tuesday",
    if(2) render "Wednesday",
    if(3) render "Thursday",
    if(4) render "Friday",
    else  render "Weekday"
};

Maybe Expression

The maybe expression assigns a boolean value at runtime, randomly choosing between true or false. This value is determined using a random function from the Raspberry Pi Pico.

example_13.utx

# Print a string if the maybe has a true value.

should_print = maybe;
if(should_print)
    render "Maybe had a true value."
else render "Maybe had a false value.";

Function Expression

Function expressions define a function, optionally with parameters, which encapsulates a block of code.

example_14.utx

# Function example for printing a greeting message.

greet = func(name) {
    render "Hello, " + name + "!\r\n";
};

greet("world");

Array Expressions

Array expressions create an array of values and/or elements. An array might contain combinations of number, string, function, nil, boolean values, or even another array.

example_15.utx

# Simple array example.

array = ["Hello", "Kamusta", "Konnichiwa"];
render array[1] + "\r\n";

example_16.utx

# Multidimensional array example.

array = [[0, 1, 2], [3, 4, 5], [6, 7, 8]];
render array;
render "\r\n";

array[1][1] = "Four";
render array;
render "\r\n";

Logical and Bitwise Expressions

These expressions perform logical and bitwise operations. While the logical operators (|| and &t&) returns boolean values, the bitwise operators (| and &) returns number values.

example_17.utx

# Logical and bitwise operation examples.

render (true || false) +  "\r\n"; # Logical or
render (true && false) +  "\r\n"; # Logical and
render (8 | 9) +  "\r\n";         # Bitwise or
render (8 & 9) +  "\r\n";         # Bitwise and
render (8 ^ 9) + "\r\n";          # Bitwise xor

Equality and Comparison Expressions

These expressions compare values and return boolean results, useful for conditional logic.

example_18.utx

# Comparison operator examples.

render (8 == 9) +  "\r\n";   # Equals
render (8 != 9) +  "\r\n";   # Not equals
render (8 < 9) +  "\r\n";    # Less than equals
render (8 > 9) +  "\r\n";    # Greater than equals
render (8 <= 9) +  "\r\n";   # Less than equals
render (8 >= 9) +  "\r\n";   # Greater than equals

Shift Expressions

Shift expressions perform bitwise shifts on numbers, either to the left or right.

example_19.utx

# Shift operator examples.

render (8 << 9) + "\r\n"; # Left shift expression
render (8 >> 9) + "\r\n"; # Right shift expression

Term and Factor Expressions

Term and factor expressions involve basic arithmetic operations such as addition, subtraction, multiplication, division, and modulus.

example_20.utx

# Term and factor operator examples.

render (8 + 9) + "\r\n";  # Add
render (8 - 9) + "\r\n";  # Subtract
render (8 / 9) + "\r\n";  # Divide
render (8 * 9) + "\r\n";  # Multiply
render (8 % 9) + "\r\n";  # Reminder/Modulus

Literal Expressions

Literal expressions represent constant values in the language, such as numbers, booleans, and nil.

example_21.utx

# Literal expression examples.

render 3.14 + "\r\n";   # Number
render true + "\r\n";   # True
render false + "\r\n";  # False
render nil;             # Nil expression

Statements

Statements in Uartix are instructions that perform actions. They can control the flow of execution, declare variables, or perform expressions.


Use Statement

The use statement imports variables, functions, and other elements from specified Uartix script files, allowing for modular and reusable code.

example_22_say.utx

# Declares `greet` function variable

greet = func(name)
    render "Hello, " + name;

example_22_main.utx

# Uses the `greet` function variable from file `example_22_say.utx`

use "example_22_say.utx";
greet("world");

Break Statement

The break statement exits the nearest enclosing loop prematurely, often used to halt loop execution based on a condition.

example_23.utx

# Break loop if i is 5.

loop(i = 0; i < 10; i = i + 1) {
    if(i == 5) {
        break;
    };

    render i + "\r\n";
};

Continue Statement

The continue statement skips the current iteration of the nearest enclosing loop and proceeds with the next iteration, typically used to bypass certain conditions within a loop.

example_24.utx

# Skip loop iteration if i is even number.

loop(i = 0; i < 10; i = i + 1) {
    if(i % 2 == 0) {
        continue;
    };

    render i + "\r\n";
};

Return Statement

The return (ret) statement exits a function and optionally returns a value. If a block is a non-function, do not use return statement instead just put the return value inside an expression statement.

example_25.utx

# Return value from a function.

add = func(x, y) {
    ret x + y;
};

render "The sum of x and y is " + add(8, 9);

Throw Statement

The throw statement raises an exception, which can be handled by a catch-handle block. This mechanism is used for error handling in Uartix scripts.

example_26.utx

# Throw with string expression example.

check_value = func(value) {
    if(value < 0) {
        throw "Negative value not allowed!";
    };

    ret value;
};

catch {
    check_value(-1);
}
handle error {
    render "Error: " + error + "\r\n";
};

Test Statement

The test statement defines a test unit with a suite name and an expression for validation. It is used for on-code testing, providing reverse assertions to verify expected outcomes.

example_27.utx

# Test unit example for checking odd and even number.

test("Is 888 Odd or Even Test")
    (888 % 2 == 0);

Strings

In Uartix, strings are sequences of characters enclosed in double quotes ("). Strings are used to represent text and can contain a variety of characters, including letters, numbers, punctuation, and special characters.

Additionally, strings can be concatenated using the + operator and can also be reversed with the ~ unary operator. The multiplication (*) operator can also multiply a string.

example_28.utx

# String operation example.

render (~"Hello") + "\r\n";
render "Go! " * 3;

Escape Sequences

Escape sequences are used in strings to represent special characters that cannot be directly included in the string. An escape sequence begins with a backslash (\) followed by one or more characters. Uartix supports several escape sequences:

  • Newline (\n)

    Represents a newline character, which moves the cursor to the beginning of the next line.

  • Carriage Return (\r)

    Represents a carriage return character, which moves the cursor to the beginning of the current line.

  • Tab (\t)

    Represents a tab character, which moves the cursor to the next tab stop.

  • Backspace (\b)

    Represents a backspace character, which moves the cursor one position to the left.

  • Form Feed (\f)

    Represents a form feed character, which moves the cursor to the beginning of the next page.

  • Double Quote (\")

    Represents a double quote character, allowing double quotes to be included within strings.

  • Backslash (\\)

    Represents a backslash character, allowing backslashes to be included within strings.

  • Unicode (\uXXXX)

    Represents a Unicode character, where XXXX is a four-digit hexadecimal number corresponding to the Unicode code point.

example_29.utx

# String escape sequence examples.

render "Hello,\nWorld!\r\n";
render "Hello,\rWorld!\r\n";
render "Hello,\tWorld!\r\n";
render "Hello,\bWorld!\r\n";
render "Hello,\fWorld!\r\n";
render "They say, \"Hello, World!\"\r\n";
render "This is a backslash: \\\r\n";
render "Smiley face: \u263A\r\n";

Digits and Numbers

In Uartix, numbers are a fundamental data type used for arithmetic operations, comparisons, and other numerical computations. Uartix supports a variety of numerical formats, including standard decimal numbers, as well as binary, trinary, octal, and hexadecimal numbers. This allows developers to work with different numerical bases efficiently.

  • Decimal Numbers

    Decimal numbers are the most commonly used numerical format. They can include integer values or floating-point values with decimals. Uartix uses double-precision floating-point representation for numbers with decimals, ensuring high accuracy for mathematical computations.

  • Binary Numbers

    Binary numbers in Uartix are prefixed with 0b and consist of the digits 0 and 1. This format is useful for low-level programming, bitwise operations, and when working with binary data.

  • Trinary Numbers

    Trinary numbers are prefixed with 0t and can include the digits 0, 1, and 2. While less common than other bases, trinary can be useful in specific computational contexts.

  • Octal Numbers

    Octal numbers are prefixed with 0c and use the digits 0 through 7. Octal representation is often used in computing for compact binary representation, as each octal digit corresponds to three binary digits.

  • Hexadecimal Numbers

    Hexadecimal numbers are prefixed with 0x and can include the digits 0 through 9 and the letters a through f (or A through F). Hexadecimal is widely used in computing for memory addresses, color codes, and other applications requiring a compact representation of binary data.

example_30.utx

# Different number bases examples.

binary_num  = 0b1101;
trinary_num = 0t102;
octal_num   = 0c17;
hex_num     = 0xf;

render binary_num + "\r\n";
render trinary_num + "\r\n";
render octal_num + "\r\n";
render hex_num + "\r\n";

render 8.9e-3 + "\r\n";   # 0.0089
render 8.9e+3 + "\r\n";   # 8900.0



Copyright 2024 © Nathanne Isip