transform read_line to new func

This commit is contained in:
Mark 2024-07-03 13:39:15 +02:00
parent a9e5f9209c
commit 25c605d801

View File

@ -5,11 +5,13 @@ use std::{
use crate::{ use crate::{
data::{self, Data, Type}, data::{self, Data, Type},
errors::{CheckError, EColor},
program::{self, run::CheckInfo}, program::{self, run::CheckInfo},
}; };
use super::Config; use super::{
gen::{function::func, OneOrNone},
Config,
};
impl Config { impl Config {
/// `println: fn` prints to stdout and adds a newline to the end /// `println: fn` prints to stdout and adds a newline to the end
@ -19,102 +21,113 @@ impl Config {
/// `debug: fn` debug-prints any value /// `debug: fn` debug-prints any value
/// `read_line: fn` reads a line from stdin and returns it /// `read_line: fn` reads a line from stdin and returns it
pub fn with_stdio(self) -> Self { pub fn with_stdio(self) -> Self {
self.add_var( self
"read_line", // .add_var(
data::function::Function { // "read_line",
info: program::run::Info::neverused(), // data::function::Function {
info_check: Arc::new(Mutex::new(CheckInfo::neverused())), // info: program::run::Info::neverused(),
out: Ok(Arc::new(|a, _i| { // info_check: Arc::new(Mutex::new(CheckInfo::neverused())),
if a.is_zero_tuple() { // out: Ok(Arc::new(|a, _i| {
Ok(Type::newm(vec![ // if a.is_zero_tuple() {
Arc::new(data::tuple::TupleT(vec![Type::new(data::string::StringT)])), // Ok(Type::newm(vec![
Arc::new(data::tuple::TupleT(vec![])), // Arc::new(data::tuple::TupleT(vec![Type::new(data::string::StringT)])),
])) // Arc::new(data::tuple::TupleT(vec![])),
} else { // ]))
Err(CheckError::new().msg(vec![ // } else {
("expected (), got ".to_owned(), None), // Err(CheckError::new().msg(vec![
(a.to_string(), Some(EColor::FunctionArgument)), // ("expected (), got ".to_owned(), None),
])) // (a.to_string(), Some(EColor::FunctionArgument)),
} // ]))
})), // }
run: Arc::new(|_a, _i| { // })),
// run: Arc::new(|_a, _i| {
// Ok(if let Some(Ok(line)) = std::io::stdin().lines().next() {
// Data::one_tuple(Data::new(data::string::String(line)))
// } else {
// Data::empty_tuple()
// })
// }),
// inner_statements: None,
// },
// )
.add_var(
"read_line",
func(|_: (), _| {
Ok(if let Some(Ok(line)) = std::io::stdin().lines().next() { Ok(if let Some(Ok(line)) = std::io::stdin().lines().next() {
Data::one_tuple(Data::new(data::string::String(line))) OneOrNone(Some(line))
} else { } else {
Data::empty_tuple() OneOrNone(None)
}) })
}), }),
inner_statements: None, )
}, .add_var(
) "debug",
.add_var( data::function::Function {
"debug", info: program::run::Info::neverused(),
data::function::Function { info_check: Arc::new(Mutex::new(CheckInfo::neverused())),
info: program::run::Info::neverused(), out: Ok(Arc::new(|a, _i| Ok(a.clone()))),
info_check: Arc::new(Mutex::new(CheckInfo::neverused())), run: Arc::new(|a, _i| {
out: Ok(Arc::new(|a, _i| Ok(a.clone()))), let a2 = a.get();
run: Arc::new(|a, _i| { eprintln!("{} :: {}", a2.as_type(), a2);
let a2 = a.get(); drop(a2);
eprintln!("{} :: {}", a2.as_type(), a2); Ok(a)
drop(a2); }),
Ok(a) inner_statements: None,
}), },
inner_statements: None, )
}, .add_var(
) "eprint",
.add_var( data::function::Function {
"eprint", info: program::run::Info::neverused(),
data::function::Function { info_check: Arc::new(Mutex::new(CheckInfo::neverused())),
info: program::run::Info::neverused(), out: Ok(Arc::new(|_a, _i| Ok(Type::empty_tuple()))),
info_check: Arc::new(Mutex::new(CheckInfo::neverused())), run: Arc::new(|a, _i| {
out: Ok(Arc::new(|_a, _i| Ok(Type::empty_tuple()))), eprint!("{}", a.get());
run: Arc::new(|a, _i| { _ = std::io::stderr().lock().flush();
eprint!("{}", a.get()); Ok(Data::empty_tuple())
_ = std::io::stderr().lock().flush(); }),
Ok(Data::empty_tuple()) inner_statements: None,
}), },
inner_statements: None, )
}, .add_var(
) "eprintln",
.add_var( data::function::Function {
"eprintln", info: program::run::Info::neverused(),
data::function::Function { info_check: Arc::new(Mutex::new(CheckInfo::neverused())),
info: program::run::Info::neverused(), out: Ok(Arc::new(|_a, _i| Ok(Type::empty_tuple()))),
info_check: Arc::new(Mutex::new(CheckInfo::neverused())), run: Arc::new(|a, _i| {
out: Ok(Arc::new(|_a, _i| Ok(Type::empty_tuple()))), eprintln!("{}", a.get());
run: Arc::new(|a, _i| { Ok(Data::empty_tuple())
eprintln!("{}", a.get()); }),
Ok(Data::empty_tuple()) inner_statements: None,
}), },
inner_statements: None, )
}, .add_var(
) "print",
.add_var( data::function::Function {
"print", info: program::run::Info::neverused(),
data::function::Function { info_check: Arc::new(Mutex::new(CheckInfo::neverused())),
info: program::run::Info::neverused(), out: Ok(Arc::new(|_a, _i| Ok(Type::empty_tuple()))),
info_check: Arc::new(Mutex::new(CheckInfo::neverused())), run: Arc::new(|a, _i| {
out: Ok(Arc::new(|_a, _i| Ok(Type::empty_tuple()))), print!("{}", a.get());
run: Arc::new(|a, _i| { _ = std::io::stdout().lock().flush();
print!("{}", a.get()); Ok(Data::empty_tuple())
_ = std::io::stdout().lock().flush(); }),
Ok(Data::empty_tuple()) inner_statements: None,
}), },
inner_statements: None, )
}, .add_var(
) "println",
.add_var( data::function::Function {
"println", info: program::run::Info::neverused(),
data::function::Function { info_check: Arc::new(Mutex::new(CheckInfo::neverused())),
info: program::run::Info::neverused(), out: Ok(Arc::new(|_a, _i| Ok(Type::empty_tuple()))),
info_check: Arc::new(Mutex::new(CheckInfo::neverused())), run: Arc::new(|a, _i| {
out: Ok(Arc::new(|_a, _i| Ok(Type::empty_tuple()))), println!("{}", a.get());
run: Arc::new(|a, _i| { Ok(Data::empty_tuple())
println!("{}", a.get()); }),
Ok(Data::empty_tuple()) inner_statements: None,
}), },
inner_statements: None, )
},
)
} }
} }