TLang is a simple proof-of-concept scripting language with a compiler implementation that targets JVM bytecode. In addition to basic procedural constructs, TLang now supports a variety of new operators, control structures, and conveniences that make it more expressive and concise.
Demo scripts can be found here: https://github.com/Tonic-Box/TLang/tree/main/src/main/resources/com/tonic/demo
- 1. Lexical Structure
- 2. Types
- 3. Variables
- 4. Expressions
- 5. Control Structures
- 6. Functions
- 7. Built-in Functions & Libraries
- 8. Examples
- 9. Notes
- Single-line:
// Comment until end of line - Block:
/* Comment until closing */
- Spaces, tabs, newlines:
Ignored except as separators between tokens.
def, if, else, while, for, print, true, false, break, continue
- Rules:
[a-zA-Z_][a-zA-Z0-9_]*
(e.g.,x,myVar1)
- Integers:
[0-9]+(e.g.,42). A preceding minus sign denotes a negative number (e.g.,-5). - Booleans:
trueorfalse. - Strings:
Enclosed in double quotes ("text"). Supports standard escape sequences including character escapes (e.g.,\n,\t) and Unicode escapes (e.g.,\u1234). - Arrays:
A comma-separated list of expressions enclosed in square brackets.
Example:[1, 12, 7 * 3]
(Arrays support all types.)
- int: Integer values.
- bool: Boolean values.
- string: String values.
- Arrays are defined by appending
[]to a type.
Example:int[] arr = [1, 2, 3];
- Multi-dimensional arrays are supported by appending additional
[](e.g.,int[][] matrix = [[1,2], [3,4]];).
- Syntax:
type ID = expr;
Multiple variables can be declared in one statement, separated by commas.
Example:int x = 5, y = 10;
- Syntax:
ID = expr;
Example:x = x + 1;
- Syntax:
expr[expr] = expr;
Example:arr[0] = 42;
- Operators:
+,-,*,/,% - Notes:
- The
+operator also supports string concatenation. - Unary minus is supported for negative numbers.
- The
- Operators:
==,!=,<,>,<=,>=
- Operators:
&&,||,! - Usage:
Logical expressions evaluate totrueorfalse.
- Bitwise Operators:
^(typically used for bitwise XOR)~(bitwise NOT)
- Exponentiation Operator:
**(raises the left-hand operand to the power of the right-hand operand)
- Operators:
+=,-=,*=,/= - Notes:
- The
+=operator is also supported for strings (for concatenation).
- The
- Operators:
- Pre-increment:
++var - Post-increment:
var++ - Pre-decrement:
--var - Post-decrement:
var--
- Pre-increment:
- Usage:
These operators can be applied to both variables and array elements.
- Syntax:
condition ? expr_if_true : expr_if_false - Example:
int max = (a > b) ? a : b;
Operators in TLang are evaluated in the following order (from highest to lowest precedence):
- Parentheses:
(expr) - Unary Operators:
-,!,++,--,~ - Exponentiation:
** - Multiplicative:
*,/,% - Additive:
+,- - Bitwise:
^(when used as bitwise XOR) - Comparison:
<,>,<=,>=,==,!= - Logical AND/OR:
&&,|| - Ternary:
?: - Assignment & Compound Assignments:
=,+=,-=,*=,/=
- Syntax:
expr[expr] - Example:
int value = arr[i];
- Syntax:
ID(arg1, arg2, ...) - Example:
computeSum(a, b);
- Block Form:
if (condition) { // then block } else { // else block } - Single Instruction Form:
If the statement is a single instruction, the curly braces are optional:if (condition) print("Single instruction"); else print("Single instruction else");
- Block Form:
while (condition) { // loop body } - Single Instruction Form:
Curly braces can be omitted if the loop body is a single instruction:while (condition) print("Looping");
- Traditional for-loop Syntax:
for (int i = 0; i < 10; i = i + 1) { // loop body } - Notes:
- The initialization section can be a variable declaration or an assignment.
- The condition is optional (defaults to
trueif omitted). - The update is optional.
- Single instruction bodies do not require brackets.
- Syntax:
for (var element : collection) { // loop body using element } - Example:
int[] numbers = [1, 2, 3, 4]; for (var num : numbers) print(num);
- Break:
Immediately exits the closest enclosing loop.break; - Continue:
Skips the remainder of the current loop iteration and continues with the next iteration.continue;
- Syntax:
def functionName(type1 param1, type2 param2) { // function body } - Default Parameter Values:
Default values can be provided:def myFunc(int param1 = 12, bool param2 = false) { // body }
- Examples:
myFunc(5, true); // As a statement int result = add(12, 17); // Using the returned value
- Functions—including library methods—support default arguments, making it optional to supply every parameter during a call.
- Syntax:
print(expr); - Example:
print("Hello world!");
- Library Setup:
New libraries can be added by placing methods in the designated library directory (see Builtins.java). - Default Arguments:
Library methods also support default parameter values. Onlyint,boolean, andstring(andvoidfor return type) are permitted in library method signatures.
def factorial(n int) {
int result = 1;
// Using a traditional for loop with single instruction body
for (int i = 1; i <= n; i = i + 1)
result = result * i;
print(result);
}
factorial(5); // Prints 120
int[] data = [10, 20, 30];
data[1] = data[1] * 2; // data becomes [10, 40, 30]
- Scoping:
Variables are block-scoped (e.g., within{}). - Type Safety:
No implicit type conversions are allowed. - Function Returns:
Functions returnvoidby default unless areturnstatement is provided.