Skip to content

Commit

Permalink
feat(std:util): add input function to the util lib
Browse files Browse the repository at this point in the history
  • Loading branch information
AlenVelocity committed Mar 8, 2022
1 parent 21e14ff commit 9036dfe
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/std_library/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use super::Res;
pub fn add_globals() -> Res {
let mut globals = HashMap::new();
globals.insert(String::from("length"), Object::Inbuilt(length));
globals.insert(String::from("input"), Object::Inbuilt(input));
return Res { globals, raw: None }
}

Expand All @@ -27,4 +28,17 @@ pub fn length(args: Vec<Object>) -> Object {
Object::Array(a) => Object::Number(a.len() as f64),
o => Object::Error(format!("Argument must be a string or array. Got {}", o)),
}
}

pub fn input(args: Vec<Object>) -> Object {
println!("{}", &args[0]);
match &args[0] {
Object::String(s) => {
print!("{}", s);
}
_ => ()
}
let mut input = String::new();
std::io::stdin().read_line(&mut input).expect("Failed to read line");
return Object::String(input);
}

0 comments on commit 9036dfe

Please sign in to comment.