Skip to content

Commit 265148c

Browse files
committed
Thread/sleep, System/nanotime functions
1 parent ae3c659 commit 265148c

File tree

5 files changed

+82
-0
lines changed

5 files changed

+82
-0
lines changed

src/clojure_std.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub (crate) mod thread;
2+
pub (crate) mod time;

src/clojure_std/thread.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use crate::value::{ToValue, Value};
2+
use std::rc::Rc;
3+
use crate::ifn::IFn;
4+
use std::io::Read;
5+
use std::error::Error;
6+
use crate::error_message;
7+
use nom::lib::std::convert::TryFrom;
8+
use crate::type_tag::TypeTag;
9+
10+
use std::thread;
11+
use std::time;
12+
13+
/// provides a sleep function to sleep for given amount of ms
14+
#[derive(Debug, Clone)]
15+
pub struct SleepFn {}
16+
impl ToValue for SleepFn {
17+
fn to_value(&self) -> Value {
18+
Value::IFn(Rc::new(self.clone()))
19+
}
20+
}
21+
22+
impl IFn for SleepFn {
23+
fn invoke(&self, args: Vec<Rc<Value>>) -> Value {
24+
if args.len() == 1 {
25+
let arg = &**args.get(0).unwrap();
26+
match arg {
27+
Value::I32(i) => {
28+
std::thread::sleep(time::Duration::new(0, (*i as u32) * 1000000));
29+
return Value::Nil
30+
},
31+
_ => error_message::type_mismatch(TypeTag::I32, args.get(0).unwrap())
32+
}
33+
} else {
34+
error_message::wrong_arg_count(1, args.len());
35+
return Value::Nil
36+
}
37+
}
38+
}

src/clojure_std/time.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use crate::value::{ToValue, Value};
2+
use std::rc::Rc;
3+
use crate::ifn::IFn;
4+
use std::io::Read;
5+
use std::error::Error;
6+
use crate::error_message;
7+
use nom::lib::std::convert::TryFrom;
8+
use crate::type_tag::TypeTag;
9+
10+
use std::thread;
11+
use std::time::{SystemTime, UNIX_EPOCH};
12+
13+
/// provides a function to return current time in nanoseconds
14+
#[derive(Debug, Clone)]
15+
pub struct NanoTimeFn {}
16+
impl ToValue for NanoTimeFn {
17+
fn to_value(&self) -> Value {
18+
Value::IFn(Rc::new(self.clone()))
19+
}
20+
}
21+
22+
impl IFn for NanoTimeFn {
23+
fn invoke(&self, args: Vec<Rc<Value>>) -> Value {
24+
if args.len() == 0 {
25+
let ns = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_nanos();
26+
return Value::F64(ns as f64)
27+
} else {
28+
error_message::wrong_arg_count(0, args.len());
29+
return Value::Nil
30+
}
31+
}
32+
}

src/environment.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::namespace::{Namespace, Namespaces};
22
use crate::repl;
33
use crate::repl::Repl;
44
use crate::rust_core;
5+
use crate::clojure_std;
56
use crate::symbol::Symbol;
67
use crate::value::{ToValue, Value};
78

@@ -77,6 +78,9 @@ impl Environment {
7778
let concat_fn = rust_core::ConcatFn {};
7879
let print_string_fn = rust_core::PrintStringFn {};
7980
let assoc_fn = rust_core::AssocFn {};
81+
// clojure.std functions
82+
let thread_sleep_fn = clojure_std::thread::SleepFn {};
83+
let nanotime_fn = clojure_std::time::NanoTimeFn {};
8084
// Hardcoded fns
8185
let lexical_eval_fn = Value::LexicalEvalFn {};
8286
// Hardcoded macros
@@ -98,6 +102,11 @@ impl Environment {
98102
environment.insert(Symbol::intern("defmacro"), defmacro_macro.to_rc_value());
99103
environment.insert(Symbol::intern("eval"), eval_fn.to_rc_value());
100104

105+
// Thread namespace TODO / instead of _
106+
environment.insert(Symbol::intern("Thread_sleep"), thread_sleep_fn.to_rc_value());
107+
108+
environment.insert(Symbol::intern("System_nanotime"), nanotime_fn.to_rc_value());
109+
101110
environment.insert(Symbol::intern("+"), add_fn.to_rc_value());
102111
environment.insert(Symbol::intern("let"), let_macro.to_rc_value());
103112
environment.insert(Symbol::intern("str"), str_fn.to_rc_value());

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ mod persistent_vector;
1313
mod reader;
1414
mod repl;
1515
mod rust_core;
16+
mod clojure_std;
1617
mod symbol;
1718
mod keyword;
1819
mod type_tag;

0 commit comments

Comments
 (0)