Skip to content

Tonic-Box/TLang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TLang

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

Table of Contents


1. Lexical Structure

1.1 Comments

  • Single-line:
    // Comment until end of line
    
  • Block:
    /* Comment until closing */
    

1.2 Whitespace

  • Spaces, tabs, newlines:
    Ignored except as separators between tokens.

1.3 Keywords

def, if, else, while, for, print, true, false, break, continue

1.4 Identifiers

  • Rules:
    [a-zA-Z_][a-zA-Z0-9_]*
    (e.g., x, myVar1)

1.5 Literals

  • Integers:
    [0-9]+ (e.g., 42). A preceding minus sign denotes a negative number (e.g., -5).
  • Booleans:
    true or false.
  • 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.)

2. Types

2.1 Primitive Types

  • int: Integer values.
  • bool: Boolean values.
  • string: String values.

2.2 Arrays

  • Arrays are defined by appending [] to a type.
    Example: int[] arr = [1, 2, 3];

2.2.1 Multi-dimensional Arrays

  • Multi-dimensional arrays are supported by appending additional [] (e.g., int[][] matrix = [[1,2], [3,4]];).

3. Variables

3.1 Declaration

  • Syntax:
    type ID = expr;
    Multiple variables can be declared in one statement, separated by commas.
    Example:
    int x = 5, y = 10;
    

3.2 Assignment

  • Syntax:
    ID = expr;
    Example:
    x = x + 1;
    

3.3 Array Assignment

  • Syntax:
    expr[expr] = expr;
    Example:
    arr[0] = 42;
    

4. Expressions

4.1 Operators

4.1.1 Arithmetic Operators

  • Operators: +, -, *, /, %
  • Notes:
    • The + operator also supports string concatenation.
    • Unary minus is supported for negative numbers.

4.1.2 Comparison Operators

  • Operators: ==, !=, <, >, <=, >=

4.1.3 Logical Operators

  • Operators: &&, ||, !
  • Usage:
    Logical expressions evaluate to true or false.

4.1.4 Bitwise & Exponentiation Operators

  • Bitwise Operators:
    • ^ (typically used for bitwise XOR)
    • ~ (bitwise NOT)
  • Exponentiation Operator:
    • ** (raises the left-hand operand to the power of the right-hand operand)

4.1.5 Compound Assignment Operators

  • Operators: +=, -=, *=, /=
  • Notes:
    • The += operator is also supported for strings (for concatenation).

4.1.6 Increment/Decrement Operators

  • Operators:
    • Pre-increment: ++var
    • Post-increment: var++
    • Pre-decrement: --var
    • Post-decrement: var--
  • Usage:
    These operators can be applied to both variables and array elements.

4.1.7 Ternary Operator

  • Syntax:
    condition ? expr_if_true : expr_if_false
  • Example:
    int max = (a > b) ? a : b;
    

4.2 Operator Precedence

Operators in TLang are evaluated in the following order (from highest to lowest precedence):

  1. Parentheses: (expr)
  2. Unary Operators: -, !, ++, --, ~
  3. Exponentiation: **
  4. Multiplicative: *, /, %
  5. Additive: +, -
  6. Bitwise: ^ (when used as bitwise XOR)
  7. Comparison: <, >, <=, >=, ==, !=
  8. Logical AND/OR: &&, ||
  9. Ternary: ?:
  10. Assignment & Compound Assignments: =, +=, -=, *=, /=

4.3 Array Access

  • Syntax:
    expr[expr]
  • Example:
    int value = arr[i];
    

4.4 Method Calls

  • Syntax:
    ID(arg1, arg2, ...)
  • Example:
    computeSum(a, b);
    

5. Control Structures

5.1 if Statement

  • 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");
    

5.2 while Loop

  • 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");
    

5.3 for Loop

  • 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 true if omitted).
    • The update is optional.
    • Single instruction bodies do not require brackets.

5.4 for-each Loop

  • Syntax:
    for (var element : collection) {
      // loop body using element
    }
    
  • Example:
    int[] numbers = [1, 2, 3, 4];
    for (var num : numbers)
      print(num);
    

5.5 Break and Continue

  • Break:
    Immediately exits the closest enclosing loop.
    break;
    
  • Continue:
    Skips the remainder of the current loop iteration and continues with the next iteration.
    continue;
    

6. Functions

6.1 Declaration

  • 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
    }
    

6.2 Calling Functions

  • Examples:
    myFunc(5, true); // As a statement
    int result = add(12, 17); // Using the returned value
    

6.3 Default Parameter Values

  • Functions—including library methods—support default arguments, making it optional to supply every parameter during a call.

7. Built-in Functions & Libraries

7.1 print

  • Syntax:
    print(expr);
    
  • Example:
    print("Hello world!");
    

7.2 Library Setup & Default Args

  • 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. Only int, boolean, and string (and void for return type) are permitted in library method signatures.

8. Examples

8.1 Factorial Function

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

8.2 Array Manipulation

int[] data = [10, 20, 30];
data[1] = data[1] * 2; // data becomes [10, 40, 30]

9. Notes

  • Scoping:
    Variables are block-scoped (e.g., within {}).
  • Type Safety:
    No implicit type conversions are allowed.
  • Function Returns:
    Functions return void by default unless a return statement is provided.

About

A simple procedural scripting language that compiles to JVM bytecode.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors