nexus_lib/evaluator/
builtins.rs1use std::io;
2
3use crate::lexer::tokens::Literal;
4
5use super::objects::Object;
6
7#[derive(Debug, Clone)]
8pub enum BuiltinFunc {
9 Print(Print),
10 Input(Input),
11}
12
13impl BuiltinFunc {
14 pub fn get_ret_val(&self) -> Option<Object> {
15 match self {
16 BuiltinFunc::Print(_) => None,
17 BuiltinFunc::Input(input) => Some(Object::Lit(input.ret_val.clone())),
18 }
19 }
20}
21
22#[derive(Debug, Clone)]
23pub struct Input {
24 ret_val: Literal,
28}
29
30impl Input {
31 pub fn input(print_val: Option<String>) -> Self {
32 let mut input = String::new();
33
34 if let Some(val) = print_val {
35 println!("{}", val)
36 }
37
38 match io::stdin().read_line(&mut input) {
40 Ok(_) => (),
41 Err(error) => eprintln!("Error reading input: {}", error),
42 }
43 Self {
44 ret_val: Literal::Str(input),
45 }
46 }
47}
48
49#[derive(Debug, Clone)]
50pub struct Print;
51
52impl Print {
53 pub fn print_val(val: Option<Object>) -> Self {
54 println!(
55 "{}",
56 match val {
57 Some(val) => val.to_string(),
58 None => "".into(),
59 }
60 );
61 Self {}
62 }
63}