visitPatternVariableDeclarationStatement method

  1. @override
Object? visitPatternVariableDeclarationStatement(
  1. SPatternVariableDeclarationStatement node
)
override

Implementation

@override
Object? visitPatternVariableDeclarationStatement(
  SPatternVariableDeclarationStatement node,
) {
  final patternDecl = node.declaration;
  final pattern = patternDecl.pattern;
  final initializer = patternDecl.expression;

  // Evaluate the right-hand side value
  final rhsValue = initializer.accept<Object?>(this);

  // Match and bind the pattern to the value
  try {
    _matchAndBind(pattern, rhsValue, environment);
  } on PatternMatchD4rtException catch (e) {
    // Convert pattern match failures to standard RuntimeError for now
    throw RuntimeD4rtException("Pattern match failed: ${e.message}");
  } on TypeError {
    // SCD64: a `TypeError` raised while binding is the PROGRAM's error, not
    // this machinery failing, so it keeps its identity instead of being
    // wrapped below. `var (a!) = maybeNull;` is legal Dart whose entire
    // purpose is to raise one, and a script's `on TypeError` has to see it.
    rethrow;
  } catch (e, s) {
    // Add stack trace capture
    Logger.error(
      "during pattern binding: $e\nStack trace:\n$s",
    ); // Display the stack trace
    // Catch other potential errors during binding
    throw RuntimeD4rtException("Error during pattern binding: $e");
  }
  return null;
}