Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions src/clojure_std/thread.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
use crate::value::{ToValue, Value};
use std::rc::Rc;
use crate::ifn::IFn;
use std::io::Read;
use std::error::Error;
use crate::error_message;
use nom::lib::std::convert::TryFrom;
use crate::ifn::IFn;
use crate::type_tag::TypeTag;
use crate::value::{ToValue, Value};
use std::rc::Rc;

use std::thread;
use std::time;

/// provides a sleep function to sleep for given amount of ms
Expand All @@ -25,14 +21,14 @@ impl IFn for SleepFn {
let arg = &**args.get(0).unwrap();
match arg {
Value::I32(i) => {
std::thread::sleep(time::Duration::new(0, (*i as u32) * 1000000));
return Value::Nil
},
_ => error_message::type_mismatch(TypeTag::I32, args.get(0).unwrap())
std::thread::sleep(time::Duration::new(0, (*i as u32) * 100_0000));
Value::Nil
}
_ => error_message::type_mismatch(TypeTag::I32, args.get(0).unwrap()),
}
} else {
error_message::wrong_arg_count(1, args.len());
return Value::Nil
Value::Nil
}
}
}
}
22 changes: 10 additions & 12 deletions src/clojure_std/time.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
use crate::error_message;
use crate::ifn::IFn;
use crate::value::{ToValue, Value};
use std::rc::Rc;
use crate::ifn::IFn;
use std::io::Read;
use std::error::Error;
use crate::error_message;
use nom::lib::std::convert::TryFrom;
use crate::type_tag::TypeTag;

use std::thread;
use std::time::{SystemTime, UNIX_EPOCH};

/// provides a function to return current time in nanoseconds
Expand All @@ -21,12 +16,15 @@ impl ToValue for NanoTimeFn {

impl IFn for NanoTimeFn {
fn invoke(&self, args: Vec<Rc<Value>>) -> Value {
if args.len() == 0 {
let ns = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_nanos();
return Value::F64(ns as f64)
if args.is_empty() {
let ns = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_nanos();
Value::F64(ns as f64)
} else {
error_message::wrong_arg_count(0, args.len());
return Value::Nil
Value::Nil
}
}
}
}
12 changes: 7 additions & 5 deletions src/environment.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::clojure_std;
use crate::namespace::{Namespace, Namespaces};
use crate::repl;
use crate::repl::Repl;
use crate::rust_core;
use crate::clojure_std;
use crate::symbol::Symbol;
use crate::value::{ToValue, Value};

Expand Down Expand Up @@ -118,12 +117,15 @@ impl Environment {
environment.insert(Symbol::intern("eval"), eval_fn.to_rc_value());

// Thread namespace TODO / instead of _
environment.insert(Symbol::intern("Thread_sleep"), thread_sleep_fn.to_rc_value());
environment.insert(
Symbol::intern("Thread_sleep"),
thread_sleep_fn.to_rc_value(),
);

environment.insert(Symbol::intern("System_nanotime"), nanotime_fn.to_rc_value());

// core.clj wraps calls to the rust implementations
// @TODO add this to clojure.rs.core namespace as clojure.rs.core/slurp
// @TODO add this to clojure.rs.core namespace as clojure.rs.core/slurp
environment.insert(Symbol::intern("rust-slurp"), slurp_fn.to_rc_value());

environment.insert(Symbol::intern("+"), add_fn.to_rc_value());
Expand All @@ -141,7 +143,7 @@ impl Environment {
lexical_eval_fn.to_rc_value(),
);
environment.insert(Symbol::intern("nth"), nth_fn.to_rc_value());
environment.insert(Symbol::intern("assoc"), assoc_fn.to_rc_value());
environment.insert(Symbol::intern("assoc"), assoc_fn.to_rc_value());
environment.insert(Symbol::intern("concat"), concat_fn.to_rc_value());
environment.insert(
Symbol::intern("print-string"),
Expand Down
21 changes: 9 additions & 12 deletions src/error_message.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
use crate::type_tag::TypeTag;
use crate::value::Value;
use nom::Err;
use nom::error::ErrorKind;
use std::error::Error;

pub fn type_mismatch(expected: TypeTag, got: &Value) -> Value {
Value::Condition(format!(
"Type mismatch; Expected instance of {}, Recieved type {}",
expected,
got
expected, got
))
}

pub fn wrong_arg_count(expected: usize, got: usize) -> Value {
Value::Condition(format!(
"Wrong number of arguments given to function (Given: {}, Expected: {})",
got,
expected
got, expected
))
}

pub fn wrong_varg_count(expected: &[usize], got: usize) -> Value {
Value::Condition(format!(
"Wrong number of arguments given to function (Given: {}, Expected: {:?})",
got,
expected
got, expected
))
}

pub fn zero_arg_count(got: usize) -> Value {
Value::Condition(format!(
"Wrong number of arguments given to function (Given: {})", got))
"Wrong number of arguments given to function (Given: {})",
got
))
}

pub fn index_out_of_bounds(ind: usize, count: usize) -> Value {
Expand All @@ -44,6 +41,6 @@ pub fn index_cannot_be_negative(ind: usize) -> Value {
Value::Condition(format!("Index cannot be negative; Index ({})", ind))
}

pub fn generic_err(error: Box<Error>) -> Value {
Value::Condition(format!("{}", error.to_string()))
}
pub fn generic_err(error: Box<dyn Error>) -> Value {
Value::Condition(error.to_string())
}
1 change: 0 additions & 1 deletion src/persistent_list_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ mod tests {

#[test]
fn test_persistent_list_map() {
let empty = PersistentListMap::Empty;
let map1 = vec![
MapEntry {
key: Symbol::intern("a").to_rc_value(),
Expand Down
Loading