-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.rs
More file actions
64 lines (55 loc) · 1.84 KB
/
Copy pathfunction.rs
File metadata and controls
64 lines (55 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use crate::context::RuntimeContext;
use crate::environment::Environment;
use crate::error::RuntimeError;
use crate::value::RuntimeValue;
use std::cell::RefCell;
use std::rc::Rc;
/// Trait defining the calling contract for any callable object (AST, Native, or VM function).
pub trait Callable {
/// Returns the function or method name identifier.
fn name(&self) -> &str;
/// Returns the number of arguments expected by the callable.
fn arity(&self) -> usize;
/// Whether this callable accepts a particular argument count. Fixed-arity
/// callables retain the historical behaviour; user functions can widen
/// this range when they declare default parameters.
fn accepts_arity(&self, count: usize) -> bool {
count == self.arity()
}
/// Executes the function call using the given context and argument values.
fn call(
&self,
ctx: &mut RuntimeContext,
args: Vec<RuntimeValue>,
) -> Result<RuntimeValue, RuntimeError>;
}
/// Abstract representation of a function body to allow future bytecode/VM/LLVM extension.
#[derive(Debug, Clone)]
pub enum FunctionBody {
Ast(techscript_ast::Block),
Bytecode(Vec<u8>),
}
/// Dynamic representation of a user-defined function closure.
pub struct UserFunction {
pub name: String,
pub params: Vec<String>,
pub body: FunctionBody,
pub closure: Rc<RefCell<Environment>>,
}
impl Callable for UserFunction {
fn name(&self) -> &str {
&self.name
}
fn arity(&self) -> usize {
self.params.len()
}
fn call(
&self,
_ctx: &mut RuntimeContext,
_args: Vec<RuntimeValue>,
) -> Result<RuntimeValue, RuntimeError> {
// Crate is responsible ONLY for function definition infrastructure.
// Interpreter will intercept and traverse AST.
Ok(RuntimeValue::Null)
}
}