A simple tree-walk interpreter written in Rust for an imaginary language.
- Description
- Installation
- Usage
- Features
- Language Grammar
- Contributing
- License
- Acknowledgements
This project is a Rust-based interpreter that executes a custom programming language designed for educational purposes and efficient performance.
To install and run the interpreter, follow these steps:
- Clone the repository:
git clone https://github.com/yaegeristhitesh/RLox
- Navigate to the project directory:
cd rlox-basic/
- Build the project:
cargo build --release
After building the project, you can run the interpreter with the following command:
./target/release/rlox_basic yourscript.lox
- Basic arithmetic operations: Support for addition, subtraction, multiplication, and division.
- Variable declarations: Ability to declare and use variables in scripts.
- Control structures: Includes if-else statements and while loops for flow control.
- Functions: Define and call functions with parameters and return values.
- Standard library: A small standard library with useful functions for common tasks.
Lox's syntax is similar to C, using semicolons to terminate statements and allowing single-line comments with //
. For example:
print "Hello, world!";
Lox avoids static typing, embracing a dynamic typing system to simplify its syntax and reduce complexity.
Lox uses dynamic typing, meaning that variables can hold values of any type and change types during execution. Type errors are detected at runtime. This approach simplifies language implementation and avoids the complexity of static type systems.
Lox employs tracing garbage collection (GC) to manage memory automatically. This technique is more robust than reference counting, handling cyclic references and simplifying memory management compared to manual allocation and deallocation.
Lox supports the following built-in data types:
true
andfalse
are the Boolean values.- Example:
true; // Boolean true false; // Boolean false
- Only double-precision floating-point numbers are supported.
- Example:
1234; // Integer representation 12.34; // Decimal number
- Strings are enclosed in double quotes.
- Example:
"I am a string"; "" // Empty string "123" // String containing digits
- Represents a non-value, similar to
null
in other languages. - Example:
nil;
- Operators:
+
,-
,*
,/
- Example:
3 + 4; // 7 -5; // -5
- Operators:
<
,<=
,>
,>=
,==
,!=
- Example:
5 < 10; // true "cat" == "dog"; // false
and
,or
,!
- Example:
true and false; // false !true; // false
- Operators follow typical precedence rules, and parentheses can be used for grouping.
- Example:
(1 + 2) * 3; // 9
- Expressions can be promoted to statements with a trailing semicolon.
- Example:
print "Hello";
- Group multiple statements within
{}
. - Example:
{ print "One statement."; print "Two statements."; }
- Use
var
to declare variables. Variables default tonil
if not initialized. - Example:
var x = 10; var y; y = x;
- Executes code based on a condition.
- Example:
if (x > 0) { print "Positive"; } else { print "Non-positive"; }
- Repeats code as long as a condition is true.
- Example:
while (x > 0) { print x; x = x - 1; }
- Executes code with initialization, condition, and iteration.
- Example:
for (var i = 0; i < 5; i = i + 1) { print i; }
- Functions are called with parentheses, with or without arguments.
- Example:
sum(1, 2);
- Define functions with
fun
. - Example:
fun greet(name) { print "Hello, " + name; }
- Functions can reference variables from their enclosing scopes.
- Example:
fun makeCounter() { var count = 0; fun increment() { count = count + 1; return count; } return increment; }
- Define classes with
class
, including methods. - Example:
class Person { greet() { print "Hello!"; } }
- Create instances by calling the class like a function.
- Example:
var person = Person();
- Define an
init
method for initialization. - Example:
class Person { init(name) { this.name = name; } greet() { print "Hello, " + this.name; } }
We welcome contributions! Please follow these steps to contribute:
- Fork the repository.
- Create a new branch (
git checkout -b feature/your-feature-name
). - Commit your changes (
git commit -m 'Add some feature'
). - Push to the branch (
git push origin feature/your-feature-name
). - Open a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.
- Inspired by the book "Crafting Interpreters" by Robert Nystrom.
- Thanks to the Rust community for their support and contributions.