Skip to content

Commit cbb1139

Browse files
committed
chore: add more samples for new error handling and also update readme file
1 parent af50ce5 commit cbb1139

10 files changed

Lines changed: 176 additions & 10 deletions

README.md

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,41 @@ Or use the REPL for interactive mode.
8282

8383
## Project Structure
8484

85-
- `Interpreter.kt`: Core execution engine with expression and statement visitors
86-
- `Expr.kt`: AST node definitions for expressions
87-
- `Stmt.kt`: AST node definitions for statements
88-
- `Scanner.kt`: Lexical analysis
89-
- `Parser.kt`: Syntax analysis and AST building
90-
- `Resolver.kt`: Static scope analysis
91-
- `Environment.kt`: Scope and variable storage
92-
- `LoxCallable.kt`: Interface for callable objects
93-
- `LoxClass.kt`, `LoxInstance.kt`: Object-oriented features
94-
- `Token.kt`: Token representation
85+
```
86+
src/main/kotlin/com/aymanetech/
87+
├── Lox.kt # Main entry point and REPL
88+
89+
├── lexer/ # Lexical analysis
90+
│ ├── Scanner.kt
91+
│ ├── Token.kt
92+
│ └── TokenType.kt
93+
94+
├── parser/ # Syntax analysis
95+
│ └── Parser.kt
96+
97+
├── ast/ # Abstract Syntax Tree
98+
│ ├── Expr.kt
99+
│ └── Stmt.kt
100+
101+
├── resolver/ # Semantic analysis
102+
│ └── Resolver.kt
103+
104+
├── interpreter/ # Runtime execution
105+
│ ├── Interpreter.kt
106+
│ ├── Environment.kt
107+
│ └── NativeFunctions.kt
108+
109+
└── runtime/ # Runtime types and errors
110+
├── LoxCallable.kt
111+
├── LoxClass.kt
112+
├── LoxFunction.kt
113+
├── LoxInstance.kt
114+
└── errors/
115+
├── ErrorHandler.kt
116+
├── DiagnosticReporter.kt
117+
├── RuntimeError.kt
118+
└── RuntimeReturn.kt
119+
```
95120

96121
## References
97122

samples/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

samples/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();

samples/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

samples/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

samples/fibonacci.lox

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Fibonacci sequence calculator
2+
// Demonstrates recursion and functions
3+
4+
fun fibonacci(n) {
5+
if (n <= 1) return n;
6+
return fibonacci(n - 1) + fibonacci(n - 2);
7+
}
8+
9+
print "Fibonacci of 10:";
10+
print fibonacci(10);

samples/klox

1.7 MB
Binary file not shown.

samples/loops.lox

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Loop examples
2+
// Demonstrates for and while loops
3+
4+
print "Counting with for loop:";
5+
for (var i = 1; i <= 5; i = i + 1) {
6+
print i;
7+
}
8+
9+
print "Counting with while loop:";
10+
var j = 1;
11+
while (j <= 5) {
12+
print j;
13+
j = j + 1;
14+
}

0 commit comments

Comments
 (0)