Skip to content

Commit f1e00bb

Browse files
Claude/wokelang ebnf grammar qoe vv (#6)
* Implement WokeLang interpreter in Rust Complete implementation of WokeLang with: Lexer (src/lexer/): - Token definitions for all keywords, operators, literals - Uses logos for fast tokenization - Handles comments, strings with escapes, numbers Parser (src/parser/): - Recursive descent parser - Full expression parsing with correct precedence - All statement types: remember, when/otherwise, repeat, attempt safely - Pattern matching with decide based on - Emote tags, consent blocks, gratitude declarations AST (src/ast/): - Complete type definitions for all language constructs - Spanned nodes for error reporting Interpreter (src/interpreter/): - Tree-walking interpreter - Scoped environments for variables - Built-in functions: print, len, toString, toInt - Consent system with interactive prompts - Pattern matching evaluation - All operators (arithmetic, comparison, logical) CLI (src/main.rs): - woke <file.woke> - run program - woke --tokenize <file> - show tokens - woke --parse <file> - show AST Examples: - examples/hello.woke - feature showcase - examples/demo.woke - runnable demo * Add REPL, WASM compilation, and Zig FFI REPL (src/repl.rs): - Interactive command-line interface with rustyline - Commands: :help, :quit, :clear, :reset, :load, :ast - Expression evaluation with automatic result printing - Start with `woke` or `woke --repl` WASM Compilation (src/codegen/): - Compile WokeLang to WebAssembly binary format - Uses wasm-encoder for proper WASM generation - Supports functions, expressions, loops, conditionals - Pattern matching compilation - CLI: `woke -c input.woke` outputs input.wasm Zig FFI (src/ffi/, zig/, include/): - C-compatible API for embedding WokeLang - Interpreter lifecycle: woke_interpreter_new/free - Code execution: woke_exec, woke_eval - Value operations: type checking, conversion, creation - Static library (libwokelang.a) and shared library (.so) - C header (include/wokelang.h) - Zig bindings (zig/wokelang.zig) with idiomatic wrapper - Example Zig program and build.zig Other: - examples/math.woke - math functions for WASM demo - Cargo.toml updated for cdylib/staticlib targets * Add comprehensive documentation and wiki Includes: - Project roadmap (ROADMAP.md) with version timeline through v1.0 - Wiki home page with table of contents - Getting Started guides: Installation, Hello World, Basic Syntax, REPL - Language Guide: Functions, Control Flow, Error Handling, Variables/Types - Core Concepts: Consent System, Gratitude, Emote Tags - Reference: CLI, Built-in Functions, Keywords, Operators, Language Spec - Internals: Architecture, Lexer, Parser, Interpreter, WASM, FFI - Tutorial: Building a CLI app * Implement Phase 2 Language Completeness features - Add Result types (Okay/Oops) with pattern matching support - Implement error propagation operator (?) for Result types - Add pattern matching destructuring with guard clauses - Implement module system with use/share keywords - Add new builtins: isOkay, isOops, getOkay, getOops - Update WASM compiler and FFI for new AST variants - Add example programs for Result types and modules * Add static type inference with Hindley-Milner style checker - Implement TypeChecker module with type inference - Support for basic types: Int, Float, String, Bool, Unit - Support for compound types: Array, Result, Maybe, Function - Implement unification algorithm for type constraints - Add built-in function type handling - Include comprehensive test suite for type checking --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent c353c20 commit f1e00bb

7 files changed

Lines changed: 896 additions & 2 deletions

File tree

examples/module_test.woke

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Test program for WokeLang module system
2+
// Demonstrates use/share keywords
3+
4+
thanks to {
5+
"WokeLang" → "For module support";
6+
}
7+
8+
// Import modules
9+
use modules.math;
10+
use modules.greetings renamed greet;
11+
12+
to main() {
13+
hello "Testing Module System";
14+
15+
// Test math module (imported with default prefix)
16+
print("=== Math Module ===");
17+
remember sum = math_add(5, 3);
18+
print("5 + 3 = " + toString(sum));
19+
20+
remember diff = math_subtract(10, 4);
21+
print("10 - 4 = " + toString(diff));
22+
23+
remember product = math_multiply(6, 7);
24+
print("6 * 7 = " + toString(product));
25+
26+
remember sq = math_square(9);
27+
print("9^2 = " + toString(sq));
28+
29+
// Test greetings module (imported with rename)
30+
print("");
31+
print("=== Greetings Module ===");
32+
greet_sayHello("World");
33+
greet_sayGoodbye("Friend");
34+
greet_greet("Dr. Smith", true);
35+
greet_greet("Bob", false);
36+
37+
goodbye "Module system working!";
38+
}

examples/modules/greetings.woke

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Greetings module - provides greeting utilities
2+
3+
share sayHello;
4+
share sayGoodbye;
5+
share greet;
6+
7+
to sayHello(name: String) {
8+
print("Hello, " + name + "!");
9+
}
10+
11+
to sayGoodbye(name: String) {
12+
print("Goodbye, " + name + "!");
13+
}
14+
15+
to greet(name: String, formal: Bool) {
16+
when formal {
17+
print("Good day, " + name + ".");
18+
} otherwise {
19+
print("Hey, " + name + "!");
20+
}
21+
}

examples/modules/math.woke

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Math module - provides basic math utilities
2+
// Example of WokeLang module system
3+
4+
share add;
5+
share subtract;
6+
share multiply;
7+
share square;
8+
9+
to add(a: Int, b: Int) -> Int {
10+
give back a + b;
11+
}
12+
13+
to subtract(a: Int, b: Int) -> Int {
14+
give back a - b;
15+
}
16+
17+
to multiply(a: Int, b: Int) -> Int {
18+
give back a * b;
19+
}
20+
21+
to square(x: Int) -> Int {
22+
give back x * x;
23+
}
24+
25+
// This function is NOT exported - private to the module
26+
to internalHelper() {
27+
print("This is private");
28+
}

examples/result_types.woke

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Example: Result Types in WokeLang
2+
// Demonstrates Okay(value) and Oops(error) result types
3+
4+
thanks to {
5+
"Rust" → "For inspiration on Result types";
6+
}
7+
8+
// Function that may fail - returns Result type
9+
to safeDivide(a: Int, b: Int) {
10+
when b == 0 {
11+
give back Oops("Division by zero");
12+
}
13+
give back Okay(a / b);
14+
}
15+
16+
// Function using the ? operator for error propagation
17+
to calculate(x: Int, y: Int) {
18+
remember result = safeDivide(x, y)?;
19+
give back Okay(result * 2);
20+
}
21+
22+
to main() {
23+
hello "Testing Result Types";
24+
25+
// Test 1: Successful division
26+
remember r1 = safeDivide(10, 2);
27+
print("10 / 2 = ");
28+
print(r1);
29+
print("Is Okay? " + toString(isOkay(r1)));
30+
31+
// Test 2: Division by zero
32+
remember r2 = safeDivide(10, 0);
33+
print("10 / 0 = ");
34+
print(r2);
35+
print("Is Oops? " + toString(isOops(r2)));
36+
37+
// Test 3: Pattern matching on Result
38+
decide based on r1 {
39+
Okay(value) → {
40+
print("Success with value: " + toString(value));
41+
}
42+
Oops(error) → {
43+
print("Error: " + error);
44+
}
45+
}
46+
47+
// Test 4: Using getOkay/getOops
48+
when isOkay(r1) {
49+
print("Extracted value: " + toString(getOkay(r1)));
50+
}
51+
52+
when isOops(r2) {
53+
print("Extracted error: " + getOops(r2));
54+
}
55+
56+
goodbye "Result types working!";
57+
}

src/codegen/wasm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ impl WasmCompiler {
323323
break;
324324
}
325325
}
326-
}
326+
}next stage
327327

328328
// Close all if blocks
329329
for arm in &decide.arms {

src/parser/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ impl<'src> Parser<'src> {
234234
// === Worker/Side Quest/Superpower ===
235235

236236
fn parse_worker_def(&mut self) -> Result<WorkerDef, ParseError> {
237-
let start = self.current_span().start;
237+
let start = self.current_next stagespan().start;
238238
self.expect(Token::Worker)?;
239239
let name = self.expect_identifier()?;
240240
self.expect(Token::LBrace)?;

0 commit comments

Comments
 (0)