Add thread.sleep to built-ins
This commit is contained in:
parent
690e248df6
commit
fd0204fefa
@ -5,6 +5,8 @@ use std::{
|
|||||||
io::stdin,
|
io::stdin,
|
||||||
ops::Range,
|
ops::Range,
|
||||||
sync::{Arc, OnceLock},
|
sync::{Arc, OnceLock},
|
||||||
|
thread,
|
||||||
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
use rand::{thread_rng, Rng};
|
use rand::{thread_rng, Rng};
|
||||||
@ -334,12 +336,14 @@ static INT_PARSE: OnceLock<Value> = OnceLock::new();
|
|||||||
static INT_RANDOM_RANGE: OnceLock<Value> = OnceLock::new();
|
static INT_RANDOM_RANGE: OnceLock<Value> = OnceLock::new();
|
||||||
static READ_LINE: OnceLock<Value> = OnceLock::new();
|
static READ_LINE: OnceLock<Value> = OnceLock::new();
|
||||||
static WRITE_LINE: OnceLock<Value> = OnceLock::new();
|
static WRITE_LINE: OnceLock<Value> = OnceLock::new();
|
||||||
|
static SLEEP: OnceLock<Value> = OnceLock::new();
|
||||||
|
|
||||||
pub const BUILT_IN_FUNCTIONS: [BuiltInFunction; 4] = [
|
pub const BUILT_IN_FUNCTIONS: [BuiltInFunction; 5] = [
|
||||||
BuiltInFunction::IntParse,
|
BuiltInFunction::IntParse,
|
||||||
BuiltInFunction::IntRandomRange,
|
BuiltInFunction::IntRandomRange,
|
||||||
BuiltInFunction::ReadLine,
|
BuiltInFunction::ReadLine,
|
||||||
BuiltInFunction::WriteLine,
|
BuiltInFunction::WriteLine,
|
||||||
|
BuiltInFunction::Sleep,
|
||||||
];
|
];
|
||||||
|
|
||||||
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
|
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
|
||||||
@ -348,6 +352,7 @@ pub enum BuiltInFunction {
|
|||||||
IntRandomRange,
|
IntRandomRange,
|
||||||
ReadLine,
|
ReadLine,
|
||||||
WriteLine,
|
WriteLine,
|
||||||
|
Sleep,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BuiltInFunction {
|
impl BuiltInFunction {
|
||||||
@ -357,6 +362,7 @@ impl BuiltInFunction {
|
|||||||
BuiltInFunction::IntRandomRange => "random_range",
|
BuiltInFunction::IntRandomRange => "random_range",
|
||||||
BuiltInFunction::ReadLine => "read_line",
|
BuiltInFunction::ReadLine => "read_line",
|
||||||
BuiltInFunction::WriteLine => "write_line",
|
BuiltInFunction::WriteLine => "write_line",
|
||||||
|
BuiltInFunction::Sleep => "sleep",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -373,6 +379,9 @@ impl BuiltInFunction {
|
|||||||
BuiltInFunction::WriteLine => {
|
BuiltInFunction::WriteLine => {
|
||||||
WRITE_LINE.get_or_init(|| Value::built_in_function(BuiltInFunction::WriteLine))
|
WRITE_LINE.get_or_init(|| Value::built_in_function(BuiltInFunction::WriteLine))
|
||||||
}
|
}
|
||||||
|
BuiltInFunction::Sleep => {
|
||||||
|
SLEEP.get_or_init(|| Value::built_in_function(BuiltInFunction::Sleep))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
.clone()
|
.clone()
|
||||||
}
|
}
|
||||||
@ -395,6 +404,10 @@ impl BuiltInFunction {
|
|||||||
parameter_types: vec![Type::Any],
|
parameter_types: vec![Type::Any],
|
||||||
return_type: Box::new(Type::None),
|
return_type: Box::new(Type::None),
|
||||||
},
|
},
|
||||||
|
BuiltInFunction::Sleep => Type::Function {
|
||||||
|
parameter_types: vec![Type::Integer],
|
||||||
|
return_type: Box::new(Type::None),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -447,6 +460,13 @@ impl BuiltInFunction {
|
|||||||
BuiltInFunction::WriteLine => {
|
BuiltInFunction::WriteLine => {
|
||||||
println!("{}", arguments[0]);
|
println!("{}", arguments[0]);
|
||||||
|
|
||||||
|
Ok(Action::None)
|
||||||
|
}
|
||||||
|
BuiltInFunction::Sleep => {
|
||||||
|
if let ValueInner::Integer(milliseconds) = arguments[0].inner().as_ref() {
|
||||||
|
thread::sleep(Duration::from_millis(*milliseconds as u64));
|
||||||
|
}
|
||||||
|
|
||||||
Ok(Action::None)
|
Ok(Action::None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -460,31 +480,36 @@ impl Display for BuiltInFunction {
|
|||||||
BuiltInFunction::IntRandomRange => write!(f, "(input: range) : int {{ *MAGIC* }}"),
|
BuiltInFunction::IntRandomRange => write!(f, "(input: range) : int {{ *MAGIC* }}"),
|
||||||
BuiltInFunction::ReadLine => write!(f, "() : str {{ *MAGIC* }}"),
|
BuiltInFunction::ReadLine => write!(f, "() : str {{ *MAGIC* }}"),
|
||||||
BuiltInFunction::WriteLine => write!(f, "(to_output : any) : none {{ *MAGIC* }}"),
|
BuiltInFunction::WriteLine => write!(f, "(to_output : any) : none {{ *MAGIC* }}"),
|
||||||
|
BuiltInFunction::Sleep => write!(f, "(milliseconds : int) : none {{ *MAGIC* }}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static INT: OnceLock<Value> = OnceLock::new();
|
static INT: OnceLock<Value> = OnceLock::new();
|
||||||
static IO: OnceLock<Value> = OnceLock::new();
|
static IO: OnceLock<Value> = OnceLock::new();
|
||||||
|
static THREAD: OnceLock<Value> = OnceLock::new();
|
||||||
|
|
||||||
pub const BUILT_IN_MODULES: [BuiltInModule; 2] = [BuiltInModule::Integer, BuiltInModule::Io];
|
pub const BUILT_IN_MODULES: [BuiltInModule; 3] =
|
||||||
|
[BuiltInModule::Int, BuiltInModule::Io, BuiltInModule::Thread];
|
||||||
|
|
||||||
pub enum BuiltInModule {
|
pub enum BuiltInModule {
|
||||||
Integer,
|
Int,
|
||||||
Io,
|
Io,
|
||||||
|
Thread,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BuiltInModule {
|
impl BuiltInModule {
|
||||||
pub fn name(&self) -> &'static str {
|
pub fn name(&self) -> &'static str {
|
||||||
match self {
|
match self {
|
||||||
BuiltInModule::Integer => "int",
|
BuiltInModule::Int => "int",
|
||||||
BuiltInModule::Io => "io",
|
BuiltInModule::Io => "io",
|
||||||
|
BuiltInModule::Thread => "thread",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn value(self) -> Value {
|
pub fn value(self) -> Value {
|
||||||
match self {
|
match self {
|
||||||
BuiltInModule::Integer => {
|
BuiltInModule::Int => {
|
||||||
let mut properties = BTreeMap::new();
|
let mut properties = BTreeMap::new();
|
||||||
|
|
||||||
properties.insert(
|
properties.insert(
|
||||||
@ -512,6 +537,16 @@ impl BuiltInModule {
|
|||||||
|
|
||||||
IO.get_or_init(|| Value::map(properties)).clone()
|
IO.get_or_init(|| Value::map(properties)).clone()
|
||||||
}
|
}
|
||||||
|
BuiltInModule::Thread => {
|
||||||
|
let mut properties = BTreeMap::new();
|
||||||
|
|
||||||
|
properties.insert(
|
||||||
|
Identifier::new("sleep"),
|
||||||
|
Value::built_in_function(BuiltInFunction::Sleep),
|
||||||
|
);
|
||||||
|
|
||||||
|
THREAD.get_or_init(|| Value::map(properties)).clone()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user