Skip to content

Commit 32646a6

Browse files
Claude/wokelang ebnf grammar qoe vv (#21)
* 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 * Implement Phase 3 Worker System with message passing - Add async worker module with thread-based execution - Implement WorkerHandle, WorkerContext, WorkerPool - Add message passing: WorkerMessage enum with Value, Stop, Ping, etc. - Add cancellation tokens for task control - New lexer tokens: send, receive, channel, await, cancel, from - New AST nodes: SendMessage, ReceiveMessage, AwaitWorker, CancelWorker - Update parser for new worker syntax - Update interpreter with worker statement handling - Update WASM codegen and typechecker for new statements - Add worker tests (4 new tests) - Add workers.woke example demonstrating worker system - Update ROADMAP.md with Phase 2 completion status * Implement Phase 3 Security: capability-based security and persistent consent storage - Add CapabilityRegistry with support for FileRead, FileWrite, Execute, Network, Environment, Process, SystemInfo, Crypto, Clipboard, Notify capabilities - Implement wildcard capability matching (e.g., file:read:* grants all file reads) - Add audit logging for all capability requests, grants, denials, and revocations - Create ConsentStore for persistent storage of consent decisions - Support consent durations: Session, Day, Week, Forever, Once - Add security.woke example demonstrating superpower declarations * Implement Phase 4 Standard Library for WokeLang Add comprehensive standard library with the following modules: - std.math: Mathematical functions (abs, sqrt, pow, sin, cos, tan, floor, ceil, round, min, max, random, pi, e) - std.io: File I/O with consent-based access control (readFile, writeFile, appendFile, exists, delete, listDir, createDir, readLine) - std.json: JSON parsing and generation (parse, stringify, get, set) - std.time: Date/time handling (now, timestamp, format, parse, sleep, elapsed) - std.net: HTTP networking with consent (httpGet, httpPost, download) Key features: - StdlibRegistry for function registration and lookup - Capability-based security integration for I/O and network operations - Simple JSON parser/serializer without external dependencies - Basic HTTP client using TCP sockets (no TLS for now) - Elapsed timer with thread-local storage - Add Record variant to Value for JSON objects Includes stdlib_demo.woke example demonstrating all modules. * Implement Phase 5: Bytecode Compiler and VM with Optimizations Add a complete bytecode virtual machine for WokeLang with: Bytecode Infrastructure: - Stack-based OpCode instruction set (arithmetic, control flow, functions) - CompiledFunction with constant pool and local variable slots - CompiledProgram with function registry and entry point detection Bytecode Compiler: - Compiles AST to bytecode instructions - Handles all statement types (VarDecl, Assignment, Conditional, Loop, etc.) - Pattern matching compilation for decide statements - Function call and closure support - Result type (Okay/Oops) compilation Virtual Machine: - Stack-based execution with call frames - Local and global variable support - Full arithmetic and comparison operations - Control flow (jumps, conditionals, loops) - Array and record operations - Result type handling with error propagation - Safety limits (max stack size, max call depth) Optimizer: - Constant folding (evaluates constant expressions at compile time) - Dead code elimination (removes unreachable instructions) - Peephole optimizations (double negation, jump-to-next removal) - Nop removal with jump target patching Performance Benchmarks: - Comparative benchmarks for interpreter vs VM execution - Shows 6-11x speedup with precompiled bytecode * Add EBNF grammar and standard library structure EBNF Grammar (docs/grammar.ebnf): - Complete lexical grammar (literals, identifiers, operators) - Full syntactic grammar for all language constructs - Program structure, functions, statements, expressions - Pattern matching, concurrency, modules, types - Consent blocks, gratitude declarations, pragmas - Emote tags and emotional annotations - Operator precedence documentation Standard Library Structure: lib/common/ - Language-agnostic utilities: - prelude.woke: Core utilities (identity, boolean, numeric, string, array, result) - collections.woke: Higher-order functions (map, filter, reduce, zip, partition) - async.woke: Concurrent patterns (workers, retry, timeout, channels) lib/wokelang/ - WokeLang-specific features: - consent.woke: Consent and permission management with safe operations - emotes.woke: Emotional annotations, sentiment-aware code, code health - gratitude.woke: Attribution management, contributor recognition Includes README documenting library structure and usage patterns. * Restructure library with common (aggregate-library) and WokeLang-specific modules Library Structure: lib/common/ - Implements aggregate-library specifications: - core.woke: 20 core operations shared across all 7 languages - Arithmetic: add, subtract, multiply, divide, modulo - Comparison: less_than, greater_than, equal, not_equal, less_equal, greater_equal - Logical: logical_and, logical_or, logical_not - String: concat, string_length, substring - Collection: map, filter, fold, contains - Conditional: if_then_else - prelude.woke: Extended core utilities - collections.woke: Extended collection operations - async.woke: Concurrent programming patterns - README.md: Documentation linking to aggregate-library lib/wokelang/ - WokeLang-specific features (NOT in aggregate-library): - consent.woke: Permission management, safe I/O, privacy - emotes.woke: Emotional annotations, sentiment-aware code - gratitude.woke: Attribution and acknowledgment utilities - README.md: Documentation of unique WokeLang features Updated main lib/README.md explaining the separation between: - Common: 20 operations shared across WokeLang, Duet, Eclexia, Oblíbený, RT-Lang, Phronesis, Julia the Viper - Specific: Consent, emotes, gratitude unique to WokeLang's human-centered philosophy --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent dc3f1b3 commit 32646a6

3 files changed

Lines changed: 404 additions & 0 deletions

File tree

lib/common/README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# WokeLang Common Library
2+
3+
This directory contains WokeLang's implementation of the **aggregate-library** common operations, plus additional utilities that are language-agnostic.
4+
5+
## Relationship to aggregate-library
6+
7+
The [aggregate-library](https://github.com/hyperpolymath/aggregate-library) defines 20 core operations that work across all seven languages in the ecosystem. WokeLang implements all of these operations.
8+
9+
### Core Operations (from aggregate-library)
10+
11+
| Category | Operations |
12+
|----------|------------|
13+
| Arithmetic (5) | `add`, `subtract`, `multiply`, `divide`, `modulo` |
14+
| Comparison (6) | `less_than`, `greater_than`, `equal`, `not_equal`, `less_equal`, `greater_equal` |
15+
| Logical (3) | `logical_and`, `logical_or`, `logical_not` |
16+
| String (3) | `concat`, `string_length`, `substring` |
17+
| Collection (4) | `map`, `filter`, `fold`, `contains` |
18+
| Conditional (1) | `if_then_else` |
19+
20+
### Extended Utilities (WokeLang common extensions)
21+
22+
Beyond the 20 core operations, this directory includes additional language-agnostic utilities:
23+
24+
- **prelude.woke** - Identity functions, numeric utilities, array helpers, Result utilities
25+
- **collections.woke** - Extended collection operations (zip, partition, groupBy, etc.)
26+
- **async.woke** - Concurrent programming patterns (workers, retry, timeout)
27+
28+
## Usage
29+
30+
```woke
31+
(* Import common library *)
32+
use lib.common.prelude;
33+
use lib.common.collections;
34+
35+
to main() {
36+
(* Use core operations *)
37+
remember sum = add(10, 20);
38+
remember doubled = map([1, 2, 3], to (x: Int) -> Int { give back multiply(x, 2); });
39+
40+
(* Use extended utilities *)
41+
remember evens = filter(doubled, isEven);
42+
43+
give back sum;
44+
}
45+
```
46+
47+
## Files
48+
49+
| File | Description |
50+
|------|-------------|
51+
| `core.woke` | The 20 aggregate-library operations |
52+
| `prelude.woke` | Extended core utilities |
53+
| `collections.woke` | Extended collection operations |
54+
| `async.woke` | Concurrent programming utilities |
55+
56+
## Specification Compliance
57+
58+
All implementations in `core.woke` comply with the aggregate-library specifications and pass the defined test cases.
59+
60+
## See Also
61+
62+
- [aggregate-library specifications](https://github.com/hyperpolymath/aggregate-library)
63+
- [WokeLang-specific library](../wokelang/README.md)

lib/common/core.woke

Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
(* WokeLang Common Library - Core Operations *)
2+
(* Implementation of aggregate-library specifications *)
3+
(* https://github.com/hyperpolymath/aggregate-library *)
4+
5+
#care on;
6+
7+
(* ===================================================================== *)
8+
(* ARITHMETIC OPERATIONS (5) *)
9+
(* Spec: aggregate-library/specs/arithmetic/ *)
10+
(* ===================================================================== *)
11+
12+
(* add: Number, Number -> Number *)
13+
(* Computes the sum of two numbers *)
14+
(* Properties: Commutative, Associative, Identity element = 0 *)
15+
to add(a: Number, b: Number) -> Number {
16+
give back a + b;
17+
}
18+
19+
(* subtract: Number, Number -> Number *)
20+
(* Computes the difference of two numbers *)
21+
(* Properties: Non-commutative, Right identity = 0 *)
22+
to subtract(a: Number, b: Number) -> Number {
23+
give back a - b;
24+
}
25+
26+
(* multiply: Number, Number -> Number *)
27+
(* Computes the product of two numbers *)
28+
(* Properties: Commutative, Associative, Identity element = 1, Zero element = 0 *)
29+
to multiply(a: Number, b: Number) -> Number {
30+
give back a * b;
31+
}
32+
33+
(* divide: Number, Number -> Result[Number, String] *)
34+
(* Computes the quotient of two numbers *)
35+
(* WokeLang uses Result type for safe division *)
36+
@careful(level=3)
37+
to divide(a: Number, b: Number) -> Result[Number, String] {
38+
when b == 0 {
39+
give back Oops("Division by zero is not allowed");
40+
}
41+
give back Okay(a / b);
42+
}
43+
44+
(* modulo: Number, Number -> Result[Number, String] *)
45+
(* Computes the remainder of division *)
46+
(* Properties: Related to division by a = b * (a / b) + (a % b) *)
47+
@careful(level=3)
48+
to modulo(a: Number, b: Number) -> Result[Number, String] {
49+
when b == 0 {
50+
give back Oops("Modulo by zero is not allowed");
51+
}
52+
give back Okay(a % b);
53+
}
54+
55+
(* ===================================================================== *)
56+
(* COMPARISON OPERATIONS (6) *)
57+
(* Spec: aggregate-library/specs/comparison/ *)
58+
(* ===================================================================== *)
59+
60+
(* less_than: A, A -> Bool *)
61+
(* Returns true if a < b *)
62+
(* Properties: Irreflexive, Asymmetric, Transitive *)
63+
to less_than(a: Comparable, b: Comparable) -> Bool {
64+
give back a < b;
65+
}
66+
67+
(* greater_than: A, A -> Bool *)
68+
(* Returns true if a > b *)
69+
(* Properties: Irreflexive, Asymmetric, Transitive *)
70+
to greater_than(a: Comparable, b: Comparable) -> Bool {
71+
give back a > b;
72+
}
73+
74+
(* equal: A, A -> Bool *)
75+
(* Returns true if a == b *)
76+
(* Properties: Reflexive, Symmetric, Transitive *)
77+
to equal(a: Comparable, b: Comparable) -> Bool {
78+
give back a == b;
79+
}
80+
81+
(* not_equal: A, A -> Bool *)
82+
(* Returns true if a != b *)
83+
(* Properties: Negation of equal *)
84+
to not_equal(a: Comparable, b: Comparable) -> Bool {
85+
give back a != b;
86+
}
87+
88+
(* less_equal: A, A -> Bool *)
89+
(* Returns true if a <= b *)
90+
(* Properties: Reflexive, Antisymmetric, Transitive *)
91+
to less_equal(a: Comparable, b: Comparable) -> Bool {
92+
give back a <= b;
93+
}
94+
95+
(* greater_equal: A, A -> Bool *)
96+
(* Returns true if a >= b *)
97+
(* Properties: Reflexive, Antisymmetric, Transitive *)
98+
to greater_equal(a: Comparable, b: Comparable) -> Bool {
99+
give back a >= b;
100+
}
101+
102+
(* ===================================================================== *)
103+
(* LOGICAL OPERATIONS (3) *)
104+
(* Spec: aggregate-library/specs/logical/ *)
105+
(* ===================================================================== *)
106+
107+
(* logical_and: Bool, Bool -> Bool *)
108+
(* Returns true if both arguments are true *)
109+
(* Properties: Commutative, Associative, Identity = true, Zero = false *)
110+
to logical_and(a: Bool, b: Bool) -> Bool {
111+
give back a and b;
112+
}
113+
114+
(* logical_or: Bool, Bool -> Bool *)
115+
(* Returns true if at least one argument is true *)
116+
(* Properties: Commutative, Associative, Identity = false, Zero = true *)
117+
to logical_or(a: Bool, b: Bool) -> Bool {
118+
give back a or b;
119+
}
120+
121+
(* logical_not: Bool -> Bool *)
122+
(* Returns the negation of the argument *)
123+
(* Properties: Involution (not(not(a)) = a) *)
124+
to logical_not(a: Bool) -> Bool {
125+
give back not a;
126+
}
127+
128+
(* ===================================================================== *)
129+
(* STRING OPERATIONS (3) *)
130+
(* Spec: aggregate-library/specs/string/ *)
131+
(* ===================================================================== *)
132+
133+
(* concat: String, String -> String *)
134+
(* Concatenates two strings *)
135+
(* Properties: Associative, Identity element = "" *)
136+
to concat(a: String, b: String) -> String {
137+
give back a + b;
138+
}
139+
140+
(* string_length: String -> Int *)
141+
(* Returns the length of a string *)
142+
(* Properties: Non-negative, length("") = 0 *)
143+
to string_length(s: String) -> Int {
144+
give back len(s);
145+
}
146+
147+
(* substring: String, Int, Int -> Result[String, String] *)
148+
(* Extracts a substring from start index (inclusive) to end index (exclusive) *)
149+
(* WokeLang uses Result type for safe bounds checking *)
150+
@careful(level=2)
151+
to substring(s: String, start: Int, end: Int) -> Result[String, String] {
152+
when start < 0 {
153+
give back Oops("Start index cannot be negative");
154+
}
155+
when end < start {
156+
give back Oops("End index cannot be less than start index");
157+
}
158+
when end > len(s) {
159+
give back Oops("End index exceeds string length");
160+
}
161+
162+
remember result = "";
163+
remember i = start;
164+
repeat end - start times {
165+
result = result + s[i];
166+
i = i + 1;
167+
}
168+
169+
give back Okay(result);
170+
}
171+
172+
(* ===================================================================== *)
173+
(* COLLECTION OPERATIONS (4) *)
174+
(* Spec: aggregate-library/specs/collection/ *)
175+
(* ===================================================================== *)
176+
177+
(* map: Collection[A], Function[A -> B] -> Collection[B] *)
178+
(* Transforms each element of a collection by applying a function *)
179+
(* Properties: Preserves length, Preserves order, Identity: map(c, id) = c *)
180+
to map(collection: [A], fn: (A) -> B) -> [B] {
181+
remember result = [];
182+
repeat len(collection) times {
183+
result = append(result, fn(collection[__i__]));
184+
}
185+
give back result;
186+
}
187+
188+
(* filter: Collection[A], Function[A -> Bool] -> Collection[A] *)
189+
(* Selects elements that satisfy a predicate *)
190+
(* Properties: Preserves order, length(filter(c, p)) <= length(c) *)
191+
to filter(collection: [A], predicate: (A) -> Bool) -> [A] {
192+
remember result = [];
193+
repeat len(collection) times {
194+
when predicate(collection[__i__]) {
195+
result = append(result, collection[__i__]);
196+
}
197+
}
198+
give back result;
199+
}
200+
201+
(* fold: Collection[A], B, Function[B, A -> B] -> B *)
202+
(* Reduces a collection to a single value (left-to-right) *)
203+
(* Also known as: reduce, accumulate, aggregate *)
204+
to fold(collection: [A], initial: B, fn: (B, A) -> B) -> B {
205+
remember accumulator = initial;
206+
repeat len(collection) times {
207+
accumulator = fn(accumulator, collection[__i__]);
208+
}
209+
give back accumulator;
210+
}
211+
212+
(* contains: Collection[A], A -> Bool *)
213+
(* Checks if an element exists in the collection *)
214+
(* Properties: contains([], x) = false for all x *)
215+
to contains(collection: [A], element: A) -> Bool {
216+
repeat len(collection) times {
217+
when collection[__i__] == element {
218+
give back true;
219+
}
220+
}
221+
give back false;
222+
}
223+
224+
(* ===================================================================== *)
225+
(* CONDITIONAL OPERATION (1) *)
226+
(* Spec: aggregate-library/specs/conditional/ *)
227+
(* ===================================================================== *)
228+
229+
(* if_then_else: Bool, A, A -> A *)
230+
(* Conditional branching - returns first value if true, second otherwise *)
231+
(* Note: WokeLang typically uses 'when/otherwise' syntax instead *)
232+
to if_then_else(condition: Bool, then_value: A, else_value: A) -> A {
233+
when condition {
234+
give back then_value;
235+
} otherwise {
236+
give back else_value;
237+
}
238+
}
239+
240+
(* ===================================================================== *)
241+
(* GRATITUDE *)
242+
(* ===================================================================== *)
243+
244+
thanks to {
245+
"aggregate-library" → "Defining common operations across seven languages";
246+
"Seven Language Communities" → "WokeLang, Duet, Eclexia, Oblíbený, RT-Lang, Phronesis, Julia the Viper";
247+
"Functional Programming" → "Foundational patterns for collection operations";
248+
"Mathematical Foundations" → "Properties that ensure correctness";
249+
}

0 commit comments

Comments
 (0)