mech-interpreter 0.3.3

The Mech language runtime.
Documentation
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
use crate::*;

// Patterns
// ----------------------------------------------------------------------------

// Implements pattern matching. Handles matching runtime Values against 
// Pattern AST nodes, binding variables into an Environment. 
 
// Supports destructuring of tuples, arrays/matrices, and tagged tuple-structs, 
// and reconstructing values from patterns.

// Used by functinos, state machines, and option guards.

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum PatternMatchSemantics {
  Standard,     // Standard semantics: pattern expressions are evaluated once and compared to the value.
  OptionGuard,  // Option guard semantics: pattern expressions are evaluated for each value and must evaluate to true for all to match.
}

// Entry point for multi-argument dispatch. When a function is called with 
// multiple arguments, this wraps them as if they were a tuple and delegates 
// to pattern_matches_value.
pub fn pattern_matches_arguments(pattern: &Pattern, args: &Vec<Value>, env: &mut Environment, p: &Interpreter) -> MResult<bool> {
  if args.len() == 1 {
    return pattern_matches_value(pattern, &args[0], env, p);
  }
  match pattern {
    Pattern::Tuple(pattern_tuple) => {
      if pattern_tuple.0.len() != args.len() {
        return Ok(false);
      }
      for (pat, arg) in pattern_tuple.0.iter().zip(args.iter()) {
        if !pattern_matches_value(pat, arg, env, p)? {
          return Ok(false);
        }
      }
      Ok(true)
    }
    _ => Ok(false),
  }
}

pub fn pattern_matches_value(pattern: &Pattern, value: &Value, env: &mut Environment, p: &Interpreter) -> MResult<bool> {
  pattern_matches_value_with_semantics(pattern, value, env, p, PatternMatchSemantics::Standard)
}

