input function

This commit is contained in:
Ava Apples Affine 2023-03-22 22:53:29 -07:00
parent df6b5b5f06
commit 1ce5fd3454
Signed by: affine
GPG key ID: 3A4645B8CF806069
4 changed files with 35 additions and 5 deletions

View file

@ -17,6 +17,8 @@
use crate::segment::{Ctr, Seg};
use crate::sym::SymTable;
use std::io::Write;
use std::io;
pub const ECHO_DOCSTRING: &str =
"traverses any number of arguments. Prints their evaluated values on a new line for each.";
@ -150,3 +152,19 @@ pub fn split_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, String> {
Ok(Ctr::Seg(ret))
}
pub const INPUT_DOCSTRING: &str = "Takes one argument (string) and prints it.
Then prompts for user input.
User input is returned as a string";
pub fn input_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, String> {
if let Ctr::String(ref s) = *ast.car {
print!("{}", s);
let _= io::stdout().flush();
let mut input = String::new();
io::stdin().read_line(&mut input).expect("couldnt read user input");
Ok(Ctr::String(input.trim().to_string()))
} else {
Err("impossible: arg not string".to_string())
}
}