Tomato Programming Language

Introduction

The Tomato Programming Language is a simple, high-level language designed for beginners to explore fundamental programming concepts. It directly integrates with an assembly-like language for low-level operations, making it ideal for learning computer architecture and programming simultaneously.

Language Features

1. Variables (Numbers)

Variables in Tomato are simple and hold numeric values. They are dynamically typed and can be initialized with any number from 0 to 65 535 (depend to computer architecture):

let x = 5;
let y = 10;
let sum = x + y;
            

Variables can be reassigned and used in mathematical operations.

2. Functions

Functions allow grouping reusable logic. The syntax is straightforward:

function add(a, b) {
    let result = a + b;
    return result;
}

let sum = add(3, 4);
            

Functions can take parameters and return values.

3. Conditional Instructions (if, if-else)

Conditional instructions control the program flow based on conditions:

if (x > 0) {
    print(1);
} else {
    print(0);
}
            

Nested conditions and logical operators are also supported.

4. Repetitive Instructions (while)

The while loop repeats a block of code as long as the condition is true:

let x = 5;
while (x > 0) {
    print(x);
    x = x - 1;
}
            

5. Comments

Tomato supports two types of comments:

Single-Line Comments

Use `//` to comment out a single line:

// This is a single-line comment
let x = 5; // This sets x to 5
            

Multi-Line Comments

Use `/* ... */` to write comments spanning multiple lines:

/*
This is a multi-line comment.
It can span several lines
and is ignored by the interpreter.
*/
let x = 5;
            

Comments are helpful for explaining code, documenting logic, or temporarily disabling parts of the code during debugging.

Custom Code Integration

The Tomato language supports embedding assembly-like instructions using %...%. This allows you to write low-level code for precise control:

function printAssembly(x) {
    %
    outa
    %
}
printAssembly(30);
            

In this example, the assembly instructions execute directly within the function, making it a hybrid between high- and low-level programming.

Examples

1. Sum

This program calculates the Sum number:

function sum(n) {
    if (n <= 1) {
        return n;
    } else {
        return n + sum(n - 1);
    }
}

let result = sum(6);
print(result); // Output: 21
            

2. Factorial

This program calculates the factorial of a number:

function factorial(n) {
    if (n == 0) {
        return 1;
    } else {
        return n * factorial(n - 1);
    }
}

let result = factorial(5);
print(result); // Output: 120