Format array.__str__ output

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2022-02-17 19:02:13 -08:00
parent 76a50ce8c0
commit 9801674815

View File

@@ -29,7 +29,7 @@ thread_local! {
0 => { 0 => {
let array = machine.stack_pop()?; let array = machine.stack_pop()?;
let index = IntObj::new(0); let index = IntObj::new(0);
let working = StrObj::new(String::new()); let working = StrObj::new(String::from("{"));
machine.stack_push(working)?; machine.stack_push(working)?;
machine.stack_push(index)?; machine.stack_push(index)?;
machine.stack_push(array)?; machine.stack_push(array)?;
@@ -48,7 +48,12 @@ thread_local! {
.ok_or_else(|| RuntimeError::WrongValue("int".to_string()))?; .ok_or_else(|| RuntimeError::WrongValue("int".to_string()))?;
let index = index_int.value() as usize; let index = index_int.value() as usize;
if array.value().len() <= index { if array.value().len() <= index {
// done iterating, exit // done iterating, add closing brace and then exit
let working_obj = machine.stack_pop()?;
let working = working_obj.as_any().downcast_ref::<StrObj>()
.ok_or_else(|| RuntimeError::WrongValue("string".to_string()))?;
let str_final = StrObj::new(format!("{} }}", working.value()));
machine.stack_push(str_final)?;
return Ok(BuiltinExit::Return); return Ok(BuiltinExit::Return);
} }
// get the current item from the array, push the index and // get the current item from the array, push the index and
@@ -82,7 +87,7 @@ thread_local! {
let working = working_obj.as_any().downcast_ref::<StrObj>() let working = working_obj.as_any().downcast_ref::<StrObj>()
.ok_or_else(|| RuntimeError::WrongValue("str".to_string()))?; .ok_or_else(|| RuntimeError::WrongValue("str".to_string()))?;
let new_str = StrObj::new(format!("{}{}", working.value(), returned.value())); let new_str = StrObj::new(format!("{} {}", working.value(), returned.value()));
// index gets incremented here // index gets incremented here
let new_index = IntObj::new(index.value() + 1); let new_index = IntObj::new(index.value() + 1);