1
0

68 lines
2.0 KiB
Rust
Raw Normal View History

use std::io::{stdin, stdout, Write};
2024-12-17 07:10:47 -05:00
use std::ops::Range;
use crate::vm::{Register, RuntimeValue, Thread};
use crate::DustString;
pub fn read_line(data: &mut Thread, destination: usize, _argument_range: Range<usize>) {
let current_frame = data.current_frame_mut();
let mut buffer = String::new();
2025-01-08 23:02:08 -05:00
if stdin().read_line(&mut buffer).is_ok() {
let length = buffer.len();
2025-01-08 23:02:08 -05:00
buffer.truncate(length.saturating_sub(1));
let string = RuntimeValue::Raw(DustString::from(buffer));
2024-12-17 07:10:47 -05:00
current_frame.registers.strings[destination].set(string);
}
}
pub fn write(data: &mut Thread, _: usize, argument_range: Range<usize>) {
let current_frame = data.current_frame();
let mut stdout = stdout();
2024-12-17 07:10:47 -05:00
for register_index in argument_range {
let value = current_frame.get_string_from_register(register_index);
match value {
RuntimeValue::Raw(value) => {
let _ = stdout.write(value.as_bytes());
}
RuntimeValue::Rc(value) => {
let _ = stdout.write(value.as_bytes());
}
RuntimeValue::RefCell(ref_cell) => {
let _ = stdout.write(ref_cell.borrow().as_bytes());
}
}
}
2025-01-08 23:02:08 -05:00
let _ = stdout.flush();
}
pub fn write_line(data: &mut Thread, _: usize, argument_range: Range<usize>) {
let current_frame = data.current_frame();
2024-12-17 07:10:47 -05:00
let mut stdout = stdout().lock();
2024-12-17 07:10:47 -05:00
for register_index in argument_range {
let value = current_frame.get_string_from_register(register_index);
match value {
RuntimeValue::Raw(value) => {
let _ = stdout.write(value.as_bytes());
}
RuntimeValue::Rc(value) => {
let _ = stdout.write(value.as_bytes());
}
RuntimeValue::RefCell(ref_cell) => {
let _ = stdout.write(ref_cell.borrow().as_bytes());
}
}
}
let _ = stdout.write(b"\n");
2025-01-08 23:02:08 -05:00
let _ = stdout.flush();
}