use endbasic_core::*;
use std::borrow::Cow;
use std::rc::Rc;
pub(super) struct MaxDoublesFunction {
metadata: Rc<CallableMetadata>,
}
impl MaxDoublesFunction {
pub(super) fn new() -> Rc<Self> {
Rc::from(Self {
metadata: CallableMetadataBuilder::new("MAX_DOUBLES")
.with_return_type(ExprType::Double)
.with_syntax(&[(
&[],
Some(&RepeatedSyntax {
name: Cow::Borrowed("expr"),
type_syn: RepeatedTypeSyntax::TypedValue(ExprType::Double),
sep: ArgSepSyntax::Exactly(ArgSep::Long),
require_one: true,
allow_missing: false,
}),
)])
.test_build(),
})
}
}
impl Callable for MaxDoublesFunction {
fn metadata(&self) -> Rc<CallableMetadata> {
self.metadata.clone()
}
fn exec(&self, scope: Scope<'_>) -> CallResult<()> {
let mut max = f64::MIN;
let num_singular: usize = 0;
let mut reg = num_singular;
while reg < scope.nargs() {
let n = scope.get_double(reg as u8);
if n > max {
max = n;
}
reg += 1;
}
scope.return_double(max)
}
}