Skip to content

Commit 83de697

Browse files
Testclaude
andcommitted
feat: add comprehensive example programs
Created examples/ directory with 8 example programs: 1. 01_hello.woke - Simplest program (returns 42) 2. 02_arithmetic.woke - Basic math operations 3. 03_conditionals.woke - Conditional execution 4. 04_loops.woke - Repetition with repeat...times 5. 05_functions.woke - Function definitions and calls 6. 06_variables.woke - Variable declaration and assignment 7. 07_arrays.woke - Array creation and indexing 8. 08_consent.woke - Consent gates (only if okay) Features demonstrated: - Human-centered syntax (to, remember, give back) - Type annotations (Int, Float, String, Bool, Array) - Control flow (when/otherwise, repeat...times) - Functions with parameters and return types - Consent-driven execution - All major language constructs All examples tested with: - Interpreter (tree-walking) - all working - VM (bytecode) - most working, some features pending Added README.md with: - Usage instructions (run, run-vm, typecheck, lint) - Feature descriptions - VM compatibility status - Contributing guidelines Note: WokeLang uses # for pragmas, not comments Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 8e5714a commit 83de697

9 files changed

Lines changed: 218 additions & 0 deletions

examples/01_hello.woke

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
to main() {
2+
give back 42;
3+
}

examples/02_arithmetic.woke

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
to main() {
2+
remember x = 10;
3+
remember y = 5;
4+
5+
remember sum = x + y;
6+
remember diff = x - y;
7+
remember product = x * y;
8+
remember quotient = x / y;
9+
remember remainder = x % y;
10+
11+
give back sum;
12+
}

examples/03_conditionals.woke

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
to main() {
2+
remember x = 42;
3+
4+
when x > 40 {
5+
remember result = 1;
6+
give back result;
7+
} otherwise {
8+
remember result = 0;
9+
give back result;
10+
}
11+
}
12+
13+
to check_sign(n: Int) -> Int {
14+
when n > 0 {
15+
give back 1;
16+
} otherwise {
17+
when n < 0 {
18+
give back -1;
19+
} otherwise {
20+
give back 0;
21+
}
22+
}
23+
}

examples/04_loops.woke

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
to main() {
2+
remember sum = 0;
3+
4+
repeat 10 times {
5+
remember sum = sum + 1;
6+
}
7+
8+
give back sum;
9+
}
10+
11+
to factorial(n: Int) -> Int {
12+
remember result = 1;
13+
remember counter = n;
14+
15+
repeat n times {
16+
remember result = result * counter;
17+
remember counter = counter - 1;
18+
}
19+
20+
give back result;
21+
}

examples/05_functions.woke

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
to add(a: Int, b: Int) -> Int {
2+
give back a + b;
3+
}
4+
5+
to multiply(a: Int, b: Int) -> Int {
6+
give back a * b;
7+
}
8+
9+
to square(n: Int) -> Int {
10+
give back multiply(n, n);
11+
}
12+
13+
to main() {
14+
remember x = 5;
15+
remember y = 3;
16+
17+
remember sum = add(x, y);
18+
remember product = multiply(x, y);
19+
remember squared = square(x);
20+
21+
give back squared;
22+
}

examples/06_variables.woke

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
to main() {
2+
remember x = 10;
3+
remember y = 20;
4+
remember z = 30;
5+
6+
x = x + 1;
7+
y = y * 2;
8+
9+
remember name = "WokeLang";
10+
remember pi = 3.14159;
11+
remember is_awesome = true;
12+
13+
give back x + y;
14+
}

examples/07_arrays.woke

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
to main() {
2+
remember numbers = [1, 2, 3, 4, 5];
3+
remember first = numbers[0];
4+
remember third = numbers[2];
5+
remember mixed = [42, "hello", true, 3.14];
6+
7+
give back first + third;
8+
}
9+
10+
to sum_array(arr: Array) -> Int {
11+
remember total = 0;
12+
remember i = 0;
13+
14+
repeat 5 times {
15+
remember total = total + arr[i];
16+
remember i = i + 1;
17+
}
18+
19+
give back total;
20+
}

