From 090c96eae998fd399975e85321189a136a7dbddd Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Thu, 14 Jan 2021 03:13:51 +0300 Subject: feat(tazjin/rlox): Scaffolding for builtin functions ... and adds an example builtin which returns the current epoch. The types introduced by this, especially in the interpreter module, are going to be used for user-defined functions, too. Change-Id: I0364a67241e94642cde08489ac711a340e30ebe8 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2381 Reviewed-by: tazjin Tested-by: BuildkiteCI --- users/tazjin/rlox/src/interpreter/builtins.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 users/tazjin/rlox/src/interpreter/builtins.rs (limited to 'users/tazjin/rlox/src/interpreter/builtins.rs') diff --git a/users/tazjin/rlox/src/interpreter/builtins.rs b/users/tazjin/rlox/src/interpreter/builtins.rs new file mode 100644 index 0000000000..6ed9f07c3f --- /dev/null +++ b/users/tazjin/rlox/src/interpreter/builtins.rs @@ -0,0 +1,25 @@ +use std::fmt; +use std::time::{SystemTime, UNIX_EPOCH}; + +use crate::errors::Error; +use crate::interpreter::Value; +use crate::parser::Literal; + +pub trait Builtin: fmt::Debug { + fn arity(&self) -> usize; + fn call(&self, args: Vec) -> Result; +} + +// 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) -> Result { + let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap(); + Ok(Value::Literal(Literal::Number(now.as_secs() as f64))) + } +} -- cgit 1.4.1