try some macro magic

This commit is contained in:
Sebastian Hugentobler 2025-05-28 22:05:30 +02:00
parent 7a173035a6
commit c3cf09c517
Signed by: shu
SSH key fingerprint: SHA256:ppcx6MlixdNZd5EUM1nkHOKoyQYoJwzuQKXM6J/t66M

View file

@ -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
pub fn all(cli_args: Vec<String>) -> Vec<(String, Value)> {
vec![
(
"asciiOut".into(),
Value::Callable((Rc::new(AsciiOut {}), CallableType::Function)),
),
(
"clock".into(),
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)),
),
native_fn!(AsciiOut {}),
native_fn!(Clock {}),
native_fn!(Out {}),
native_fn!(ReadFile {}),
native_fn!(PromptAscii {}),
native_fn!(CliArgs { cli_args }),
]
}