|
| 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 |
0 commit comments