// Takes a semantics mode. Dispatches on pattern kind:
// Wildcard - always succeeds, binds nothing.
// - Tuple - recursively matches element-wise against a Value::Tuple, requiring equal arity.
// - Array - matches prefix and suffix element-wise against any matrix-like value, with optional spread binding (...) for the middle slice.
// - Expression(Var()) - if the variable is already in the environment, checks equality (used for repeated variable patterns).
// - Expression(other) - attempts to extract a variable id from more complex expression wrappers (parentheticals, single-term formulas) and handles them like Var.
// - TupleStruct - matches a tagged tuple
pub fn pattern_matches_value_with_semantics(pattern: &Pattern, value: &Value, env: &mut Environment, p: &Interpreter, semantics: PatternMatchSemantics) -> MResult<bool> {
  let detached_value = deep_detach_value(value);
  match pattern {
    Pattern::Wildcard => Ok(true),
    #[cfg(feature = "tuple")]
    Pattern::Tuple(pattern_tuple) => {
      match detached_value {
        Value::Tuple(tuple) => {
          let tuple_brrw = tuple.borrow();
          if pattern_tuple.0.len() != tuple_brrw.elements.len() {
            return Ok(false);
          }
          for (pat, val) in pattern_tuple.0.iter().zip(tuple_brrw.elements.iter()) {
            if !pattern_matches_value_with_semantics(pat, val, env, p, semantics)? {
              return Ok(false);
            }
          }
          return Ok(true);
        }
        _ => {return Ok(false);},
      }
      return Ok(false);
    }
    #[cfg(feature = "matrix")]
    Pattern::Array(pattern_array) => {
      let values = match matrix_like_values(&detached_value) {
        Some(values) => values,
        None => return Ok(false),
      };
      if values.len() < pattern_array.prefix.len() + pattern_array.suffix.len() {
        return Ok(false);
      }
      for (pat, val) in pattern_array.prefix.iter().zip(values.iter()) {
        if !pattern_matches_value_with_semantics(pat, val, env, p, semantics)? {
          return Ok(false);
        }
      }
      let suffix_start = values.len() - pattern_array.suffix.len();
      for (pat, val) in pattern_array
          .suffix
          .iter()
          .zip(values[suffix_start..].iter())
      {
        if !pattern_matches_value_with_semantics(pat, val, env, p, semantics)? {
          return Ok(false);
        }
      }

      // If there's no spread, the number of values must match exactly. 
      // If there is a spread, there can be any number of values in the middle.
      if pattern_array.spread.is_none() && values.len() != pattern_array.prefix.len() + pattern_array.suffix.len()
      {
        return Ok(false);
      }
      if let Some(spread) = &pattern_array.spread {
        if let Some(binding) = &spread.binding {
          let middle_start = pattern_array.prefix.len();
          #[cfg(feature = "matrix")]
          let captured = capture_middle_matrix(&detached_value, middle_start, suffix_start);
          if !pattern_matches_value_with_semantics(binding, &captured, env, p, semantics)?
          {
            return Ok(false);
          }
        }
      }
      Ok(true)
    }
    Pattern::Expression(Expression::Var(var)) => {
      let var_id = var.name.hash();
      if let Some(existing) = env.get(&var_id) {
        Ok(existing == &detached_value)
      } else {
        env.insert(var_id, detached_value);
        Ok(true)
      }
    }
    Pattern::Expression(expr) => {
      if let Some(var_id) = extract_pattern_variable_id(expr) {
        if let Some(existing) = env.get(&var_id) {
          return Ok(existing == &detached_value);
        }
        env.insert(var_id, detached_value);
        return Ok(true);
      }
      let expected = expression(expr, Some(env), p)?;
      if semantics == PatternMatchSemantics::OptionGuard {
        #[cfg(feature = "bool")]
        if let Value::Bool(flag) = &expected {
          return Ok(*flag.borrow());
        }
      }
      Ok(values_match(&deep_detach_value(&expected), &detached_value))
    }
    #[cfg(all(feature = "tuple", feature = "atom"))]
    Pattern::TupleStruct(pat_struct) => {
      match detached_value {
        Value::Tuple(tuple) => {
          let tuple_brrw = tuple.borrow();
          if tuple_brrw.elements.len() != pat_struct.patterns.len() + 1 {
            return Ok(false);
          }
          let expected_state = atom(&Atom {name: pat_struct.name.clone(),},p);
          if !values_match(&expected_state, &deep_detach_value(&tuple_brrw.elements[0])) {
            return Ok(false);
          }
          for (pat, val) in pat_struct
              .patterns
              .iter()
              .zip(tuple_brrw.elements.iter().skip(1))
          {
            if !pattern_matches_value_with_semantics(pat, val, env, p, semantics)? {
              return Ok(false);
            }
          }
          return Ok(true);
        }
        _ => return Ok(false),
      }
      return Ok(false);
    }
    x => Err(MechError::new(FeatureNotEnabledError, Some(format!("Pattern not enabled: {:?}", x))).with_compiler_loc().with_tokens(x.tokens())), 
  }
}


// Collects all variable ids introduced by a pattern (via 
// collect_pattern_variable_ids) and removes them from the environment. Used 
// to undo bindings when a pattern arm fails or needs to be retried.
pub fn clear_pattern_bindings(pattern: &Pattern, env: &mut Environment) {
  let mut ids = Vec::new();
  collect_pattern_variable_ids(pattern, &mut ids);
  for var_id in ids {
    env.remove(&var_id);
  }
}


// Reconstructs a Value from a pattern using the current environment. This is the inverse of matching. used to extract or re-emit bound values.
pub fn pattern_to_value(pattern: &Pattern, env: &Environment, p: &Interpreter) -> MResult<Value> {
  match pattern {
    Pattern::Wildcard => Ok(Value::Empty),
    Pattern::Expression(expr) => expression(expr, Some(env), p),
    #[cfg(feature = "tuple")]
    Pattern::Tuple(pattern_tuple) => {
      let mut values = Vec::with_capacity(pattern_tuple.0.len());
      for inner in &pattern_tuple.0 {
        values.push(pattern_to_value(inner, env, p)?);
      }
      return Ok(Value::Tuple(Ref::new(MechTuple::from_vec(values))));
    }
    #[cfg(feature = "matrix")]
    Pattern::Array(array) => {
      let mut values = Vec::new();
      for inner in &array.prefix {
        values.push(pattern_to_value(inner, env, p)?);
      }
      if let Some(spread) = &array.spread {
        if let Some(binding) = &spread.binding {
          let bound = pattern_to_value(binding, env, p)?;
          if let Some(bound_values) = matrix_like_values(&bound) {
            values.extend(bound_values);
          } else {
            values.push(bound);
          }
        }
      }
      for inner in &array.suffix {
        values.push(pattern_to_value(inner, env, p)?);
      }
      return Ok(build_row_matrix_from_values(values));
    }
    #[cfg(all(feature = "tuple", feature = "atom"))]
    Pattern::TupleStruct(pattern_tuple_struct) => {
      let mut values = Vec::with_capacity(pattern_tuple_struct.patterns.len() + 1);
      values.push(atom(&Atom {name: pattern_tuple_struct.name.clone()}, p));
      for inner in &pattern_tuple_struct.patterns {
        values.push(pattern_to_value(inner, env, p)?);
      }
      return Ok(Value::Tuple(Ref::new(MechTuple::from_vec(values))));
    }
    _ => Err(MechError::new(FeatureNotEnabledError, None).with_compiler_loc()),
  }
}

