Skip to content

Commit 4533c86

Browse files
committed
chore: add lox code examples
1 parent dc85dc8 commit 4533c86

23 files changed

Lines changed: 371 additions & 0 deletions

examples/README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# KLox Sample Programs
2+
3+
This directory contains example programs demonstrating various features of the KLox language.
4+
5+
## Valid Examples
6+
7+
### Basic Programs
8+
- **fibonacci.lox** / **example_fibonacci.lox** - Recursive Fibonacci sequence calculator
9+
- **loops.lox** - For and while loop examples
10+
- **control_flow.lox** - If/else and control flow examples
11+
- **operators.lox** - Arithmetic, comparison, and logical operators
12+
- **functions.lox** - Function declaration and usage
13+
14+
### Advanced Features
15+
- **closures.lox** - Demonstrates lexical scoping and closures
16+
- **anonymous_functions.lox** - Lambda/anonymous function examples
17+
18+
### Object-Oriented Programming
19+
- **classes.lox** - Classes and methods
20+
- **inheritance.lox** - Class inheritance
21+
- **super.lox** - Superclass method calls
22+
- **static_methods.lox** - Static methods on classes
23+
24+
### Native Functions
25+
- **native_lox.lox** - Using built-in clock() function
26+
- **native_input.lox** - Using input() for user input
27+
- **native_scan.lox** - Using scan() for reading input
28+
29+
### Complete Examples
30+
- **example_quiz.lox** - Interactive quiz program
31+
- **example_user_db.lox** - User database example
32+
33+
## Error Examples
34+
35+
The `errors/` directory contains programs that demonstrate the beautiful error reporting system:
36+
37+
### Parse Errors
38+
- **parse_error.lox** - Missing expression after operator
39+
40+
### Runtime Errors
41+
- **runtime_error_division_by_zero.lox** - Division by zero
42+
- **runtime_error_undefined_variable.lox** - Accessing undefined variables
43+
- **type_error.lox** - Type mismatch in operations
44+
45+
## Running Examples
46+
47+
To run a valid example:
48+
```bash
49+
java -jar build/libs/klox.jar samples/fibonacci.lox
50+
```
51+
52+
To see the beautiful error reporting in action:
53+
```bash
54+
java -jar build/libs/klox.jar samples/errors/parse_error.lox
55+
```
56+
57+
To start the interactive REPL:
58+
```bash
59+
java -jar build/libs/klox.jar
60+
```
61+
62+
## Features Demonstrated
63+
64+
- ✓ Variables and scoping
65+
- ✓ Functions (named and anonymous)
66+
- ✓ Classes and inheritance
67+
- ✓ Closures
68+
- ✓ Loops (for, while)
69+
- ✓ Conditionals (if/else)
70+
- ✓ Operators (arithmetic, comparison, logical)
71+
- ✓ Native functions (clock, println, scan, input)
72+
- ✓ Static methods
73+
- ✓ Super keyword for parent class access
74+
- ✓ Beautiful Rust-style error reporting with source context

examples/anonymous_functions.lox

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var numbers = 5;
2+
var double = fn(n) { n * 2 };
3+
4+
println(double(numbers));
5+
6+
var apply = fn(func, value) {
7+
func(value)
8+
};
9+
10+
println(apply(double, 10));

examples/classes.lox

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Person {
2+
init(name, age) {
3+
this.name = name;
4+
this.age = age;
5+
}
6+
7+
introduce() {
8+
println("Hi, I'm " + this.name + " and I'm " + this.age + " years old");
9+
}
10+
11+
birthday() {
12+
this.age = this.age + 1;
13+
println(this.name + " is now " + this.age);
14+
}
15+
}
16+
17+
var alice = Person("Alice", 30);
18+
alice.introduce();
19+
alice.birthday();

examples/closures.lox

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Closure example
2+
// Demonstrates lexical scoping and closures
3+
4+
fun makeCounter() {
5+
var count = 0;
6+
7+
fun increment() {
8+
count = count + 1;
9+
return count;
10+
}
11+
12+
return increment;
13+
}
14+
15+
var counter = makeCounter();
16+
print counter();
17+
print counter();
18+
print counter();

examples/control_flow.lox

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var x = 10;
2+
var y = 20;
3+
4+
if (x < y) {
5+
println("x is less than y");
6+
}
7+
8+
var counter = 0;
9+
while (counter < 5) {
10+
println("Counter: " + counter);
11+
counter = counter + 1;
12+
}

examples/errors/parse_error.lox

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// This file demonstrates parse error reporting
2+
// Missing expression after the + operator
3+
4+
var x = 10
5+
var y = 20 +
6+
print x
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// This file demonstrates runtime error reporting
2+
// Division by zero error
3+
4+
var x = 10
5+
var y = "hello" + x
6+
var z = 5 / 0
7+
print z
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// This file demonstrates runtime error reporting
2+
// Undefined variable access
3+
4+
var x = 10
5+
print x + undefinedVariable

examples/errors/type_error.lox

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// This file demonstrates runtime type error
2+
// Cannot add incompatible types in certain operations
3+
4+
var x = "hello"
5+
var y = 10
6+
var z = x - y
7+
print z

examples/example_fibonacci.lox

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
fun fibonacci(n) {
2+
if (n <= 1) {
3+
n
4+
} else {
5+
fibonacci(n - 1) + fibonacci(n - 2)
6+
}
7+
}
8+
9+
var start = clock();
10+
var result = fibonacci(30);
11+
var elapsed = clock() - start;
12+
13+
println("fibonacci(30) = " + result);
14+
println("Computed in " + elapsed + " seconds");

0 commit comments

Comments
 (0)