CsLox is a C# implementation of the Lox programming language interpreter, based on the "Crafting Interpreters" book by Robert Nystrom.
Lox is a lightweight, high-level, dynamically typed, and interpreted language designed for learning and exploring programming language theory and interpreter construction.
CsLox aims to provide a robust and efficient interpreter that adheres closely to the specifications and philosophy of the original Lox language while leveraging the strengths of C#.
- Full implementation of the Lox language
- Dynamic typing
- Automatic memory management
- First-class functions
- Closures
- Classes and inheritance
- .NET Core 8.0 or higher
Clone the repository to your local machine:
git clone https://github.com/yourusername/CsLox.git
cd CsLox
Build the project using .NET CLI:
dotnet build
To run a Lox program, use the following command:
dotnet run --project CsLox path/to/your_script.lox
Alternatively, to start an interactive REPL:
dotnet run --project CsLox
Lox is a dynamically-typed programming language designed for ease of use and clarity. It supports object-oriented principles and offers a robust standard library.
- Boolean: true or false
- Number: All numbers are floating point. Example: 123, 45.67
- String: Text enclosed in double quotes. Example: "Hello, world!"
- Nil: Represents the absence of a value or null.
Declare variables using the var
keyword. Variables must be initialized upon declaration.
var name = "Lox";
var version = 1.0;
-
Conditional Statements:
if (condition) { // statements } else { // statements }
-
While Loop:
if (condition) { // statements } else { // statements }
-
For Loop:
for (var i = 0; i < 10; i = i + 1) { print i; }
Define functions using the fun
keyword. Functions may return values using return
.
fun greet(name) {
print "Hello " + name + "!";
}
Lox supports simple classes with inheritance.
class Animal {
fun speak() {
print "Some noise";
}
}
class Dog < Animal {
fun speak() {
print "Bark";
}
}
- Arithmetic: +, -, *, /
- Comparison: ==, !=, <, >, <=, >=
- Logical: and, or, !
Use // for single-line comments.
// This is a comment
-
print: Outputs a string to the console.
print "Hello, world!";
The scanner supports various tokens including:
- Keywords: and, class, else, false, for, fun, if, nil, or, print, return, super, this, true, var, while
- Symbols: (, ), {, }, ,, ., -, +, ;, *, /, !, =, <, >
- Literals: Identifiers, strings, numbers
- Miscellaneous: Comments, whitespace (spaces, tabs, and new lines are ignored except to separate tokens)
- Robert Nystrom, for his excellent book "Crafting Interpreters," which inspired this project.
- The C# community for their continuous support and resources.