// Mutable reference unwrapper. Recursively follows Value::MutableReference 
// chains until it reaches a plain value, then clones it. Ensures the pattern 
// matcher always works on an owned, non-reference value.
fn deep_detach_value(value: &Value) -> Value {
  match value {
    Value::MutableReference(reference) => deep_detach_value(&reference.borrow()),
    _ => value.clone(),
  }
}

// Variable id harvester. Recursively walks a pattern and pushes the hashed ids 
// of all bound variable names into a Vec<u64>. Handles Var expressions, tuples, 
// arrays (including spread bindings), and tuple-structs. Used by 
// clear_pattern_bindings.
fn collect_pattern_variable_ids(pattern: &Pattern, ids: &mut Vec<u64>) {
  match pattern {
    Pattern::Expression(Expression::Var(var)) => ids.push(var.name.hash()),
    #[cfg(feature = "tuple")]
    Pattern::Tuple(tuple) => {
      for item in &tuple.0 {
        collect_pattern_variable_ids(item, ids);
      }
    }
    #[cfg(feature = "matrix")]
    Pattern::Array(array) => {
      for item in &array.prefix {
        collect_pattern_variable_ids(item, ids);
      }
      if let Some(spread) = &array.spread {
        if let Some(binding) = &spread.binding {
          collect_pattern_variable_ids(binding, ids);
        }
      }
      for item in &array.suffix {
        collect_pattern_variable_ids(item, ids);
      }
    }
    #[cfg(all(feature = "tuple", feature = "atom"))]
    Pattern::TupleStruct(tuple_struct) => {
      for item in &tuple_struct.patterns {
        collect_pattern_variable_ids(item, ids);
      }
    }
    _ => {}
  }
}

