The ucode programming language features a syntax that closely resembles ECMAScript 6. However, the ucode interpreter supports two distinct syntax modes: template mode and raw mode. In template mode, ucode consumes Jinja-like templates that allow for the embedding of script code within the template structure. This mode enables the combination of expressive template constructs with JavaScript like functionality. On the other hand, raw mode in ucode directly consumes ECMAScript 6-like syntax without any template-specific markup. This mode is mainly useful to develop standalone applications or libraries. ## Template mode By default, *ucode* is executed in *raw mode*, means it expects a given source file to only contain script code. By invoking the ucode interpreter with the `-T` flag or by using the `utpl` alias, the *ucode* interpreter is switched into *template mode* where the source file is expected to be a plaintext file containing *template blocks* containing ucode script expressions or comments. ### Block types There are three kinds of blocks; *expression blocks*, *statement blocks* and *comment blocks*. The former two embed code logic using ucode's JavaScript-like syntax while the latter comment block type is simply discarded during processing. #### 1. Statement block Statement blocks are enclosed in an opening `{%` and a closing `%}` tag and may contain any number of script code statements, even entire programs. It is allowed to omit the closing `%}` of a statement block to parse the entire remaining source text after the opening tag as ucode script. By default, statement blocks produce no output and the entire block is reduced to an empty string during template evaluation but contained script code might invoke functions such as `print()` to explicitly output contents. For example the following template would result in `The epoch is odd` or `The epoch is even`, depending on the current epoch value: `The epoch is {% if (time() % 2): %}odd{% else %}even{% endif %}!` #### 2. Expression block Expression blocks are enclosed in an opening `{{` and a closing `}}` tag and may only contain a single expression statement (multiple expressions may be chained with comma). The implicit result of the rightmost evaluated expression is used as output when processing the block. For example the template `Hello world, {{ getenv("USER") }}!` would result in the output "Hello world, user!" where `user` would correspond to the name of the current user executing the ucode interpreter. #### 3. Comment block Comment blocks, which are denoted with an opening `{#` and a closing `#}` tag may contain arbitrary text except the closing `#}` tag itself. Comments blocks are completely stripped during processing and are replaced with an empty string. The following example template would result in the output "Hello world": `Hello {# mad #}word` ### Whitespace handling Each block start tag may be suffixed with a dash to strip any whitespace before the block and likewise any block end tag may be prefixed with a dash to strip any whitespace following the block. Without using whitespace stripping, the following example: ``` This is a first line {% for (x in [1, 2, 3]): %} This is item {{ x }}. {% endfor %} This is the last line ``` Would result in the following output: ``` This is a first line This is item 1. This is item 2. This is item 3. This is the last line ``` By adding a trailing dash to apply whitespace stripping after the block, the empty lines can be eliminated: ``` This is a first line {% for (x in [1, 2, 3]): -%} This is item {{ x }}. {% endfor -%} This is the last line ``` Output: ``` This is a first line This is item 1. This is item 2. This is item 3. This is the last line ``` By applying whitespace stripping before the block, all lines can be joined into a single output line: ``` This is a first line {%- for (x in [1, 2, 3]): -%} This is item {{ x }}. {%- endfor -%} This is the last line ``` Output: ``` This is a first lineThis is item 1.This is item 2.This is item 3.This is the last line ``` ## Script syntax The ucode script language - used either within statement and expression blocks or throughout the entire file in *raw mode*, uses untyped variables and employs a simplified JavaScript like syntax. The language implements function scoping and differentiates between local and global variables. Each function has its own private scope while executing and local variables declared inside a function are not accessible in the outer calling scope. ### 1. Data types Ucode supports seven different basic types as well as two additional special types; function values and ressource values. The supported types are: - Boolean values (`true` or `false`) - Integer values (`-9223372036854775808` to `+9223372036854775807`) - Double values (`-1.7e308` to `+1.7e308`) - String values (e.g. `'Hello world!'` or `"Sunshine \u2600!"`) - Array values (e.g. `[1, false, "foo"]`) - Object values (e.g. `{ foo: true, "bar": 123 }`) - Null value (`null`) Ucode utilizes reference counting to manage memory used for variables and values and frees data automatically as soon as values go out of scope. Numeric values are either stored as signed 64bit integers or as IEEE 756 double value. Conversion between integer and double values can happen implicitly, e.g. through numeric operations, or explicitely, e.g. by invoking functions such as `int()`. ### 2. Variables Variable names must start with a letter or an underscore and may only contain the characters `A`..`Z`, `a`..`z`, `0`..`9` or `_`. By prefixing a variable name with the keyword `let`, it is declared in the local block scope only and not visible outside anymore. Variables may also be declared using the `const` keyword. Such variables follow the same scoping rules as `let` declared ones but they cannot be modified after they have been declared. Any attempt to do so will result in a syntax error during compilation. ```javascript {% a = 1; // global variable assignment function test() { let b = 2; // declare `b` as local variable a = 2; // overwrite global a } test(); print(a, "\n"); // outputs "2" print(b, "\n"); // outputs nothing const c = 3; print(c, "\n"); // outputs "3" c = 4; // raises syntax error c++; // raises syntax error const d; // raises syntax error, const variables must // be initialized at declaration time %} ``` ### 3. Control statements Similar to JavaScript, ucode supports `if`, `for` and `while` statements to control execution flow. #### 3.1. Conditional statement If/else blocks can be used to execute statements depending on a condition. ```javascript {% user = getenv("USER"); if (user == "alice") { print("Hello Alice!\n"); } else if (user == "bob") { print("Hello Bob!\n"); } else { print("Hello guest!\n"); } %} ``` If only a single statement is wrapped by an if or else branch, the enclosing curly braces may be omitted: ```javascript {% if (rand() == 3) print("This is quite unlikely\n"); %} ``` #### 3.2. Loop statements Ucode script supports three different flavors of loop control statements; a `while` loop that executes enclosed statements as long as the loop condition is fulfilled, a `for in` loop that iterates keys of objects or items of arrays and a counting `for` loop that is a variation of the `while` loop. ```javascript {% i = 0; arr = [1, 2, 3]; obj = { Alice: 32, Bob: 54 }; // execute as long as condition is true while (i < length(arr)) { print(arr[i], "\n"); i++; } // execute for each item in arr for (n in arr) { print(n, "\n"); } // execute for each key in obj for (person in obj) { print(person, " is ", obj[person], " years old.\n"); } // execute initialization statement (j = 0) once // execute as long as condition (j < length(arr)) is true // execute step statement (j++) after each iteration for (j = 0; j < length(arr); j++) { print(arr[j], "\n"); } %} ``` #### 3.3. Alternative syntax Since conditional statements and loops are often used for template formatting purposes, e.g. to repeat a specific markup for each item of a list, ucode supports an alternative syntax that does not require curly braces to group statements but that uses explicit end keywords to denote the end of the control statement body for better readability instead. The following two examples first illustrate the normal syntax, followed by the alternative syntax that is more suitable for statement blocks: ``` Printing a list: {% for (n in [1, 2, 3]) { -%} - Item #{{ n }} {% } %} ``` The alternative syntax replaces the opening curly brace (`{`) with a colon (`:`) and the closing curly brace (`}`) with an explicit `endfor` keyword: ``` Printing a list: {% for (n in [1, 2, 3]): -%} - Item #{{ n }} {% endfor %} ``` For each control statement type, a corresponding alternative end keyword is defined: - `if (...): ... endif` - `for (...): ... endfor` - `while (...): ... endwhile` ### 4. Functions Ucode scripts may define functions to group repeating operations into reusable operations. Functions can be both declared with a name, in which case they're automatically registered in the current scope, or anonymously which allows assigning the resulting value to a variable, e.g. to build arrays or objects of functions: ```javascript {% function duplicate(n) { return n * 2; } let utilities = { concat: function(a, b) { return "" + a + b; }, greeting: function() { return "Hello, " + getenv("USER") + "!"; } }; -%} The duplicate of 2 is {{ duplicate(2) }}. The concatenation of 'abc' and 123 is {{ utilities.concat("abc", 123) }}. Your personal greeting is: {{ utilities.greeting() }}. ``` #### 4.1. Alternative syntax Function declarations support the same kind of alternative syntax as defined for control statements (3.3.) The alternative syntax replaces the opening curly brace (`{`) with a colon (`:`) and the closing curly brace (`}`) with an explicit `endfunction` keyword: ``` {% function printgreeting(name): -%} Hallo {{ name }}, nice to meet you. {% endfunction -%}