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