diff --git a/src/interpreter/core.rs b/src/interpreter/core.rs index 843e39c1..6945ee82 100644 --- a/src/interpreter/core.rs +++ b/src/interpreter/core.rs @@ -29,6 +29,15 @@ fn debug_log(msg: &str) { } } +// Conditional debug macro - only outputs if NYASH_DEBUG=1 environment variable is set +macro_rules! debug_trace { + ($($arg:tt)*) => { + if std::env::var("NYASH_DEBUG").unwrap_or_default() == "1" { + eprintln!($($arg)*); + } + }; +} + /// 実行時エラー #[derive(Error, Debug)] pub enum RuntimeError { diff --git a/src/interpreter/expressions/access.rs b/src/interpreter/expressions/access.rs index 13885afd..bf5028d9 100644 --- a/src/interpreter/expressions/access.rs +++ b/src/interpreter/expressions/access.rs @@ -10,6 +10,15 @@ use crate::instance_v2::InstanceBox; use crate::interpreter::core::{NyashInterpreter, RuntimeError}; use std::sync::Arc; +// Conditional debug macro - only outputs if NYASH_DEBUG=1 environment variable is set +macro_rules! debug_trace { + ($($arg:tt)*) => { + if std::env::var("NYASH_DEBUG").unwrap_or_default() == "1" { + eprintln!($($arg)*); + } + }; +} + impl NyashInterpreter { /// フィールドアクセスを実行 - Field access processing with weak reference support pub(super) fn execute_field_access(&mut self, object: &ASTNode, field: &str) @@ -54,12 +63,12 @@ impl NyashInterpreter { if let Some(weak_value) = instance.get_weak_field(field, self) { // Pass self match &weak_value { crate::value::NyashValue::Null => { - eprintln!("🔗 DEBUG: Weak field '{}' is null (reference dropped)", field); + debug_trace!("🔗 DEBUG: Weak field '{}' is null (reference dropped)", field); // Return null box for compatibility return Ok(Arc::new(crate::boxes::null_box::NullBox::new())); } _ => { - eprintln!("🔗 DEBUG: Weak field '{}' still has valid reference", field); + debug_trace!("🔗 DEBUG: Weak field '{}' still has valid reference", field); // Convert back to Box for now if let Ok(box_value) = weak_value.to_box() { if let Ok(inner_box) = box_value.try_lock() { diff --git a/src/interpreter/statements.rs b/src/interpreter/statements.rs index 7379744c..431ff3d4 100644 --- a/src/interpreter/statements.rs +++ b/src/interpreter/statements.rs @@ -10,6 +10,15 @@ use super::*; use super::BuiltinStdlib; use std::sync::Arc; +// Conditional debug macro - only outputs if NYASH_DEBUG=1 environment variable is set +macro_rules! debug_trace { + ($($arg:tt)*) => { + if std::env::var("NYASH_DEBUG").unwrap_or_default() == "1" { + eprintln!($($arg)*); + } + }; +} + impl NyashInterpreter { /// 文を実行 - Core statement execution engine pub(super) fn execute_statement(&mut self, statement: &ASTNode) -> Result, RuntimeError> { @@ -253,17 +262,17 @@ impl NyashInterpreter { // 🔗 DEMO: Weak Reference Invalidation Simulation // If we're setting a variable to 0, simulate "dropping" the previous value if val.to_string_box().value == "0" { - eprintln!("🔗 DEBUG: Variable '{}' set to 0 - simulating object drop", name); + debug_trace!("🔗 DEBUG: Variable '{}' set to 0 - simulating object drop", name); // Get the current value before dropping it if let Ok(old_value) = self.resolve_variable(name) { let old_value_str = old_value.to_string_box().value; - eprintln!("🔗 DEBUG: Old value being dropped: {}", old_value_str); + debug_trace!("🔗 DEBUG: Old value being dropped: {}", old_value_str); // For demo purposes, if we're dropping a "parent" variable, // manually invalidate weak references to Parent instances if name.contains("parent") && old_value_str.contains("instance #") { - eprintln!("🔗 DEBUG: Triggering weak reference invalidation for: {}", old_value_str); + debug_trace!("🔗 DEBUG: Triggering weak reference invalidation for: {}", old_value_str); // Call the interpreter method with actual object info self.trigger_weak_reference_invalidation(&old_value_str); @@ -291,7 +300,7 @@ impl NyashInterpreter { let box_decls = self.shared.box_declarations.read().unwrap(); if let Some(box_decl) = box_decls.get(&instance.class_name) { if box_decl.weak_fields.contains(&field.to_string()) { - eprintln!("🔗 DEBUG: Assigning to weak field '{}' in class '{}'", field, instance.class_name); + debug_trace!("🔗 DEBUG: Assigning to weak field '{}' in class '{}'", field, instance.class_name); // 🎯 PHASE 2: Use the new legacy conversion helper instance.set_weak_field_from_legacy(field.to_string(), val.clone_box())