snix/users/tazjin/rlox/src/treewalk/interpreter/builtins.rs
Vincent Ambo 30a6fcccee refactor(tazjin/rlox): Move entrypoints into interpreters
Right now this introduces a simple mechanism to flip between the
interpreters.

Change-Id: I92ee920c53d76ab6b664ac671993a6d6426af61a
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2412
Reviewed-by: tazjin <mail@tazj.in>
Tested-by: BuildkiteCI
2021-01-17 21:17:54 +00:00

25 lines
644 B
Rust

use std::fmt;
use std::time::{SystemTime, UNIX_EPOCH};
use crate::errors::Error;
use crate::parser::Literal;
use crate::treewalk::interpreter::Value;
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)))
}
}