forked from dylan-sutton-chavez/edge-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol.rs
More file actions
582 lines (497 loc) · 23 KB
/
control.rs
File metadata and controls
582 lines (497 loc) · 23 KB
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
use crate::s;
use super::Parser;
use super::types::OpCode;
use crate::modules::lexer::{Token, TokenType};
use alloc::{vec, vec::Vec, string::ToString};
impl<'src, I: Iterator<Item = Token>> Parser<'src, I> {
/* if/elif/else compiler; emits JumpIfFalse/Jump and patches branch join targets */
pub(super) fn if_stmt(&mut self) {
self.advance();
self.enter_block();
self.if_body();
self.commit_block();
}
pub(super) fn if_body(&mut self) {
self.expr();
self.chunk.emit(OpCode::JumpIfFalse, 0);
let jf = self.chunk.instructions.len() - 1;
self.eat(TokenType::Colon);
self.compile_block();
match self.peek() {
Some(TokenType::Elif) => {
self.advance();
self.chunk.emit(OpCode::Jump, 0);
let jmp = self.chunk.instructions.len() - 1;
self.mid_block();
self.patch(jf);
self.if_body();
self.patch(jmp);
}
Some(TokenType::Else) => {
self.advance();
self.chunk.emit(OpCode::Jump, 0);
let jmp = self.chunk.instructions.len() - 1;
self.mid_block();
self.patch(jf);
self.eat(TokenType::Colon);
self.compile_block();
self.patch(jmp);
}
_ => {
self.patch(jf);
}
}
}
/* match/case: literals, captures, wildcards, OR, guards, sequences; emits subject-load + pattern + guard + Jump-end. */
pub(super) fn match_stmt(&mut self) {
self.advance();
self.expr();
let ver = self.increment_version(super::SSA_TMP_MATCH);
let subj = self.chunk.push_name(&s!(str super::SSA_TMP_MATCH, int ver));
self.chunk.emit(OpCode::StoreName, subj);
self.eat(TokenType::Colon);
self.eat_if(TokenType::Indent);
let mut end_jumps = Vec::new();
while matches!(self.peek(), Some(TokenType::Case)) {
self.advance();
let mut fail_jumps: Vec<usize> = Vec::new();
self.parse_pattern(subj, &mut fail_jumps);
// Guard fail joins pattern fails; both land at the next case.
if self.eat_if(TokenType::If) {
self.expr();
self.chunk.emit(OpCode::JumpIfFalse, 0);
fail_jumps.push(self.chunk.instructions.len() - 1);
}
self.eat(TokenType::Colon);
self.compile_block();
self.chunk.emit(OpCode::Jump, 0);
end_jumps.push(self.chunk.instructions.len() - 1);
for j in fail_jumps { self.patch(j); }
}
self.eat_if(TokenType::Dedent);
for pos in end_jumps { self.patch(pos); }
}
/* Emits bytecode for one pattern; appends case-fail jumps to `fail_jumps`; reloads subject from subj. */
pub(super) fn parse_pattern(&mut self, subj: u16, fail_jumps: &mut Vec<usize>) {
// OR pattern: each alt gets a success-jump landing past all alts.
let alt_start = self.chunk.instructions.len();
let _ = alt_start; // unused; alts link via `fail_jumps`.
let mut alts: Vec<Vec<usize>> = Vec::new();
let mut succ_jumps: Vec<usize> = Vec::new();
loop {
let mut this_alt_fails: Vec<usize> = Vec::new();
self.parse_simple_pattern(subj, &mut this_alt_fails);
// On match: jump past remaining alts.
self.chunk.emit(OpCode::Jump, 0);
succ_jumps.push(self.chunk.instructions.len() - 1);
// On mismatch: redirect fails to next alt; only last alt propagates to case-fail.
alts.push(this_alt_fails);
if !matches!(self.peek(), Some(TokenType::Vbar)) {
break;
}
// Previous alt's fails land at next alt entry.
let here = self.chunk.instructions.len();
for j in alts.last_mut().unwrap().drain(..) {
let target = here as u16;
self.chunk.instructions[j].operand = target;
}
self.advance(); // consume `|`
}
// Last alt's fails become the case-fail exits.
if let Some(last) = alts.last_mut() {
fail_jumps.append(last);
}
// All success jumps land here, past the OR.
for j in succ_jumps { self.patch(j); }
}
/* Dispatches single pattern alternative by token: wildcard, capture (StoreName), or literal equality. */
fn parse_simple_pattern(&mut self, subj: u16, fail_jumps: &mut Vec<usize>) {
match self.peek() {
// Wildcard: always succeeds, no binding.
Some(TokenType::Underscore) => { self.advance(); }
// Sequence pattern.
Some(TokenType::Lsqb) => { self.parse_sequence_pattern(subj, fail_jumps); }
// Capture: bind subject to name, always succeeds.
Some(TokenType::Name) => {
let t = self.advance();
let name = self.lexeme(&t).to_string();
self.chunk.emit(OpCode::LoadName, subj);
let ver = self.increment_version(&name);
let i = self.push_ssa_name(&name, ver);
self.chunk.emit(OpCode::StoreName, i);
}
// Literal/expr: equality-test against subject; precedence > bitwise-or keeps `1|2|3` as OR pattern.
_ => {
self.chunk.emit(OpCode::LoadName, subj);
self.expr_bp(11);
self.chunk.emit(OpCode::Eq, 0);
self.chunk.emit(OpCode::JumpIfFalse, 0);
fail_jumps.push(self.chunk.instructions.len() - 1);
}
}
}
/* Sequence pattern: length-checks subject, indexes items into fresh slots; star captures middle slice. */
fn parse_sequence_pattern(&mut self, subj: u16, fail_jumps: &mut Vec<usize>) {
self.advance(); // consume `[`
// Two-pass: scan counts items/star; second pass emits bytecode.
let _ = Vec::<bool>::new(); // remove `item_positions` entirely, it's dead
// Pass 1: buffer tokens to count items and locate the star.
let mut buffered: Vec<crate::modules::lexer::Token> = Vec::new();
let mut depth: i32 = 0;
let mut commas = 0;
let mut empty = true;
let mut star_count = 0;
loop {
match self.peek() {
Some(TokenType::Rsqb) if depth == 0 => break,
None => break,
Some(TokenType::Lpar | TokenType::Lsqb | TokenType::Lbrace) => {
depth += 1; empty = false;
buffered.push(self.advance());
}
Some(TokenType::Rpar | TokenType::Rsqb | TokenType::Rbrace) => {
depth -= 1;
buffered.push(self.advance());
}
Some(TokenType::Comma) if depth == 0 => {
commas += 1; empty = false;
buffered.push(self.advance());
}
Some(TokenType::Star) if depth == 0 => {
star_count += 1; empty = false;
buffered.push(self.advance());
}
_ => { empty = false; buffered.push(self.advance()); }
}
}
let item_count = if empty { 0 } else { commas + 1 };
self.eat(TokenType::Rsqb);
if star_count > 1 {
self.error_at(buffered[0].start, buffered.last().unwrap().end, "multiple stars in sequence pattern");
}
// Length check: exact without star, >= (count-1) with star.
let len_min = if star_count > 0 { item_count - 1 } else { item_count };
self.chunk.emit(OpCode::LoadName, subj);
self.chunk.emit(OpCode::CallLen, 0);
let ci = self.chunk.push_const(super::types::Value::Int(len_min as i64));
self.chunk.emit(OpCode::LoadConst, ci);
let cmp = if star_count > 0 { OpCode::GtEq } else { OpCode::Eq };
self.chunk.emit(cmp, 0);
self.chunk.emit(OpCode::JumpIfFalse, 0);
fail_jumps.push(self.chunk.instructions.len() - 1);
// Pass 2: walk buffered tokens, emitting per-item bytecode.
let saved: Vec<crate::modules::lexer::Token> = buffered;
let mut idx = 0usize;
let total = saved.len();
// Fresh slot to use as the per-item sub-subject.
let item_ver = self.increment_version(super::SSA_TMP_MATCH_ITEM);
let item_subj = self.chunk.push_name(&s!(str super::SSA_TMP_MATCH_ITEM, int item_ver));
// Walk items; split on top-level commas.
let mut item_idx: i64 = 0;
let mut star_idx_seen: Option<i64> = None;
while idx < total {
// Check for star prefix.
let is_star = matches!(saved.get(idx), Some(t) if t.kind == TokenType::Star);
if is_star { idx += 1; }
// Find item end: next depth-0 comma or EOF.
let item_start = idx;
let mut d: i32 = 0;
while idx < total {
let k = saved[idx].kind;
if d == 0 && k == TokenType::Comma { break; }
if matches!(k, TokenType::Lpar | TokenType::Lsqb | TokenType::Lbrace) { d += 1; }
if matches!(k, TokenType::Rpar | TokenType::Rsqb | TokenType::Rbrace) { d -= 1; }
idx += 1;
}
let item_end = idx;
// Consume trailing comma.
if idx < total && saved[idx].kind == TokenType::Comma { idx += 1; }
// Index: positive before star, star gets slice, negative after.
if is_star {
star_idx_seen = Some(item_idx);
// Slice: subj[item_idx : len-suffix]
let suffix = (item_count as i64) - item_idx - 1;
self.chunk.emit(OpCode::LoadName, subj);
let cs = self.chunk.push_const(super::types::Value::Int(item_idx));
self.chunk.emit(OpCode::LoadConst, cs);
self.chunk.emit(OpCode::LoadName, subj);
self.chunk.emit(OpCode::CallLen, 0);
let cend = self.chunk.push_const(super::types::Value::Int(suffix));
self.chunk.emit(OpCode::LoadConst, cend);
self.chunk.emit(OpCode::Sub, 0);
self.chunk.emit(OpCode::LoadNone, 0);
self.chunk.emit(OpCode::BuildSlice, 3);
self.chunk.emit(OpCode::GetItem, 0);
self.chunk.emit(OpCode::CallList, 0);
self.chunk.emit(OpCode::StoreName, item_subj);
} else {
// Negative index for items after the star.
let physical_idx: i64 = if star_idx_seen.is_some() {
-((item_count as i64) - item_idx)
} else {
item_idx
};
self.chunk.emit(OpCode::LoadName, subj);
let cidx = self.chunk.push_const(super::types::Value::Int(physical_idx));
self.chunk.emit(OpCode::LoadConst, cidx);
self.chunk.emit(OpCode::GetItem, 0);
self.chunk.emit(OpCode::StoreName, item_subj);
}
// Dispatch: wildcard, capture, or literal. No nested sequences; use if/match instead.
let toks = &saved[item_start..item_end];
if toks.is_empty() || (toks.len() == 1 && toks[0].kind == TokenType::Underscore) {
// Wildcard: discard stored `item_subj`.
} else if toks.len() == 1 && toks[0].kind == TokenType::Name {
let name = self.source[toks[0].start..toks[0].end].to_string();
if name != "_" {
self.chunk.emit(OpCode::LoadName, item_subj);
let ver = self.increment_version(&name);
let ni = self.push_ssa_name(&name, ver);
self.chunk.emit(OpCode::StoreName, ni);
}
} else {
// Literal: replay tokens; supports Int/Float/Str/Bool/None/negation; else error.
let mut pos = 0;
let mut neg = false;
if pos < toks.len() && toks[pos].kind == TokenType::Minus { neg = true; pos += 1; }
if pos < toks.len() {
let t = &toks[pos];
let raw = &self.source[t.start..t.end];
let val = match t.kind {
TokenType::Int => {
let cleaned = raw.replace('_', "");
// i64 first; fall back to i128 when literal is wider.
cleaned.parse::<i64>().ok().map(super::types::Value::Int)
.or_else(|| cleaned.parse::<i128>().ok().map(super::types::Value::LongInt))
}
TokenType::Float => raw.replace('_', "").parse::<f64>().ok().map(super::types::Value::Float),
TokenType::String => Some(super::types::Value::Str(super::types::parse_string(raw))),
TokenType::True => Some(super::types::Value::Bool(true)),
TokenType::False => Some(super::types::Value::Bool(false)),
TokenType::None => Some(super::types::Value::None),
_ => None,
};
if let Some(mut v) = val {
if neg {
v = match v {
super::types::Value::Int(i) => super::types::Value::Int(-i),
// i128::MIN.neg() overflows; trap as parse error to keep sub-pattern semantics aligned with literal materialisation.
super::types::Value::LongInt(i) => match i.checked_neg() {
Some(n) => super::types::Value::LongInt(n),
None => super::types::Value::LongInt(i),
},
super::types::Value::Float(f) => super::types::Value::Float(-f),
other => other,
};
}
self.chunk.emit(OpCode::LoadName, item_subj);
let ci = self.chunk.push_const(v);
self.chunk.emit(OpCode::LoadConst, ci);
self.chunk.emit(OpCode::Eq, 0);
self.chunk.emit(OpCode::JumpIfFalse, 0);
fail_jumps.push(self.chunk.instructions.len() - 1);
} else {
self.error_at(toks[0].start, toks.last().unwrap().end, "unsupported sub-pattern in sequence (use literals, names, or _)");
}
}
}
item_idx += 1;
}
}
/* while: cond + body + back-edge; optional else when cond falsifies. */
pub(super) fn while_stmt(&mut self) {
self.advance();
self.enter_block();
let loop_start = self.chunk.instructions.len() as u16;
self.loop_starts.push(loop_start);
self.loop_breaks.push(vec![]);
self.loop_kinds.push(false);
self.expr();
self.chunk.emit(OpCode::JumpIfFalse, 0);
let jf = self.chunk.instructions.len() - 1;
self.eat(TokenType::Colon);
self.compile_block();
self.chunk.emit(OpCode::Jump, loop_start);
self.patch(jf);
if self.eat_if(TokenType::Else) {
self.eat(TokenType::Colon);
self.compile_block();
}
self.loop_starts.pop();
self.loop_kinds.pop();
for pos in self.loop_breaks.pop().unwrap_or_default() {
self.patch(pos);
}
self.commit_block();
}
/* for / async for, with optional tuple/star unpacking. */
pub(super) fn for_stmt_inner(&mut self, is_async: bool) {
self.advance();
let parens = self.eat_if(TokenType::Lpar);
let mut vars = Vec::new();
let mut star_pos: Option<usize> = None;
loop {
if self.eat_if(TokenType::Star) { star_pos = Some(vars.len()); }
vars.push(self.advance_text());
if !self.eat_if(TokenType::Comma) { break; }
if matches!(self.peek(), Some(TokenType::In | TokenType::Rpar)) { break; }
}
if parens {
self.eat(TokenType::Rpar);
}
self.eat(TokenType::In);
self.expr();
self.chunk.emit(OpCode::GetIter, is_async as u16);
self.enter_block();
let loop_start = self.chunk.instructions.len() as u16;
self.loop_starts.push(loop_start);
self.loop_breaks.push(vec![]);
self.loop_kinds.push(true);
self.chunk.emit(OpCode::ForIter, 0);
let fi = self.chunk.instructions.len() - 1;
if vars.len() == 1 && star_pos.is_none() {
self.store_name(vars[0].clone());
} else {
if let Some(sp) = star_pos {
let before = sp as u16;
let after = (vars.len() - sp - 1) as u16;
self.chunk.emit(OpCode::UnpackEx, (before << 8) | after);
} else {
self.chunk.emit(OpCode::UnpackSequence, vars.len() as u16);
}
for var in &vars { self.store_name(var.clone()); }
}
self.eat(TokenType::Colon);
self.compile_block();
self.chunk.emit(OpCode::Jump, loop_start);
self.patch(fi);
if !is_async && self.eat_if(TokenType::Else) {
self.eat(TokenType::Colon);
self.compile_block();
}
self.loop_starts.pop();
self.loop_kinds.pop();
for pos in self.loop_breaks.pop().unwrap_or_default() {
self.patch(pos);
}
self.commit_block();
}
/* try / except / else / finally with exception arm chaining. */
pub(super) fn try_stmt(&mut self) {
self.advance();
self.eat(TokenType::Colon);
self.chunk.emit(OpCode::SetupExcept, 0);
let setup = self.chunk.instructions.len() - 1;
self.enter_block();
self.compile_block();
self.chunk.emit(OpCode::PopExcept, 0);
self.chunk.emit(OpCode::Jump, 0);
let success_jump = self.chunk.instructions.len() - 1;
self.mid_block();
self.patch(setup);
let mut end_jumps: Vec<usize> = Vec::new();
let mut next_arm_jump: Option<usize> = None;
let mut had_bare = false;
while self.eat_if(TokenType::Except) {
if let Some(j) = next_arm_jump.take() { self.patch(j); }
if had_bare {
self.error("default 'except:' must be last");
break;
}
if matches!(self.peek(), Some(TokenType::Colon)) {
had_bare = true;
self.chunk.emit(OpCode::PopTop, 0);
} else {
self.chunk.emit(OpCode::Dup, 0);
self.expr();
let isinst_pos = self.last_end as u32;
self.chunk.emit(OpCode::CallIsInstance, 0);
self.chunk.record_call_pos(isinst_pos);
self.chunk.emit(OpCode::JumpIfFalse, 0);
next_arm_jump = Some(self.chunk.instructions.len() - 1);
if self.eat_if(TokenType::As) {
let n = self.advance_text();
self.store_name(n);
} else { self.chunk.emit(OpCode::PopTop, 0); }
}
self.eat(TokenType::Colon);
self.compile_block();
let more = matches!(
self.peek(),
Some(TokenType::Except | TokenType::Else | TokenType::Finally)
);
if !had_bare || more {
self.chunk.emit(OpCode::Jump, 0);
end_jumps.push(self.chunk.instructions.len() - 1);
}
}
if let Some(j) = next_arm_jump {
self.patch(j);
self.chunk.emit(OpCode::Raise, 0);
}
self.patch(success_jump);
for j in end_jumps {
self.patch(j);
}
if self.eat_if(TokenType::Else) {
self.eat(TokenType::Colon);
self.compile_block();
}
if self.eat_if(TokenType::Finally) {
self.eat(TokenType::Colon);
self.compile_block();
}
self.commit_block();
}
/* with / async with: each CM gets its own implicit `SetupExcept` so the per-CM cleanup pad can run `__exit__(exc_type, exc, None)` and honour the suppression contract. Normal exit pops the except frame before running `__exit__(None, None, None)`. */
pub(super) fn with_stmt_inner(&mut self, is_async: bool) {
self.advance();
let operand = is_async as u16;
let mut setup_except_idxs: Vec<usize> = Vec::new();
loop {
self.expr();
self.chunk.emit(OpCode::SetupWith, operand);
// Implicit `SetupExcept` per CM; handler IP patched once the cleanup pad is emitted.
setup_except_idxs.push(self.chunk.instructions.len());
self.chunk.emit(OpCode::SetupExcept, 0);
if self.eat_if(TokenType::As) {
let name = self.advance_text();
self.store_name(name);
}
if !self.eat_if(TokenType::Comma) { break; }
}
self.eat(TokenType::Colon);
self.compile_block();
// Normal exit: innermost first. PopExcept BEFORE ExitWith so a raising `__exit__(None,...)` propagates to the outer CM's cleanup.
let n = setup_except_idxs.len();
let normal_exit_start = self.chunk.instructions.len();
for _ in 0..n {
self.chunk.emit(OpCode::PopExcept, 0);
self.chunk.emit(OpCode::ExitWith, operand);
}
let skip_cleanup_jump = self.chunk.instructions.len();
self.chunk.emit(OpCode::Jump, 0);
// Cleanup pads: per-CM in source order (outermost first). Each runs `WithCleanup` then jumps into the normal-exit sequence at the point right after its own slot, so outer CMs get their `__exit__(None, None, None)` on a suppression path.
let mut cleanup_pad_positions: Vec<usize> = Vec::with_capacity(n);
for i in 0..n {
cleanup_pad_positions.push(self.chunk.instructions.len());
self.chunk.emit(OpCode::WithCleanup, 0);
// `normal_exit_start + 2*(n-i)` lands past the PopExcept+ExitWith pairs for CMs i..n-1 (innermost). i == 0 lands at the `Jump @end` which falls through to `end`.
let target = (normal_exit_start + 2 * (n - i)) as u16;
self.chunk.emit(OpCode::Jump, target);
}
let end_label = self.chunk.instructions.len();
// Patch SetupExcept handler IPs and the skip-cleanup jump.
for (i, &se_idx) in setup_except_idxs.iter().enumerate() {
self.chunk.instructions[se_idx].operand = cleanup_pad_positions[i] as u16;
}
self.chunk.instructions[skip_cleanup_jump].operand = end_label as u16;
}
/* Delegates to imports.rs; compile-time only, no import opcodes reach the VM. */
pub(super) fn import_stmt(&mut self) {
self.do_import_stmt();
}
pub(super) fn parse_from_stmt(&mut self) {
self.do_from_stmt();
}
}