1
0

Fix write and write_line

This commit is contained in:
Jeff 2024-10-30 05:31:46 -04:00
parent c264aaeb13
commit d7be203bfc

View File

@ -428,52 +428,55 @@ impl Vm {
Some(Value::Primitive(Primitive::String(string))) Some(Value::Primitive(Primitive::String(string)))
} }
NativeFunction::Write => { NativeFunction::Write => {
let to_register = instruction.a();
let mut stdout = stdout(); let mut stdout = stdout();
let map_err = |io_error: io::Error| VmError::Io {
for argument_index in 0..argument_count {
if argument_index != 0 {
stdout.write(b" ").map_err(|io_error| VmError::Io {
error: io_error.kind(), error: io_error.kind(),
position, position,
})?; };
let first_argument = to_register.saturating_sub(argument_count);
let last_argument = to_register.saturating_sub(1);
for argument_index in first_argument..=last_argument {
if argument_index != first_argument {
stdout.write(b" ").map_err(map_err)?;
} }
let argument = self.get(argument_index, position)?; let argument_string =
self.get(argument_index, position)?.to_string();
write!(stdout, "{}", argument).map_err(|io_error| VmError::Io { stdout
error: io_error.kind(), .write_all(argument_string.as_bytes())
position, .map_err(map_err)?;
})?;
} }
None None
} }
NativeFunction::WriteLine => { NativeFunction::WriteLine => {
let mut stdout = stdout(); let mut stdout = stdout();
let map_err = |io_error: io::Error| VmError::Io {
for argument_index in 0..argument_count {
if argument_index != 0 {
stdout.write(b" ").map_err(|io_error| VmError::Io {
error: io_error.kind(), error: io_error.kind(),
position, position,
})?; };
let first_argument = to_register.saturating_sub(argument_count);
let last_argument = to_register.saturating_sub(1);
for argument_index in first_argument..=last_argument {
if argument_index != 0 {
stdout.write(b" ").map_err(map_err)?;
} }
let argument_string = let argument_string =
self.get(argument_index, position)?.to_string(); self.get(argument_index, position)?.to_string();
stdout.write_all(argument_string.as_bytes()).map_err( stdout
|io_error| VmError::Io { .write_all(argument_string.as_bytes())
error: io_error.kind(), .map_err(map_err)?;
position,
},
)?;
} }
stdout.write(b"\n").map_err(|io_error| VmError::Io { stdout.write(b"\n").map_err(map_err)?;
error: io_error.kind(),
position,
})?;
None None
} }