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 <mail@tazj.in> Tested-by: BuildkiteCI
This commit is contained in:
parent
e93a2fc48f
commit
090c96eae9
2 changed files with 49 additions and 0 deletions
|
|
@ -5,8 +5,32 @@ use std::collections::HashMap;
|
|||
use std::rc::Rc;
|
||||
use std::sync::RwLock;
|
||||
|
||||
// Implementation of built-in functions.
|
||||
mod builtins;
|
||||
|
||||
// Tree-walk interpreter
|
||||
|
||||
// Representation of all callables, including builtins & user-defined
|
||||
// functions.
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum Callable {
|
||||
Builtin(&'static dyn builtins::Builtin),
|
||||
}
|
||||
|
||||
impl Callable {
|
||||
fn arity(&self) -> usize {
|
||||
match self {
|
||||
Callable::Builtin(builtin) => builtin.arity(),
|
||||
}
|
||||
}
|
||||
|
||||
fn call(&self, args: Vec<Value>) -> Result<Value, Error> {
|
||||
match self {
|
||||
Callable::Builtin(builtin) => builtin.call(args),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Representation of an in-language value.
|
||||
#[derive(Clone, Debug)]
|
||||
enum Value {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue