1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
use std::{
any::Any,
cell::RefCell,
collections::HashMap,
rc::Rc,
};
#[derive(Clone)]
pub enum ZData {
Raw(Rc<str>),
Dyn(Rc<dyn Any>),
}
#[derive(Clone)]
pub struct ZType {
pub tag: Rc<str>, // dynamic tags remain supported
pub data: ZData,
}
type FnPtr<State> = fn(&mut State, &ZLang<State>, Vec<ZType>) -> Option<ZType>;
pub struct ZLang<State> {
// Globals: variable slots and storage
var_slots: RefCell<HashMap<String, usize>>,
vars: RefCell<Vec<Option<ZType>>>,
// Function registry by name (compile-time resolution)
functions: HashMap<String, FnPtr<State>>,
// Common tag for raw literals
raw_tag: Rc<str>,
}
#[derive(Clone, Debug)]
pub enum Tokens<'a> {
Ident(&'a str),
Raw(&'a str),
Equals,
LParen,
RParen,
Comma,
}
enum Op<State> {
// Frame to collect only present argument values for a call
PushFrame,
// Discard everything pushed since the last frame (used for unknown functions)
DiscardFrame,
// Values and variables
LoadVar(usize),
LoadRaw(usize), // index into literal pool
StoreVar(usize),
// Calls
Call(FnPtr<State>),
// Statements
Pop, // discard top value if present
}
struct Program<State> {
ops: Vec<Op<State>>,
literals: Vec<Rc<str>>,
}
impl<State> ZLang<State> {
pub fn new() -> Self {
Self {
var_slots: RefCell::new(HashMap::new()),
vars: RefCell::new(Vec::new()),
functions: HashMap::new(),
raw_tag: Rc::<str>::from("raw"),
}
}
pub fn register_function(
&mut self,
name: impl Into<String>,
function: FnPtr<State>,
) {
self.functions.insert(name.into(), function);
}
pub fn interpret(&self, state: &mut State, code: &str) {
let tokens = self.tokenize(code);
let prog = self.compile(&tokens);
self.execute(state, &prog);
}
// -----------------------------
// Tokenization (UTF-8 safe, no per-step bounds checks)
// -----------------------------
fn tokenize<'a>(&self, code: &'a str) -> Vec<Tokens<'a>> {
let mut toks = Vec::with_capacity(code.len() / 4);
let mut it = code.char_indices().peekable();
let mut paren_depth = 0usize;
while let Some(&(i, c)) = it.peek() {
match c {
' ' | '\t' | '\r' | '\n' => {
it.next();
}
'=' => {
toks.push(Tokens::Equals);
it.next();
}
'(' => {
paren_depth = paren_depth.saturating_add(1);
toks.push(Tokens::LParen);
it.next();
}
')' => {
if paren_depth > 0 {
paren_depth -= 1;
}
toks.push(Tokens::RParen);
it.next();
}
',' => {
if paren_depth > 0 {
toks.push(Tokens::Comma);
}
it.next();
}
'"' => {
// string literal
let start = i + c.len_utf8();
it.next(); // consume the opening quote
let mut end = start;
loop {
match it.next() {
Some((j, ch)) if ch == '"' => {
end = j;
break;
}
Some((j, ch)) => {
end = j + ch.len_utf8();
}
None => {
end = code.len();
break;
}
}
}
toks.push(Tokens::Raw(&code[start..end]));
}
_ => {
if c == '_' || c.is_ascii_alphanumeric() {
// scan identifier
let start = i;
let mut end = i + c.len_utf8();
it.next(); // consume first char
while let Some(&(j, ch)) = it.peek() {
if ch == '_' || ch.is_ascii_alphanumeric() {
end = j + ch.len_utf8();
it.next();
} else {
break;
}
}
toks.push(Tokens::Ident(&code[start..end]));
} else {
// skip unknown
it.next();
}
}
}
}
toks
}
// -----------------------------
// Compile to bytecode-like ops (no AST kept)
// -----------------------------
fn compile<'a>(&'a self, toks: &'a [Tokens<'a>]) -> Program<State> {
let mut pos = 0usize;
let len = toks.len();
let mut ops: Vec<Op<State>> = Vec::with_capacity(len); // rough
let mut literals: Vec<Rc<str>> = Vec::new();
// Helpers capture
let mut literal_index = |s: &str| -> usize {
literals.push(Rc::<str>::from(s));
literals.len() - 1
};
// Parse a single expression and emit ops; returns true if it was a direct raw literal
fn compile_expr<State>(
lang: &ZLang<State>,
toks: &[Tokens<'_>],
pos: &mut usize,
ops: &mut Vec<Op<State>>,
literal_index: &mut impl FnMut(&str) -> usize,
) -> bool {
let len = toks.len();
if *pos >= len {
return false;
}
match &toks[*pos] {
Tokens::Ident(name) => {
// lookahead for call or var
if *pos + 1 < len && matches!(toks[*pos + 1], Tokens::LParen) {
// function call: ident '(' args ')'
let ident = *name;
*pos += 2; // consume ident, '('
ops.push(Op::PushFrame);
// parse arguments: zero or more expr, separated by commas, until ')'
while *pos < len && !matches!(toks[*pos], Tokens::RParen) {
compile_expr(lang, toks, pos, ops, literal_index);
if *pos < len && matches!(toks[*pos], Tokens::Comma) {
*pos += 1; // consume comma
} else {
// either ')' or end or next token starts another expr
}
}
// expect ')'
if *pos < len && matches!(toks[*pos], Tokens::RParen) {
*pos += 1;
}
if let Some(&fp) = lang.functions.get(ident) {
ops.push(Op::Call(fp));
} else {
eprintln!("Unknown function `{}`", ident);
// Drop any argument results
ops.push(Op::DiscardFrame);
}
false
} else {
// variable ref
let slot = lang.get_or_create_slot(*name);
*pos += 1;
ops.push(Op::LoadVar(slot));
false
}
}
Tokens::Raw(s) => {
let idx = literal_index(s);
*pos += 1;
ops.push(Op::LoadRaw(idx));
true
}
_ => {
// skip token and treat as no-op expression
*pos += 1;
false
}
}
}
while pos < len {
match &toks[pos] {
Tokens::Ident(name) if pos + 1 < len && matches!(toks[pos + 1], Tokens::Equals) => {
// assignment: name '=' expr
let var_name = *name;
let slot = self.get_or_create_slot(var_name);
pos += 2; // consume name and '='
// Disallow direct raw literal assignment (compile-time check)
let starts_with_raw = matches!(toks.get(pos), Some(Tokens::Raw(_)));
let was_raw = compile_expr(self, toks, &mut pos, &mut ops, &mut literal_index);
if starts_with_raw && was_raw {
eprintln!(
"Error: cannot assign raw string directly to variable `{}`",
var_name
);
// discard computed value if any
ops.push(Op::Pop);
} else {
ops.push(Op::StoreVar(slot));
}
}
// expression statement
Tokens::Ident(_) | Tokens::Raw(_) | Tokens::LParen => {
let _ = compile_expr(self, toks, &mut pos, &mut ops, &mut literal_index);
// discard trailing value if present
ops.push(Op::Pop);
}
_ => {
pos += 1;
}
}
}
Program { ops, literals }
}
// -----------------------------
// Execution engine (stack + frames, zero-alloc hot path)
// -----------------------------
fn execute(&self, state: &mut State, prog: &Program<State>) {
let mut stack: Vec<ZType> = Vec::with_capacity(16);
let mut frames: Vec<usize> = Vec::with_capacity(8); // stores stack base indices
let mut arg_buf: Vec<ZType> = Vec::with_capacity(8); // reused argument buffer
for op in &prog.ops {
match op {
Op::PushFrame => {
frames.push(stack.len());
}
Op::DiscardFrame => {
if let Some(base) = frames.pop() {
stack.truncate(base);
}
}
Op::LoadVar(slot) => {
if let Some(val) = self.vars.borrow().get(*slot).and_then(|o| o.clone()) {
stack.push(val);
} // else: absent => no push (keeps Option semantics)
}
Op::LoadRaw(idx) => {
let s = prog.literals[*idx].clone();
stack.push(ZType {
tag: self.raw_tag.clone(),
data: ZData::Raw(s),
});
}
Op::StoreVar(slot) => {
if let Some(val) = stack.pop() {
self.set_slot(*slot, val);
} else {
eprintln!("Error: assignment has no value");
}
}
Op::Call(fp) => {
// collect args since last frame
let base = match frames.pop() {
Some(b) => b,
None => {
// malformed program; be defensive
arg_buf.clear();
if let Some(_v) = stack.pop() {
// drop one value if present
}
continue;
}
};
// drain in order
arg_buf.clear();
// Move values [base..] into arg_buf preserving order
while stack.len() > base {
// popping reverses; collect in temp then reverse or use remove
// For efficiency, swap-remove into a temp then reverse once
// Simpler: drain to a temp vec and extend
break;
}
// Efficient drain preserving order:
let mut tail: Vec<ZType> = stack.drain(base..).collect();
arg_buf.append(&mut tail);
if let Some(ret) = fp(state, self, std::mem::take(&mut arg_buf)) {
stack.push(ret);
}
// else: no push (Option semantics)
}
Op::Pop => {
let _ = stack.pop();
}
}
}
}
// Slot management: map name -> slot, ensure storage exists
fn get_or_create_slot(&self, name: &str) -> usize {
// Fast path: check without mut borrow
if let Some(&slot) = self.var_slots.borrow().get(name) {
return slot;
}
// Create
let mut slots = self.var_slots.borrow_mut();
if let Some(&slot) = slots.get(name) {
return slot;
}
let mut vars = self.vars.borrow_mut();
let slot = vars.len();
vars.push(None);
slots.insert(name.to_owned(), slot);
slot
}
fn set_slot(&self, slot: usize, val: ZType) {
let mut vars = self.vars.borrow_mut();
if slot >= vars.len() {
vars.resize(slot + 1, None);
}
vars[slot] = Some(val);
}
}