https://github.com/pawel-parma/lox-java
Lox implementation "following" the book: Crafting Interpreters
https://github.com/pawel-parma/lox-java
crafting-interpreters craftinginterpreters jlox lox lox-interpreter lox-language lox-programming-language
Last synced: 3 months ago
JSON representation
Lox implementation "following" the book: Crafting Interpreters
- Host: GitHub
- URL: https://github.com/pawel-parma/lox-java
- Owner: Pawel-Parma
- License: mit
- Created: 2024-05-21T16:12:53.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-06-16T19:29:01.000Z (about 1 year ago)
- Last Synced: 2025-02-05T13:51:23.566Z (5 months ago)
- Topics: crafting-interpreters, craftinginterpreters, jlox, lox, lox-interpreter, lox-language, lox-programming-language
- Language: Java
- Homepage:
- Size: 123 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Jlox
Lox interpreter written in Java following the book [Crafting Interpreters](http://www.craftinginterpreters.com/).
## Branches:
- ### [*main*](https://github.com/Pawel-Parma/lox-java)If you are in the main branch you are looking at the implementation derived from the book.
It has some solved challenges and my own modifications.Changes:
-
const - declaration for immutable data. Examples:
// Values
const x = 1;
x = 2; // Error at 'x': Cannot reassign a constant.
// Classes
class A {
init(b) { this.b = 1; }
}const a = A(1);
a.b = 2; // Error at 'b': Cannot modify a field of a constant object.
// But this is allowed:
var a_mut = a;
a.b = 2; // No error
-
break - statement for breaking out of loops. Example:
for (var i = 0; i < 10; i = i + 1) {
if (i == 5) break;
print i;
}Output:
// 0
// 1
// 2
// 3
// 4
-
continue - statement for skipping the rest of the loop body. Example:
for (var i = 0; i < 10; i = i + 1) {
if (i % 2 == 0) continue;
print i;
}Output:
// 1
// 3
// 5
// 7
// 9
-
Escape sequences in strings - [ \n, \r, \t, \b, \', \", \\ ]. Note " ' " still works, no need for " \' ".
Examples:
// New line
print "Hello\nWorld"; // Hello
// World
//
// Tab
print "Hello\tWorld"; // World World// Backspace
print "Hello\b World"; // Hell World
-
String and number concatenation using the '+' operator.// string + number
print "123" + 4; // 1234 (string)// number + string
print 4 + "123"; // 4123 (string)
-
def keyword before method declaration is now required.// Before
class A {
init() { print "A"; }
}// Now
class A {
def init() { print "A"; }
}
-
lambda expressions - anonymous functions. Examples:// Single line
const add = lambda(a, b) { return a + b; };
print add(1, 2); // 3// Multi line
const add = lambda(a, b) {
var c = a + b;
return c;
};
print add(3, 2); // 3
-
imports - importing other files. Examples:// file1.lox
import "file2"; // Hello from file2
import "file3" as math;fun foo() {
print "Hello from foo of file1";
}
foo(); // Hello from foo of file1
file2.foo(); // Hello from foo of file2print math.pow(2, -math.PI); // 0.0625
// file2.lox
fun foo() {
print "Hello from foo of file2";
}print "Hello from file2";
// file3.lox
fun pow(val, exp) {
if (exp < 0) {
return 1 / pow(val, -exp);
}
var result = 1;
for (var i = 0; i < exp; i = i + 1) {
result = result * val;
}
return result;
}const PI = 3.14;
-
modulo operator. Examples:print 4 % 2; // 0
print 5 % 2; // 1
- ### [*jlox*](https://github.com/Pawel-Parma/lox-java/tree/jlox)
If you are in the jlox branch you are looking at the implementation following the book.