Tralalero_lang is a small, interpreted programming language designed for simplicity and a touch of whimsy. It's built with Rust and aims to provide a straightforward environment for learning basic programming concepts, experimenting with language design, or simply having fun. The language uses a unique set of keywords inspired by playful Italian phrases, making the coding experience a bit more lighthearted.
This will compile the `main.rs` file and create an executable in the `target/debug/` directory (e.g., `target/debug/Tralalero_lang.exe` on Windows, or `target/debug/Tralalero_lang` on Linux/macOS).
Basic arithmetic operations (`+`, `-`, `*`, `/`) can be performed within `let` statements. The current implementation supports simple binary operations (operand operator operand).
Unire Corde final_greeting greeting_part "morning"; // final_greeting will be "Goodmorning"
```
**Note:** Due to current parsing limitations, avoid spaces within string literals when directly used with `Unire Corde`. It's recommended to assign strings with spaces to variables first, then use the variables for concatenation.
### Conditional Statements
Conditional logic is implemented using `Tung Tung Tung` (if) and `Ballerina Cappuccina` (else). Supported comparison operators are `==`, `!=`, `>`, `<`, `>=`, `<=`.
Functions are defined using `Lirili Larila` and called using `Trippi Troppi`. Functions can take arguments.
```tralla
Lirili Larila greet (name)
{
Matteeeo "Hello, ";
Matteeeo name;
}
Trippi Troppi greet("Alice"); // Calls the greet function with "Alice"
```
### Known Limitations
***Function Return Values:** Functions currently execute their body but do not return values that can be captured or used in the calling scope. Any `return` statements within a function are not processed to pass a value back.
***Complex Expressions:** The `let` keyword's arithmetic parsing is limited to simple `operand operator operand` structures. More complex mathematical expressions (e.g., `(a + b) * c`) are not supported.
***String Literals with Spaces in `Unire Corde`:** Directly using string literals containing spaces with `Unire Corde` can lead to parsing issues. Assign such strings to variables first.
## Examples
The `examples/` directory contains several `.tralla` files demonstrating various language features:
*`calculator.tralla`: Basic arithmetic operations and variable usage.
*`fibonacci.tralla`: Demonstrates loops and function calls (note the return value limitation).
This section provides a brief overview of how the Tralalero_lang interpreter is structured and some of the design choices and challenges encountered during its development.
### Interpreter Core
The interpreter is implemented in `src/main.rs` as a single-pass, line-by-line parser and executor. It reads the `.tralla` file content, splits it into lines, and then processes each line sequentially.
### Parsing Strategy
***Line-by-Line Processing:** The interpreter reads and executes code one line at a time.
***Comment Stripping:** Before processing, each line is checked for `//` (single-line comments), and any content after `//` is removed.
***Whitespace Splitting:** Commands and arguments are primarily parsed by splitting the line by whitespace. This simplicity leads to some of the known limitations, particularly with string literals containing spaces.
***Semicolon Trimming:** Trailing semicolons (`;`) are trimmed from expressions and variable names during parsing to ensure correct value retrieval and storage.
### Variable Handling
Variables are stored in a `HashMap<String, String>`, where the key is the variable name and the value is its string representation. Numeric values are converted to strings before storage and parsed back to `f64` when used in arithmetic operations or comparisons.
### Arithmetic and Expression Evaluation
***`let` Keyword:** The `let` keyword handles both direct variable assignments (e.g., `let x = 10;`) and simple binary arithmetic expressions (e.g., `let sum = a + b;`).
***`get_value` Function:** This helper function attempts to parse a given string as an `f64` literal. If it fails, it checks if the string corresponds to a variable name and, if so, attempts to parse the variable's string value as an `f64`.
***Limited Expression Parsing:** The current arithmetic parsing within `let` is limited to `operand operator operand`. More complex expressions require a more sophisticated parsing algorithm (e.g., shunting-yard or abstract syntax tree generation), which is not yet implemented.
### Function Call Mechanism
***Function Definition (`Lirili Larila`):** Functions are stored with their name, argument list, and the start/end program counter (PC) lines of their body.
***Function Call (`Trippi Troppi`):** When a function is called, a new local scope (a cloned `HashMap` of variables) is created. Arguments are passed by value into this local scope. The interpreter then executes the lines within the function's body.
***No Return Value Propagation:** A significant current limitation is that functions do not return values to the calling scope. Any `return` statements within a function only terminate its execution; the value is not captured or made available to the caller. This means functions primarily serve for side effects (e.g., printing).