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
//! `for`, `switch`, `catch` and `error`.
//!
//! Every command here obeys the compiler's invariant that a command leaves
//! exactly one value on the stack, and each branch of one is compiled at the
//! same entry depth, so `break` and `continue` can still discard a statically
//! known number of values before jumping.
//!
//! `catch` is the exception to "no runtime unwinder", and it is deliberately a
//! small one. `Op::ExtendedWide(ext_wide::CATCH, handler_ip)` records the
//! runtime stack and frame depths together with the op index of a handler
//! block that the compiler emits ahead of the guarded script and jumps over.
//! When a chunk stops with an error, the driver in [`crate::runtime`] restores
//! those depths, pushes the message, and resumes the VM at the handler. The
//! handler and the ordinary path meet at the same compile-time depth, so no
//! part of the surrounding code has to know a `catch` is there.
use fusevm::Op;
use crate::compiler::{ext, ext_wide, CompileError, Compiler};
use crate::list;
use crate::parser::Word;
/// How `switch` compares its subject to a pattern.
#[derive(Clone, Copy, PartialEq, Eq)]
/// How a `switch` clause matches, as the low bit of [`ext::MATCH`]'s operand.
/// The high bit carries `-nocase`, so the four combinations ride in one byte
/// and an emitter that knows nothing of case folding still means what it did.
enum Match {
Exact,
Glob,
/// `-regexp`, matched by [`crate::regexp`]. Value 2, so it does not collide
/// with the `-nocase` bit the operand carries at bit 1.
Regexp = 4,
}
/// One `pattern body` clause, with `-` fall-through already resolved.
struct Clause {
/// The pattern's literal text, or `None` when it is a word to evaluate.
text: Option<String>,
word: Option<Word>,
body: String,
}
impl Compiler {
/// `for start test next body`.
pub(crate) fn cmd_for(&mut self, args: &[Word]) -> Result<(), CompileError> {
let [start, test, next, body] = args else {
return self.error("wrong # args: should be \"for start test next body\"");
};
let start = self.body_of(start)?;
let body = self.body_of(body)?;
let next = self.body_of(next)?;
self.emit_body(&start)?;
// `continue` skips the rest of the body and runs the step; `break` in
// the step terminates the loop, as `for(n)` specifies. Both fall out of
// the rotated shape, where the step precedes the next test.
self.rotated_loop(
|c| c.emit_body(&body),
|c| c.emit_body(&next),
|c| c.expr_word(test),
)?;
self.push_empty();
Ok(())
}
/// `switch ?options? string pattern body ?pattern body ...?` and the form
/// that groups the patterns and bodies into one braced list.
pub(crate) fn cmd_switch(&mut self, args: &[Word]) -> Result<(), CompileError> {
let mut i = 0;
let mut mode = Match::Exact;
let mut nocase = false;
// `switch(n)`: a leading `-` argument is an option only while at least
// two arguments follow it — the subject and the patterns. That bound is
// the interpreter's own (`Tcl_SwitchObjCmd` scans `i < objc-2`), and it
// is why `switch -exact -9223372036854775807 {a* {…} default {…}}`
// matches on the number rather than reporting a bad option: after
// `-exact` only two arguments remain, so the number is the subject.
while i + 2 < args.len() {
let Some(text) = args.get(i).and_then(|w| w.as_literal()) else {
break;
};
if !text.starts_with('-') {
break;
}
i += 1;
match text {
"-exact" => mode = Match::Exact,
"-glob" => mode = Match::Glob,
"-nocase" => nocase = true,
// Named so the option list above stays honest, and refused with
// this frontend's own wording rather than being mistaken for a
// bad option. `-regexp` needs the regular-expression engine.
"-regexp" => mode = Match::Regexp,
// Still refused: both need the match results handed back
// through a variable, which this lowering has nowhere to put.
"-matchvar" | "-indexvar" => {
return self.error(format!(
"the {text} option of \"switch\" is not supported yet"
))
}
"--" => break,
other => {
return self.error(format!(
"bad option \"{other}\": must be -exact, -glob, -indexvar, \
-matchvar, -nocase, -regexp, or --"
))
}
}
}
let Some(subject) = args.get(i) else {
return self
.error("wrong # args: should be \"switch ?options? string {pattern body ...}\"");
};
let clauses = self.switch_clauses(&args[i + 1..])?;
self.word(subject)?;
let entry = self.depth;
let mut ends = Vec::new();
let mut defaulted = false;
for (k, clause) in clauses.iter().enumerate() {
self.depth = entry;
let last = k + 1 == clauses.len();
if last && clause.text.as_deref() == Some("default") {
// `default` matches anything, but only as the final pattern.
defaulted = true;
self.emit(Op::Pop, -1);
self.switch_body(&clause.body)?;
break;
}
self.emit(Op::Dup, 1);
match (&clause.text, &clause.word) {
(Some(text), _) => self.push_text(text),
(None, Some(w)) => self.word(w)?,
(None, None) => unreachable!("a clause has a pattern"),
}
// The string module's matcher, so `-nocase` folds exactly as
// `string match -nocase` folds; it answers "1"/"0", which the
// boolean op turns into the 1/0 the branch below tests.
self.emit(
Op::LoadInt(i64::from(mode as u8 | u8::from(nocase) << 1)),
1,
);
self.emit(Op::Extended(crate::cmd_string::ext::SWITCH_MATCH, 3), -2);
self.emit(Op::Extended(ext::BOOL, 0), 0);
let miss = self.emit(Op::JumpIfFalse(usize::MAX), -1);
self.emit(Op::Pop, -1);
self.switch_body(&clause.body)?;
ends.push(self.emit(Op::Jump(usize::MAX), 0));
let next = self.b.current_pos();
self.b.patch_jump(miss, next);
}
self.depth = entry;
if !defaulted {
// Nothing matched: the subject is discarded and `switch` is empty.
self.emit(Op::Pop, -1);
self.push_empty();
}
let end = self.b.current_pos();
for j in ends {
self.b.patch_jump(j, end);
}
Ok(())
}
/// Flatten a `switch` tail into clauses, resolving the `-` body that means
/// "share the next pattern's body" by repeating that body per pattern.
fn switch_clauses(&mut self, tail: &[Word]) -> Result<Vec<Clause>, CompileError> {
let mut patterns: Vec<(Option<String>, Option<Word>)> = Vec::new();
let mut bodies: Vec<String> = Vec::new();
if tail.is_empty() {
return self
.error("wrong # args: should be \"switch ?options? string pattern body ...\"");
}
if tail.len() == 1 {
// The grouped form: the whole tail is one list, and because braces
// suppress substitution its patterns are literal text.
let text = self.literal_of(&tail[0], "switch pattern list")?;
let elements = match list::split(text) {
Ok(elements) => elements,
Err(msg) => return self.error(msg),
};
if elements.is_empty() {
return self.error(
"wrong # args: should be \"switch ?options? string {pattern body ...}\"",
);
}
if !elements.len().is_multiple_of(2) {
return self.error("extra switch pattern with no body");
}
for pair in elements.chunks(2) {
patterns.push((Some(pair[0].clone()), None));
bodies.push(pair[1].clone());
}
} else {
if !tail.len().is_multiple_of(2) {
return self.error("extra switch pattern with no body");
}
for pair in tail.chunks(2) {
bodies.push(self.literal_of(&pair[1], "switch body")?.to_string());
match pair[0].as_literal() {
Some(text) => patterns.push((Some(text.to_string()), None)),
None => patterns.push((None, Some(pair[0].clone()))),
}
}
}
let mut clauses = Vec::with_capacity(patterns.len());
for (k, (text, word)) in patterns.into_iter().enumerate() {
let mut at = k;
while bodies[at] == "-" {
at += 1;
if at >= bodies.len() {
return self.error(format!(
"no body specified for pattern \"{}\"",
text.as_deref().unwrap_or_default()
));
}
}
clauses.push(Clause {
text,
word,
body: bodies[at].clone(),
});
}
Ok(clauses)
}
fn switch_body(&mut self, text: &str) -> Result<(), CompileError> {
// An arm that is never selected is never parsed by tclsh, so an arm
// whose text will not parse raises only if it is the arm chosen.
match crate::parser::parse(text) {
Ok(script) => self.nested_value(&script),
Err(e) => {
let msg = e.msg;
self.raise_at_run_time(&msg)
}
}
}
/// `catch script ?resultVarName?`.
pub(crate) fn cmd_catch(&mut self, args: &[Word]) -> Result<(), CompileError> {
let (body, var) = match args {
[b] => (b, None),
[b, v] => (b, Some(self.var_name_of(v)?)),
_ => {
return self.error(
"wrong # args: should be \"catch script ?resultVarName?\"; the options \
variable is not supported",
)
}
};
let script = self.body_of(body)?;
let entry = self.depth;
// The handler comes first so its op index is known when the region is
// opened; the ordinary path jumps over it.
let over = self.emit(Op::Jump(usize::MAX), 0);
let handler = self.b.current_pos();
// The driver resumes here with the error message on the stack.
self.depth = entry + 1;
self.store_or_drop(var.as_deref());
self.emit(Op::LoadInt(1), 1);
let to_end = self.emit(Op::Jump(usize::MAX), 0);
let guarded = self.b.current_pos();
self.b.patch_jump(over, guarded);
self.depth = entry;
self.emit(Op::ExtendedWide(ext_wide::CATCH, handler), 0);
self.catch_depth += 1;
let compiled = self.emit_body_value(&script);
self.catch_depth -= 1;
compiled?;
self.emit(Op::Extended(ext::CATCH_END, 0), 0);
self.store_or_drop(var.as_deref());
self.emit(Op::LoadInt(0), 1);
let end = self.b.current_pos();
self.b.patch_jump(to_end, end);
Ok(())
}
/// `error message` — `info` and `code` set return options this frontend
/// does not model.
pub(crate) fn cmd_error(&mut self, args: &[Word]) -> Result<(), CompileError> {
let [message] = args else {
return self.error(
"wrong # args: should be \"error message\"; the info and code arguments are \
not supported",
);
};
self.word(message)?;
self.emit(Op::Extended(ext::ERROR, 0), -1);
// Control has left; the value keeps the depth arithmetic honest.
self.push_empty();
Ok(())
}
/// Store the top of the stack in `var`, or discard it when `catch` was
/// given no variable to write.
fn store_or_drop(&mut self, var: Option<&str>) {
match var {
Some(name) => self.emit_set_var(name),
None => {
self.emit(Op::Pop, -1);
}
}
}
}