about summary refs log tree commit diff
path: root/users/tazjin/rlox/src/treewalk/interpreter/builtins.rs
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2021-01-17T09·13+0300
committertazjin <mail@tazj.in>2021-01-17T09·34+0000
commitf8b3e2a100fdb28cad24948703439d2964c31580 (patch)
treefdd7b13f4a359532f6acaa183d78e7d777f5c32d /users/tazjin/rlox/src/treewalk/interpreter/builtins.rs
parent052f8976bb3273d16fb0e1c4643de5abcaf0f135 (diff)
refactor(tazjin/rlox): Move treewalk interpreter into subdirectory r/2120
Change-Id: I9163f75db5a1ff75e1b1f81bad78fd9d8ddb104a
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2409
Reviewed-by: tazjin <mail@tazj.in>
Tested-by: BuildkiteCI
Diffstat (limited to 'users/tazjin/rlox/src/treewalk/interpreter/builtins.rs')
-rw-r--r--users/tazjin/rlox/src/treewalk/interpreter/builtins.rs25
1 files changed, 25 insertions, 0 deletions
diff --git a/users/tazjin/rlox/src/treewalk/interpreter/builtins.rs b/users/tazjin/rlox/src/treewalk/interpreter/builtins.rs
new file mode 100644
index 000000000000..614f30ff3b23
--- /dev/null
+++ b/users/tazjin/rlox/src/treewalk/interpreter/builtins.rs
@@ -0,0 +1,25 @@
+use std::fmt;
+use std::time::{SystemTime, UNIX_EPOCH};
+
+use crate::errors::Error;
+use crate::treewalk::interpreter::Value;
+use crate::parser::Literal;
+
+pub trait Builtin: fmt::Debug {
+    fn arity(&self) -> usize;
+    fn call(&self, args: Vec<Value>) -> Result<Value, Error>;
+}
+
+// Builtin to return the current timestamp.
+#[derive(Debug)]
+pub struct Clock {}
+impl Builtin for Clock {
+    fn arity(&self) -> usize {
+        0
+    }
+
+    fn call(&self, _args: Vec<Value>) -> Result<Value, Error> {
+        let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
+        Ok(Value::Literal(Literal::Number(now.as_secs() as f64)))
+    }
+}