examples/08_consent.woke

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
to main() {
2+
only if okay "file.read" {
3+
remember data = "file contents";
4+
give back 1;
5+
}
6+
7+
only if okay "network.connect" {
8+
remember response = "network data";
9+
give back 2;
10+
}
11+
12+
give back 0;
13+
}

examples/README.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# WokeLang Example Programs
2+
3+
This directory contains example programs demonstrating WokeLang's features.
4+
5+
## Running Examples
6+
7+
### Using the Interpreter (Tree-Walking)
8+
```bash
9+
woke run examples/01_hello.woke
10+
```
11+
12+
### Using the VM (Bytecode)
13+
```bash
14+
woke run-vm examples/01_hello.woke
15+
```
16+
17+
### Type Checking
18+
```bash
19+
woke typecheck examples/01_hello.woke
20+
```
21+
22+
### Linting
23+
```bash
24+
woke lint examples/01_hello.woke
25+
```
26+
27+
## Examples
28+
29+
### 01_hello.woke ✓ Interpreter ✓ VM
30+
The simplest possible WokeLang program. Returns the answer to everything.
31+
32+
### 02_arithmetic.woke ✓ Interpreter ✓ VM
33+
Basic arithmetic operations: addition, subtraction, multiplication, division, modulo.
34+
35+
### 03_conditionals.woke ✓ Interpreter ✓ VM
36+
Conditional execution with `when/otherwise` (if/else) statements.
37+
38+
### 04_loops.woke ✓ Interpreter ⚠ VM (partial)
39+
Repetition with `repeat...times` loops. Includes a factorial implementation.
40+
*Note: VM loop implementation in progress*
41+
42+
### 05_functions.woke ✓ Interpreter ⚠ VM (not yet)
43+
Function definitions, parameters, return types, and function calls.
44+
*Note: VM function calls not yet implemented*
45+
46+
### 06_variables.woke ✓ Interpreter ✓ VM
47+
Variable declaration with `remember` and reassignment.
48+
49+
### 07_arrays.woke ✓ Interpreter ⚠ VM (not yet)
50+
Array creation, indexing, and manipulation.
51+
*Note: VM array indexing not yet implemented*
52+
53+
### 08_consent.woke ✓ Interpreter ✓ VM
54+
Consent gates with `only if okay` for permission-protected operations.
55+
56+
## Language Features
57+
58+
### Human-Centered Syntax
59+
- `to function_name()` - Define a function
60+
- `remember x = value` - Declare a variable
61+
- `give back value` - Return from a function
62+
- `when condition { ... } otherwise { ... }` - Conditional
63+
- `repeat n times { ... }` - Loop
64+
- `only if okay "permission" { ... }` - Consent gate
65+
66+
### Types
67+
- `Int` - Integer numbers
68+
- `Float` - Floating-point numbers
69+
- `String` - Text strings
70+
- `Bool` - Boolean values (true/false)
71+
- `Array` - Collections
72+
- `Result<T, E>` - Success/error values (Okay/Oops)
73+
74+
### Unique Features
75+
- **Consent-Driven**: `only if okay` gates for capability-based security
76+
- **Gratitude Blocks**: `thanks to { ... }` for acknowledgments
77+
- **Emote Annotations**: `@emote` for emotional context
78+
- **Units of Measure**: `measured in meters` for dimensional types
79+
80+
## Contributing
81+
82+
Feel free to add more examples! Follow these guidelines:
83+
- Number examples sequentially
84+
- Include comments explaining the code
85+
- Keep examples focused on a single feature
86+
- Test examples before committing
87+
88+
## License
89+
90+
PMPL-1.0-or-later

0 commit comments

Comments
 (0)