1
0

70 lines
1.6 KiB
Rust
Raw Normal View History

use std::io::{stdin, stdout, Write};
use smallvec::SmallVec;
2024-12-17 03:22:44 -05:00
use crate::{vm::Record, ConcreteValue, NativeFunctionError, Value};
2024-12-17 03:22:44 -05:00
pub fn read_line(
record: &mut Record,
_: SmallVec<[&Value; 4]>,
) -> Result<Option<Value>, NativeFunctionError> {
let mut buffer = String::new();
match stdin().read_line(&mut buffer) {
Ok(_) => {
let length = buffer.len();
buffer.truncate(length.saturating_sub(1));
Ok(Some(Value::Concrete(ConcreteValue::string(buffer))))
}
Err(error) => Err(NativeFunctionError::Io {
error: error.kind(),
}),
}
}
pub fn write(
2024-12-17 03:22:44 -05:00
record: &mut Record,
2024-12-14 08:49:02 -05:00
arguments: SmallVec<[&Value; 4]>,
) -> Result<Option<Value>, NativeFunctionError> {
let mut stdout = stdout();
for argument in arguments {
2024-12-17 03:22:44 -05:00
let string = argument.display(record);
stdout
.write_all(string.as_bytes())
.map_err(|io_error| NativeFunctionError::Io {
error: io_error.kind(),
})?;
}
Ok(None)
}
pub fn write_line(
2024-12-17 03:22:44 -05:00
record: &mut Record,
2024-12-14 08:49:02 -05:00
arguments: SmallVec<[&Value; 4]>,
) -> Result<Option<Value>, NativeFunctionError> {
let mut stdout = stdout();
for argument in arguments {
2024-12-17 03:22:44 -05:00
let string = argument.display(record);
stdout
.write_all(string.as_bytes())
.map_err(|io_error| NativeFunctionError::Io {
error: io_error.kind(),
})?;
}
stdout
.write(b"\n")
.map_err(|io_error| NativeFunctionError::Io {
error: io_error.kind(),
})?;
Ok(None)
}