#[cfg(feature = "matrix")]
fn capture_middle_matrix(value: &Value, start: usize, end: usize) -> Value {
  let cols = end.saturating_sub(start);
  match value {
    #[cfg(feature = "matrix")]
    Value::MatrixIndex(matrix) => Value::MatrixIndex(Matrix::from_vec(matrix.as_vec()[start..end].to_vec(), 1, cols)),
    #[cfg(all(feature = "matrix", feature = "bool"))]
    Value::MatrixBool(matrix) => Value::MatrixBool(Matrix::from_vec(matrix.as_vec()[start..end].to_vec(), 1, cols)),
    #[cfg(all(feature = "matrix", feature = "u8"))]
    Value::MatrixU8(matrix) => Value::MatrixU8(Matrix::from_vec(matrix.as_vec()[start..end].to_vec(), 1, cols)),
    #[cfg(all(feature = "matrix", feature = "u16"))]
    Value::MatrixU16(matrix) => Value::MatrixU16(Matrix::from_vec(matrix.as_vec()[start..end].to_vec(), 1, cols)),
    #[cfg(all(feature = "matrix", feature = "u32"))]
    Value::MatrixU32(matrix) => Value::MatrixU32(Matrix::from_vec(matrix.as_vec()[start..end].to_vec(), 1, cols)),
    #[cfg(all(feature = "matrix", feature = "u64"))]
    Value::MatrixU64(matrix) => Value::MatrixU64(Matrix::from_vec(matrix.as_vec()[start..end].to_vec(), 1, cols)),
    #[cfg(all(feature = "matrix", feature = "u128"))]
    Value::MatrixU128(matrix) => Value::MatrixU128(Matrix::from_vec(matrix.as_vec()[start..end].to_vec(), 1, cols)),
    #[cfg(all(feature = "matrix", feature = "i8"))]
    Value::MatrixI8(matrix) => Value::MatrixI8(Matrix::from_vec(matrix.as_vec()[start..end].to_vec(), 1, cols)),
    #[cfg(all(feature = "matrix", feature = "i16"))]
    Value::MatrixI16(matrix) => Value::MatrixI16(Matrix::from_vec(matrix.as_vec()[start..end].to_vec(), 1, cols)),
    #[cfg(all(feature = "matrix", feature = "i32"))]
    Value::MatrixI32(matrix) => Value::MatrixI32(Matrix::from_vec(matrix.as_vec()[start..end].to_vec(), 1, cols)),
    #[cfg(all(feature = "matrix", feature = "i64"))]
    Value::MatrixI64(matrix) => Value::MatrixI64(Matrix::from_vec(matrix.as_vec()[start..end].to_vec(), 1, cols)),
    #[cfg(all(feature = "matrix", feature = "i128"))]
    Value::MatrixI128(matrix) => Value::MatrixI128(Matrix::from_vec(matrix.as_vec()[start..end].to_vec(), 1, cols)),
    #[cfg(all(feature = "matrix", feature = "f32"))]
    Value::MatrixF32(matrix) => Value::MatrixF32(Matrix::from_vec(matrix.as_vec()[start..end].to_vec(), 1, cols)),
    #[cfg(all(feature = "matrix", feature = "f64"))]
    Value::MatrixF64(matrix) => Value::MatrixF64(Matrix::from_vec(matrix.as_vec()[start..end].to_vec(), 1, cols)),
    #[cfg(all(feature = "matrix", feature = "string"))]
    Value::MatrixString(matrix) => Value::MatrixString(Matrix::from_vec(matrix.as_vec()[start..end].to_vec(), 1, cols)),
    #[cfg(all(feature = "matrix", feature = "rational"))]
    Value::MatrixR64(matrix) => Value::MatrixR64(Matrix::from_vec(matrix.as_vec()[start..end].to_vec(), 1, cols)),
    #[cfg(all(feature = "matrix", feature = "complex"))]
    Value::MatrixC64(matrix) => Value::MatrixC64(Matrix::from_vec(matrix.as_vec()[start..end].to_vec(), 1, cols)),
    #[cfg(feature = "matrix")]
    Value::MatrixValue(matrix) => Value::MatrixValue(Matrix::from_vec(matrix.as_vec()[start..end].to_vec(), 1, cols)),
    _ => {
      let values = matrix_like_values(value).unwrap_or_default();
      Value::MatrixValue(Matrix::from_vec(values[start..end].to_vec(), 1, cols))
    }
  }
}

