2025-02-16 22:55:55 -05:00
|
|
|
use std::io::{stdin, stdout, Write};
|
2024-12-17 07:10:47 -05:00
|
|
|
use std::ops::Range;
|
2024-12-10 01:34:53 -05:00
|
|
|
|
2025-02-17 08:01:10 -05:00
|
|
|
use crate::vm::Thread;
|
2025-02-05 19:12:26 -05:00
|
|
|
use crate::DustString;
|
2024-12-10 01:34:53 -05:00
|
|
|
|
2025-02-03 17:49:38 -05:00
|
|
|
pub fn read_line(data: &mut Thread, destination: usize, _argument_range: Range<usize>) {
|
2025-02-16 22:55:55 -05:00
|
|
|
let current_frame = data.current_frame_mut();
|
2024-12-10 01:34:53 -05:00
|
|
|
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();
|
2024-12-10 01:34:53 -05:00
|
|
|
|
2025-01-08 23:02:08 -05:00
|
|
|
buffer.truncate(length.saturating_sub(1));
|
2024-12-10 01:34:53 -05:00
|
|
|
|
2025-02-17 08:01:10 -05:00
|
|
|
let string = DustString::from(buffer);
|
2024-12-17 07:10:47 -05:00
|
|
|
|
2025-02-17 08:01:10 -05:00
|
|
|
current_frame
|
|
|
|
.registers
|
|
|
|
.strings
|
|
|
|
.set_to_new_register(destination, string);
|
2024-12-10 01:34:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-03 17:49:38 -05:00
|
|
|
pub fn write(data: &mut Thread, _: usize, argument_range: Range<usize>) {
|
2025-02-17 08:01:10 -05:00
|
|
|
let current_frame = data.current_frame_mut();
|
2024-12-10 01:34:53 -05:00
|
|
|
let mut stdout = stdout();
|
|
|
|
|
2024-12-17 07:10:47 -05:00
|
|
|
for register_index in argument_range {
|
2025-02-17 10:04:31 -05:00
|
|
|
let string = current_frame
|
|
|
|
.registers
|
|
|
|
.strings
|
|
|
|
.get(register_index)
|
|
|
|
.as_value();
|
2025-02-17 08:01:10 -05:00
|
|
|
let _ = stdout.write(string.as_bytes());
|
2024-12-10 01:34:53 -05:00
|
|
|
}
|
|
|
|
|
2025-01-08 23:02:08 -05:00
|
|
|
let _ = stdout.flush();
|
2024-12-10 01:34:53 -05:00
|
|
|
}
|
|
|
|
|
2025-02-03 17:49:38 -05:00
|
|
|
pub fn write_line(data: &mut Thread, _: usize, argument_range: Range<usize>) {
|
2025-02-17 08:01:10 -05:00
|
|
|
let current_frame = data.current_frame_mut();
|
2024-12-17 07:10:47 -05:00
|
|
|
let mut stdout = stdout().lock();
|
2024-12-10 01:34:53 -05:00
|
|
|
|
2024-12-17 07:10:47 -05:00
|
|
|
for register_index in argument_range {
|
2025-02-17 10:04:31 -05:00
|
|
|
let string = current_frame
|
|
|
|
.registers
|
|
|
|
.strings
|
|
|
|
.get(register_index)
|
|
|
|
.as_value();
|
2025-02-17 08:01:10 -05:00
|
|
|
let _ = stdout.write(string.as_bytes());
|
2024-12-10 01:34:53 -05:00
|
|
|
}
|
|
|
|
|
2025-02-03 17:49:38 -05:00
|
|
|
let _ = stdout.write(b"\n");
|
2025-01-08 23:02:08 -05:00
|
|
|
let _ = stdout.flush();
|
2024-12-10 01:34:53 -05:00
|
|
|
}
|