use crate::plan::CustomFieldAccess;
use crate::plan::{
BoolExpr, CaptureArg, ConstantFloatFunctionInstantiation, FloatExpr, FloatFunctionLocalId,
FloatFunctionReference, FunctionFunctionExpr, FunctionInstantiation, FunctionListExpr,
FunctionType, IntExpr, PanicExpr, Step, StringExpr, TupleExpr,
};
use ecow::EcoString;
use num_bigint::BigInt;
#[derive(Debug, Clone, PartialEq)]
pub struct FloatFunctionExpr {
type_: FunctionType,
kind: FloatFunctionExprKind,
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum FloatFunctionExprKind {
Constant(ConstantFloatFunctionInstantiation),
Reference(FloatFunctionReference),
Closure {
function: FunctionInstantiation,
captures: Vec<CaptureArg>,
},
LocalGet {
local: FloatFunctionLocalId,
name: EcoString,
},
Call {
function: FunctionInstantiation,
args: Vec<crate::plan::CallArg>,
type_: FunctionType,
site: crate::plan::HostCallSite,
},
FunctionCall {
function: Box<FunctionFunctionExpr>,
args: Vec<crate::plan::CallArg>,
type_: FunctionType,
site: crate::plan::HostCallSite,
},
TupleIndex {
tuple: Box<TupleExpr>,
index: usize,
type_: FunctionType,
},
CustomField(CustomFieldAccess),
ListIndex {
list: Box<FunctionListExpr>,
index: usize,
type_: FunctionType,
},
Panic(PanicExpr),
BoolCase {
subject: Box<BoolExpr>,
true_: Box<FloatFunctionExpr>,
false_: Box<FloatFunctionExpr>,
},
IntCase {
subject: Box<IntExpr>,
clauses: Vec<(BigInt, FloatFunctionExpr)>,
fallback: Box<FloatFunctionExpr>,
},
StringCase {
subject: Box<StringExpr>,
clauses: Vec<(EcoString, FloatFunctionExpr)>,
fallback: Box<FloatFunctionExpr>,
},
FloatCase {
subject: Box<FloatExpr>,
clauses: Vec<(f64, FloatFunctionExpr)>,
fallback: Box<FloatFunctionExpr>,
},
Block {
steps: Vec<Step>,
return_: Box<FloatFunctionExpr>,
},
}
impl FloatFunctionExpr {
pub(crate) fn constant(value: ConstantFloatFunctionInstantiation, type_: FunctionType) -> Self {
Self {
type_,
kind: FloatFunctionExprKind::Constant(value),
}
}
pub(crate) fn reference(value: FloatFunctionReference) -> Self {
let type_ = value.instantiation().shape().type_();
Self {
type_,
kind: FloatFunctionExprKind::Reference(value),
}
}
pub(crate) fn closure(
function: FunctionInstantiation,
captures: Vec<CaptureArg>,
type_: FunctionType,
) -> Self {
Self {
type_,
kind: FloatFunctionExprKind::Closure { function, captures },
}
}
pub(crate) fn local_get(
local: FloatFunctionLocalId,
name: EcoString,
type_: FunctionType,
) -> Self {
Self {
type_,
kind: FloatFunctionExprKind::LocalGet { local, name },
}
}
#[cfg(test)]
pub(crate) fn call(
function: FunctionInstantiation,
args: Vec<crate::plan::CallArg>,
type_: FunctionType,
) -> Self {
Self::call_at(function, args, type_, crate::plan::HostCallSite::unknown())
}
pub(crate) fn call_at(
function: FunctionInstantiation,
args: Vec<crate::plan::CallArg>,
type_: FunctionType,
site: crate::plan::HostCallSite,
) -> Self {
Self {
type_: type_.clone(),
kind: FloatFunctionExprKind::Call {
function,
args,
type_,
site,
},
}
}
#[cfg(test)]
pub(crate) fn function_call(
function: FunctionFunctionExpr,
args: Vec<crate::plan::CallArg>,
type_: FunctionType,
) -> Self {
Self::function_call_at(function, args, type_, crate::plan::HostCallSite::unknown())
}
pub(crate) fn function_call_at(
function: FunctionFunctionExpr,
args: Vec<crate::plan::CallArg>,
type_: FunctionType,
site: crate::plan::HostCallSite,
) -> Self {
Self {
type_: type_.clone(),
kind: FloatFunctionExprKind::FunctionCall {
function: Box::new(function),
args,
type_,
site,
},
}
}
pub(crate) fn tuple_index(tuple: TupleExpr, index: usize, type_: FunctionType) -> Self {
Self {
type_: type_.clone(),
kind: FloatFunctionExprKind::TupleIndex {
tuple: Box::new(tuple),
index,
type_,
},
}
}
pub(crate) fn custom_field(access: CustomFieldAccess, type_: FunctionType) -> Self {
Self {
type_,
kind: FloatFunctionExprKind::CustomField(access),
}
}
pub(crate) fn list_index(
list: impl Into<FunctionListExpr>,
index: usize,
type_: FunctionType,
) -> Self {
Self {
type_: type_.clone(),
kind: FloatFunctionExprKind::ListIndex {
list: Box::new(list.into()),
index,
type_,
},
}
}
pub(crate) fn panic(panic: PanicExpr, type_: FunctionType) -> Self {
Self {
type_,
kind: FloatFunctionExprKind::Panic(panic),
}
}
pub(crate) fn bool_case(
subject: BoolExpr,
true_: FloatFunctionExpr,
false_: FloatFunctionExpr,
) -> Self {
Self {
type_: true_.type_.clone(),
kind: FloatFunctionExprKind::BoolCase {
subject: Box::new(subject),
true_: Box::new(true_),
false_: Box::new(false_),
},
}
}
pub(crate) fn int_case(
subject: IntExpr,
clauses: Vec<(BigInt, FloatFunctionExpr)>,
fallback: FloatFunctionExpr,
) -> Self {
Self {
type_: fallback.type_.clone(),
kind: FloatFunctionExprKind::IntCase {
subject: Box::new(subject),
clauses,
fallback: Box::new(fallback),
},
}
}
pub(crate) fn string_case(
subject: StringExpr,
clauses: Vec<(EcoString, FloatFunctionExpr)>,
fallback: FloatFunctionExpr,
) -> Self {
Self {
type_: fallback.type_.clone(),
kind: FloatFunctionExprKind::StringCase {
subject: Box::new(subject),
clauses,
fallback: Box::new(fallback),
},
}
}
pub(crate) fn float_case(
subject: FloatExpr,
clauses: Vec<(f64, FloatFunctionExpr)>,
fallback: FloatFunctionExpr,
) -> Self {
Self {
type_: fallback.type_.clone(),
kind: FloatFunctionExprKind::FloatCase {
subject: Box::new(subject),
clauses,
fallback: Box::new(fallback),
},
}
}
pub(crate) fn block(steps: Vec<Step>, return_: FloatFunctionExpr) -> Self {
Self {
type_: return_.type_.clone(),
kind: FloatFunctionExprKind::Block {
steps,
return_: Box::new(return_),
},
}
}
pub fn type_(&self) -> &FunctionType {
&self.type_
}
pub(crate) fn kind(&self) -> &FloatFunctionExprKind {
&self.kind
}
}
#[cfg(test)]
mod tests {
use super::{FloatFunctionExpr, FloatFunctionExprKind};
use crate::plan::{
BoolExpr, Expr, FloatExpr, FloatFunctionLocalId, FloatFunctionReference,
FunctionFunctionExpr, FunctionFunctionReference, FunctionInstantiation, FunctionShape,
FunctionType, IntExpr, Step, StringExpr, ValueShape, ValueType,
monomorphic_function_instantiation,
};
#[test]
fn float_function_expr_kind_accessors() {
assert_eq!(
float_function_type(),
FunctionType::new(vec![ValueType::Float], ValueType::Float),
);
assert_eq!(
float_function_value().kind(),
&FloatFunctionExprKind::Reference(
FloatFunctionReference::new(function_instantiation())
),
);
assert_eq!(
FloatFunctionExpr::closure(
function_instantiation(),
Vec::new(),
float_function_type(),
)
.kind(),
&FloatFunctionExprKind::Closure {
function: function_instantiation(),
captures: Vec::new(),
},
);
assert_eq!(
FloatFunctionExpr::local_get(
FloatFunctionLocalId(0),
"f".into(),
float_function_type(),
)
.kind(),
&FloatFunctionExprKind::LocalGet {
local: FloatFunctionLocalId(0),
name: "f".into(),
},
);
assert_eq!(
FloatFunctionExpr::call(
function_returning_function_instantiation(),
Vec::new(),
float_function_type()
)
.kind(),
&FloatFunctionExprKind::Call {
function: function_returning_function_instantiation(),
args: Vec::new(),
type_: float_function_type(),
site: crate::plan::HostCallSite::unknown(),
},
);
assert_eq!(
FloatFunctionExpr::function_call(
function_function_value(),
Vec::new(),
float_function_type(),
)
.kind(),
&FloatFunctionExprKind::FunctionCall {
function: Box::new(function_function_value()),
args: Vec::new(),
type_: float_function_type(),
site: crate::plan::HostCallSite::unknown(),
},
);
assert_eq!(
FloatFunctionExpr::tuple_index(tuple_expr(), 0, float_function_type()).kind(),
&FloatFunctionExprKind::TupleIndex {
tuple: Box::new(tuple_expr()),
index: 0,
type_: float_function_type(),
},
);
assert_eq!(
FloatFunctionExpr::bool_case(
BoolExpr::value(true),
float_function_value(),
float_function_value(),
)
.kind(),
&FloatFunctionExprKind::BoolCase {
subject: Box::new(BoolExpr::value(true)),
true_: Box::new(float_function_value()),
false_: Box::new(float_function_value()),
},
);
assert_eq!(
FloatFunctionExpr::int_case(
IntExpr::value(1.into()),
vec![(1.into(), float_function_value())],
float_function_value(),
)
.kind(),
&FloatFunctionExprKind::IntCase {
subject: Box::new(IntExpr::value(1.into())),
clauses: vec![(1.into(), float_function_value())],
fallback: Box::new(float_function_value()),
},
);
assert_eq!(
FloatFunctionExpr::string_case(
StringExpr::value("one".into()),
vec![("one".into(), float_function_value())],
float_function_value(),
)
.kind(),
&FloatFunctionExprKind::StringCase {
subject: Box::new(StringExpr::value("one".into())),
clauses: vec![("one".into(), float_function_value())],
fallback: Box::new(float_function_value()),
},
);
assert_eq!(
FloatFunctionExpr::float_case(
FloatExpr::value(1.0),
vec![(1.0, float_function_value())],
float_function_value(),
)
.kind(),
&FloatFunctionExprKind::FloatCase {
subject: Box::new(FloatExpr::value(1.0)),
clauses: vec![(1.0, float_function_value())],
fallback: Box::new(float_function_value()),
},
);
assert_eq!(
FloatFunctionExpr::block(
vec![Step::evaluate(Expr::float(FloatExpr::value(1.0)))],
float_function_value(),
)
.kind(),
&FloatFunctionExprKind::Block {
steps: vec![Step::evaluate(Expr::float(FloatExpr::value(1.0)))],
return_: Box::new(float_function_value()),
},
);
}
#[test]
fn float_function_expr_type() {
assert_eq!(float_function_value().type_(), &float_function_type());
assert_eq!(
FloatFunctionExpr::bool_case(
BoolExpr::value(true),
float_function_value(),
float_function_value(),
)
.type_(),
&float_function_type(),
);
assert_eq!(
FloatFunctionExpr::float_case(
FloatExpr::value(1.0),
vec![(1.0, float_function_value())],
float_function_value(),
)
.type_(),
&float_function_type(),
);
assert_eq!(
FloatFunctionExpr::block(Vec::new(), float_function_value()).type_(),
&float_function_type(),
);
}
fn float_function_value() -> FloatFunctionExpr {
FloatFunctionExpr::reference(FloatFunctionReference::new(function_instantiation()))
}
fn float_function_type() -> FunctionType {
FunctionType::new(vec![ValueType::Float], ValueType::Float)
}
fn function_function_value() -> FunctionFunctionExpr {
FunctionFunctionExpr::reference(
FunctionFunctionReference::new(function_returning_function_instantiation()),
float_function_type(),
)
}
fn function_instantiation() -> FunctionInstantiation {
monomorphic_function_instantiation(
0,
FunctionShape::from_function_type(float_function_type()),
)
}
fn function_returning_function_instantiation() -> FunctionInstantiation {
monomorphic_function_instantiation(
1,
FunctionShape::new(
Vec::new(),
ValueShape::Function(Box::new(FunctionShape::from_function_type(
float_function_type(),
))),
),
)
}
fn tuple_expr() -> crate::plan::TupleExpr {
crate::plan::TupleExpr::value(
vec![Expr::function(crate::plan::FunctionExpr::float(
float_function_value(),
))],
vec![ValueType::Function(Box::new(float_function_type()))],
)
}
}