diff --git a/src/interpreter/objects.rs b/src/interpreter/objects.rs index c90d1e53..1588f58f 100644 --- a/src/interpreter/objects.rs +++ b/src/interpreter/objects.rs @@ -17,6 +17,64 @@ impl NyashInterpreter { -> Result, RuntimeError> { // 組み込みBox型のチェック match class { + // Basic Box constructors (CRITICAL - these were missing!) + "StringBox" => { + // StringBoxは引数1個(文字列値)で作成 + if arguments.len() != 1 { + return Err(RuntimeError::InvalidOperation { + message: format!("StringBox constructor expects 1 argument, got {}", arguments.len()), + }); + } + let value = self.execute_expression(&arguments[0])?; + let string_value = value.to_string_box().value; + let string_box = Box::new(StringBox::new(string_value)) as Box; + return Ok(string_box); + } + "IntegerBox" => { + // IntegerBoxは引数1個(整数値)で作成 + if arguments.len() != 1 { + return Err(RuntimeError::InvalidOperation { + message: format!("IntegerBox constructor expects 1 argument, got {}", arguments.len()), + }); + } + let value = self.execute_expression(&arguments[0])?; + if let Some(int_box) = value.as_any().downcast_ref::() { + let integer_box = Box::new(IntegerBox::new(int_box.value)) as Box; + return Ok(integer_box); + } else { + // Try to parse from string or other types + let int_value = value.to_string_box().value.parse::() + .map_err(|_| RuntimeError::TypeError { + message: format!("Cannot convert '{}' to integer", value.to_string_box().value), + })?; + let integer_box = Box::new(IntegerBox::new(int_value)) as Box; + return Ok(integer_box); + } + } + "BoolBox" => { + // BoolBoxは引数1個(真偽値)で作成 + if arguments.len() != 1 { + return Err(RuntimeError::InvalidOperation { + message: format!("BoolBox constructor expects 1 argument, got {}", arguments.len()), + }); + } + let value = self.execute_expression(&arguments[0])?; + if let Some(bool_box) = value.as_any().downcast_ref::() { + let bool_box_new = Box::new(BoolBox::new(bool_box.value)) as Box; + return Ok(bool_box_new); + } else { + // Try to convert from string or other types + let bool_value = match value.to_string_box().value.to_lowercase().as_str() { + "true" => true, + "false" => false, + _ => return Err(RuntimeError::TypeError { + message: format!("Cannot convert '{}' to boolean", value.to_string_box().value), + }), + }; + let bool_box_new = Box::new(BoolBox::new(bool_value)) as Box; + return Ok(bool_box_new); + } + } "ArrayBox" => { // ArrayBoxは引数なしで作成 if !arguments.is_empty() { diff --git a/test_basic_box_comprehensive.nyash b/test_basic_box_comprehensive.nyash new file mode 100644 index 00000000..de1ac467 --- /dev/null +++ b/test_basic_box_comprehensive.nyash @@ -0,0 +1,46 @@ +// Comprehensive test to verify Basic Box constructors work identically to literals +// This demonstrates that Problem 1 is fully resolved + +local console = new ConsoleBox() +console.log("=== Comprehensive Basic Box Constructor Test ===") + +// Test StringBox equivalence +local str_box = new StringBox("test") +local str_literal = "test" +console.log("StringBox toString: " + str_box.toString()) +console.log("String literal: " + str_literal) +console.log("StringBox == literal: " + (str_box.toString() == str_literal)) + +console.log("---") + +// Test IntegerBox equivalence +local int_box = new IntegerBox(123) +local int_literal = 123 +console.log("IntegerBox toString: " + int_box.toString()) +console.log("Integer literal: " + int_literal) +console.log("IntegerBox == literal: " + (int_box.toString() == int_literal.toString())) + +console.log("---") + +// Test BoolBox equivalence +local bool_box = new BoolBox(true) +local bool_literal = true +console.log("BoolBox toString: " + bool_box.toString()) +console.log("Bool literal: " + bool_literal) +console.log("BoolBox == literal: " + (bool_box.toString() == bool_literal.toString())) + +console.log("---") + +// Test type conversion capabilities +local str_from_int = new StringBox(456) +console.log("StringBox from int: " + str_from_int.toString()) + +local int_from_str = new IntegerBox("789") +console.log("IntegerBox from string: " + int_from_str.toString()) + +local bool_from_str_true = new BoolBox("true") +local bool_from_str_false = new BoolBox("false") +console.log("BoolBox from 'true': " + bool_from_str_true.toString()) +console.log("BoolBox from 'false': " + bool_from_str_false.toString()) + +console.log("=== All Basic Box Constructors Working! ===") \ No newline at end of file