use cel::common::ast::CallExpr;
use ferricel_types::functions::RuntimeFunction;
use walrus::InstrSeqBuilder;
use crate::compiler::{
context::{CompilerContext, CompilerEnv},
expr::compile_expr,
helpers::compile_call_binary,
};
pub fn compile_string_function(
func_name: &str,
call_expr: &CallExpr,
body: &mut InstrSeqBuilder,
env: &CompilerEnv,
ctx: &CompilerContext,
module: &mut walrus::Module,
) -> Result<(), anyhow::Error> {
match func_name {
"size" => compile_size(call_expr, body, env, ctx, module),
"startsWith" => compile_call_binary(
call_expr,
"startsWith",
RuntimeFunction::StringStartsWith,
body,
env,
ctx,
module,
),
"endsWith" => compile_call_binary(
call_expr,
"endsWith",
RuntimeFunction::StringEndsWith,
body,
env,
ctx,
module,
),
"contains" => compile_call_binary(
call_expr,
"contains",
RuntimeFunction::StringContains,
body,
env,
ctx,
module,
),
"matches" => compile_call_binary(
call_expr,
"matches",
RuntimeFunction::StringMatches,
body,
env,
ctx,
module,
),
_ => anyhow::bail!("Unknown string function: {}", func_name),
}
}
fn compile_size(
call_expr: &CallExpr,
body: &mut InstrSeqBuilder,
env: &CompilerEnv,
ctx: &CompilerContext,
module: &mut walrus::Module,
) -> Result<(), anyhow::Error> {
match (call_expr.args.len(), &call_expr.target) {
(1, _) => {
compile_expr(&call_expr.args[0].expr, body, env, ctx, module)?;
}
(0, Some(target)) => {
compile_expr(&target.expr, body, env, ctx, module)?;
}
_ => anyhow::bail!("size() expects 1 argument"),
}
body.call(env.get(RuntimeFunction::ValueSize)); body.call(env.get(RuntimeFunction::CreateInt)); Ok(())
}