// Used by the Array pattern arm to get a uniform element list regardless of the matrix's concrete numeric type.
pub(crate) fn matrix_like_values(value: &Value) -> Option<Vec<Value>> {
  match value {
    #[cfg(feature = "matrix")]
    Value::MatrixIndex(matrix) => Some(
      matrix
          .as_vec()
          .into_iter()
          .map(|value| Value::Index(Ref::new(value)))
          .collect(),
    ),
    #[cfg(all(feature = "matrix", feature = "bool"))]
    Value::MatrixBool(matrix) => Some(matrix.as_vec().into_iter().map(Value::from).collect()),
    #[cfg(all(feature = "matrix", feature = "u8"))]
    Value::MatrixU8(matrix) => Some(matrix.as_vec().into_iter().map(Value::from).collect()),
    #[cfg(all(feature = "matrix", feature = "u16"))]
    Value::MatrixU16(matrix) => Some(matrix.as_vec().into_iter().map(Value::from).collect()),
    #[cfg(all(feature = "matrix", feature = "u32"))]
    Value::MatrixU32(matrix) => Some(matrix.as_vec().into_iter().map(Value::from).collect()),
    #[cfg(all(feature = "matrix", feature = "u64"))]
    Value::MatrixU64(matrix) => Some(matrix.as_vec().into_iter().map(Value::from).collect()),
    #[cfg(all(feature = "matrix", feature = "u128"))]
    Value::MatrixU128(matrix) => Some(matrix.as_vec().into_iter().map(Value::from).collect()),
    #[cfg(all(feature = "matrix", feature = "i8"))]
    Value::MatrixI8(matrix) => Some(matrix.as_vec().into_iter().map(Value::from).collect()),
    #[cfg(all(feature = "matrix", feature = "i16"))]
    Value::MatrixI16(matrix) => Some(matrix.as_vec().into_iter().map(Value::from).collect()),
    #[cfg(all(feature = "matrix", feature = "i32"))]
    Value::MatrixI32(matrix) => Some(matrix.as_vec().into_iter().map(Value::from).collect()),
    #[cfg(all(feature = "matrix", feature = "i64"))]
    Value::MatrixI64(matrix) => Some(matrix.as_vec().into_iter().map(Value::from).collect()),
    #[cfg(all(feature = "matrix", feature = "i128"))]
    Value::MatrixI128(matrix) => Some(matrix.as_vec().into_iter().map(Value::from).collect()),
    #[cfg(all(feature = "matrix", feature = "f32"))]
    Value::MatrixF32(matrix) => Some(matrix.as_vec().into_iter().map(Value::from).collect()),
    #[cfg(all(feature = "matrix", feature = "f64"))]
    Value::MatrixF64(matrix) => Some(matrix.as_vec().into_iter().map(Value::from).collect()),
    #[cfg(all(feature = "matrix", feature = "string"))]
    Value::MatrixString(matrix) => Some(matrix.as_vec().into_iter().map(Value::from).collect()),
    #[cfg(all(feature = "matrix", feature = "rational"))]
    Value::MatrixR64(matrix) => Some(
      matrix
          .as_vec()
          .into_iter()
          .map(|value| value.to_value())
          .collect(),
    ),
    #[cfg(all(feature = "matrix", feature = "complex"))]
    Value::MatrixC64(matrix) => Some(
      matrix
          .as_vec()
          .into_iter()
          .map(|value| value.to_value())
          .collect(),
    ),
    #[cfg(feature = "matrix")]
    Value::MatrixValue(matrix) => Some(matrix.as_vec()),
    _ => None,
  }
}

#[cfg(feature = "matrix")]
fn build_row_matrix_from_values(values: Vec<Value>) -> Value {
  let cols = values.len();
  #[cfg(feature = "u64")]
  if values.iter().all(|value| matches!(value, Value::U64(_))) {
    let data = values
      .iter()
      .map(|value| match value {
        Value::U64(x) => *x.borrow(),
        _ => unreachable!(),
      })
      .collect::<Vec<u64>>();
    return Value::MatrixU64(Matrix::from_vec(data, 1, cols));
  }
  Value::MatrixValue(Matrix::from_vec(values, 1, cols))
}

fn extract_pattern_variable_id(expr: &Expression) -> Option<u64> {
  match expr {
    Expression::Var(var) => Some(var.name.hash()),
    Expression::Formula(factor) => match factor {
      Factor::Expression(inner_expr) => extract_pattern_variable_id(inner_expr),
      Factor::Term(term) if term.rhs.is_empty() => extract_pattern_variable_id_from_term(&term.lhs),
      _ => None,
    },
    _ => None,
  }
}

fn extract_pattern_variable_id_from_term(factor: &Factor) -> Option<u64> {
  match factor {
    Factor::Expression(expr) => extract_pattern_variable_id(expr),
    Factor::Parenthetical(inner) => extract_pattern_variable_id_from_term(inner),
    _ => None,
  }
}

// TODO: This needs to be expanded to handle all types.
fn values_match(expected: &Value, actual: &Value) -> bool {
  if expected == actual {
    return true;
  }
  match (expected, actual) {
    #[cfg(all(feature = "u64", feature = "f64"))]
    (Value::F64(x), Value::U64(y)) => return (*x.borrow() as u64) == *y.borrow(),
    #[cfg(all(feature = "u64", feature = "f64"))]
    (Value::U64(x), Value::F64(y)) => return *x.borrow() == (*y.borrow() as u64),
    _ => {}
  }
  false
}