try some macro magic
This commit is contained in:
parent
7a173035a6
commit
c3cf09c517
1 changed files with 17 additions and 24 deletions
|
@ -263,32 +263,25 @@ impl Callable for CliArgs {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Helper macro to create a (String, Value) tuple for a native function.
|
||||||
|
macro_rules! native_fn {
|
||||||
|
($callable:expr) => {{
|
||||||
|
let callable_rc: Rc<dyn Callable> = Rc::new($callable);
|
||||||
|
(
|
||||||
|
callable_rc.name(),
|
||||||
|
Value::Callable((callable_rc.clone(), CallableType::Function)),
|
||||||
|
)
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
/// Return all native functions available to the Lox interpreter
|
/// Return all native functions available to the Lox interpreter
|
||||||
pub fn all(cli_args: Vec<String>) -> Vec<(String, Value)> {
|
pub fn all(cli_args: Vec<String>) -> Vec<(String, Value)> {
|
||||||
vec![
|
vec![
|
||||||
(
|
native_fn!(AsciiOut {}),
|
||||||
"asciiOut".into(),
|
native_fn!(Clock {}),
|
||||||
Value::Callable((Rc::new(AsciiOut {}), CallableType::Function)),
|
native_fn!(Out {}),
|
||||||
),
|
native_fn!(ReadFile {}),
|
||||||
(
|
native_fn!(PromptAscii {}),
|
||||||
"clock".into(),
|
native_fn!(CliArgs { cli_args }),
|
||||||
Value::Callable((Rc::new(Clock {}), CallableType::Function)),
|
|
||||||
),
|
|
||||||
(
|
|
||||||
"out".into(),
|
|
||||||
Value::Callable((Rc::new(Out {}), CallableType::Function)),
|
|
||||||
),
|
|
||||||
(
|
|
||||||
"read".into(),
|
|
||||||
Value::Callable((Rc::new(ReadFile {}), CallableType::Function)),
|
|
||||||
),
|
|
||||||
(
|
|
||||||
"promptAscii".into(),
|
|
||||||
Value::Callable((Rc::new(PromptAscii {}), CallableType::Function)),
|
|
||||||
),
|
|
||||||
(
|
|
||||||
"args".into(),
|
|
||||||
Value::Callable((Rc::new(CliArgs { cli_args }), CallableType::Function)),
|
|
||||||
),
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue