Skip to content

Commit 92f4b95

Browse files
Claude/wokelang ebnf grammar qoe vv (#3)
* 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 --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 62f6b69 commit 92f4b95

11 files changed

Lines changed: 1662 additions & 0 deletions

File tree

examples/math.wasm

253 Bytes
Binary file not shown.

examples/math.woke

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Math functions for WASM compilation
2+
3+
to add(a: Int, b: Int) -> Int {
4+
give back a + b;
5+
}
6+
7+
to multiply(a: Int, b: Int) -> Int {
8+
give back a * b;
9+
}
10+
11+
to factorial(n: Int) -> Int {
12+
when n <= 1 {
13+
give back 1;
14+
}
15+
give back n * factorial(n - 1);
16+
}
17+
18+
to fibonacci(n: Int) -> Int {
19+
when n <= 0 {
20+
give back 0;
21+
}
22+
when n == 1 {
23+
give back 1;
24+
}
25+
give back fibonacci(n - 1) + fibonacci(n - 2);
26+
}
27+
28+
to sum_to_n(n: Int) -> Int {
29+
remember total = 0;
30+
remember i = n;
31+
repeat n times {
32+
total = total + i;
33+
i = i - 1;
34+
}
35+
give back total;
36+
}

include/wokelang.h

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
/**
2+
* WokeLang C API
3+
*
4+
* This header provides the C-compatible interface for WokeLang.
5+
* It can be used from C, C++, Zig, or any language supporting the C ABI.
6+
*
7+
* Example usage:
8+
*
9+
* #include "wokelang.h"
10+
*
11+
* int main() {
12+
* WokeInterpreter* interp = woke_interpreter_new();
13+
* if (!interp) return 1;
14+
*
15+
* const char* code =
16+
* "to greet(name: String) -> String {\n"
17+
* " give back \"Hello, \" + name + \"!\";\n"
18+
* "}\n";
19+
*
20+
* WokeResult result = woke_exec(interp, code);
21+
* if (result != WOKE_OK) {
22+
* printf("Error: %d\n", result);
23+
* }
24+
*
25+
* woke_interpreter_free(interp);
26+
* return 0;
27+
* }
28+
*/
29+
30+
#ifndef WOKELANG_H
31+
#define WOKELANG_H
32+
33+
#include <stdint.h>
34+
35+
#ifdef __cplusplus
36+
extern "C" {
37+
#endif
38+
39+
/* Opaque types */
40+
typedef struct WokeInterpreter WokeInterpreter;
41+
typedef struct WokeValue WokeValue;
42+
43+
/* Result codes */
44+
typedef enum WokeResult {
45+
WOKE_OK = 0,
46+
WOKE_ERROR = 1,
47+
WOKE_PARSE_ERROR = 2,
48+
WOKE_RUNTIME_ERROR = 3,
49+
WOKE_NULL_POINTER = 4
50+
} WokeResult;
51+
52+
/* Value type tags */
53+
typedef enum WokeValueType {
54+
WOKE_TYPE_INT = 0,
55+
WOKE_TYPE_FLOAT = 1,
56+
WOKE_TYPE_STRING = 2,
57+
WOKE_TYPE_BOOL = 3,
58+
WOKE_TYPE_ARRAY = 4,
59+
WOKE_TYPE_UNIT = 5
60+
} WokeValueType;
61+
62+
/* === Interpreter lifecycle === */
63+
64+
/**
65+
* Create a new WokeLang interpreter.
66+
*
67+
* @return Pointer to the interpreter, or NULL on failure.
68+
* The caller is responsible for freeing with woke_interpreter_free.
69+
*/
70+
WokeInterpreter* woke_interpreter_new(void);
71+
72+
/**
73+
* Free a WokeLang interpreter.
74+
*
75+
* @param interp The interpreter to free. May be NULL (no-op).
76+
*/
77+
void woke_interpreter_free(WokeInterpreter* interp);
78+
79+
/**
80+
* Execute WokeLang source code.
81+
*
82+
* @param interp The interpreter instance.
83+
* @param source Null-terminated WokeLang source code.
84+
* @return WOKE_OK on success, error code otherwise.
85+
*/
86+
WokeResult woke_exec(WokeInterpreter* interp, const char* source);
87+
88+
/**
89+
* Evaluate an expression and get the result.
90+
*
91+
* @param interp The interpreter instance.
92+
* @param source Null-terminated expression to evaluate.
93+
* @param out_value Pointer to receive the result value.
94+
* @return WOKE_OK on success, error code otherwise.
95+
*/
96+
WokeResult woke_eval(WokeInterpreter* interp, const char* source, WokeValue** out_value);
97+
98+
/* === Value operations === */
99+
100+
/**
101+
* Free a WokeValue.
102+
*
103+
* @param value The value to free. May be NULL (no-op).
104+
*/
105+
void woke_value_free(WokeValue* value);
106+
107+
/**
108+
* Get the type of a WokeValue.
109+
*
110+
* @param value The value to inspect.
111+
* @return The type tag.
112+
*/
113+
WokeValueType woke_value_type(const WokeValue* value);
114+
115+
/**
116+
* Get an integer from a WokeValue.
117+
*
118+
* @param value The value to read.
119+
* @param out Pointer to receive the integer.
120+
* @return WOKE_OK on success, WOKE_ERROR if type mismatch.
121+
*/
122+
WokeResult woke_value_as_int(const WokeValue* value, int64_t* out);
123+
124+
/**
125+
* Get a float from a WokeValue.
126+
*
127+
* @param value The value to read.
128+
* @param out Pointer to receive the float.
129+
* @return WOKE_OK on success, WOKE_ERROR if type mismatch.
130+
*/
131+
WokeResult woke_value_as_float(const WokeValue* value, double* out);
132+
133+
/**
134+
* Get a boolean from a WokeValue.
135+
*
136+
* @param value The value to read.
137+
* @param out Pointer to receive the boolean (0 or 1).
138+
* @return WOKE_OK on success, WOKE_ERROR if type mismatch.
139+
*/
140+
WokeResult woke_value_as_bool(const WokeValue* value, int* out);
141+
142+
/**
143+
* Get a string from a WokeValue.
144+
*
145+
* @param value The value to read.
146+
* @return Newly allocated string, or NULL on error.
147+
* Caller must free with woke_string_free.
148+
*/
149+
char* woke_value_as_string(const WokeValue* value);
150+
151+
/**
152+
* Free a string returned by woke_value_as_string.
153+
*
154+
* @param s The string to free. May be NULL (no-op).
155+
*/
156+
void woke_string_free(char* s);
157+
158+
/* === Value creation === */
159+
160+
/**
161+
* Create an integer WokeValue.
162+
*
163+
* @param n The integer value.
164+
* @return New value, or NULL on allocation failure.
165+
*/
166+
WokeValue* woke_value_from_int(int64_t n);
167+
168+
/**
169+
* Create a float WokeValue.
170+
*
171+
* @param f The float value.
172+
* @return New value, or NULL on allocation failure.
173+
*/
174+
WokeValue* woke_value_from_float(double f);
175+
176+
/**
177+
* Create a boolean WokeValue.
178+
*
179+
* @param b The boolean value (0 = false, nonzero = true).
180+
* @return New value, or NULL on allocation failure.
181+
*/
182+
WokeValue* woke_value_from_bool(int b);
183+
184+
/**
185+
* Create a string WokeValue.
186+
*
187+
* @param s Null-terminated string to copy.
188+
* @return New value, or NULL on allocation failure.
189+
*/
190+
WokeValue* woke_value_from_string(const char* s);
191+
192+
/* === Utility === */
193+
194+
/**
195+
* Get the WokeLang version string.
196+
*
197+
* @return Static version string (do not free).
198+
*/
199+
const char* woke_version(void);
200+
201+
/**
202+
* Get the last error message.
203+
*
204+
* @return Error message, or NULL if no error.
205+
* Valid until the next woke_* call.
206+
*/
207+
const char* woke_last_error(void);
208+
209+
#ifdef __cplusplus
210+
}
211+
#endif
212+
213+
#endif /* WOKELANG_H */

src/codegen/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mod wasm;
2+
3+
pub use wasm::WasmCompiler;

0 commit comments

Comments
 (0)