Skip to content

Commit

Permalink
Introduce DType::is_scalar function
Browse files Browse the repository at this point in the history
  • Loading branch information
eminence committed Nov 23, 2023
1 parent 1651bbc commit 341ca6d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
6 changes: 3 additions & 3 deletions numbat/src/typechecker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ impl TypeChecker {

match *op {
ast::UnaryOperator::Factorial => {
if dtype != DType::unity() {
if !dtype.is_scalar() {
return Err(TypeCheckError::NonScalarFactorialArgument(
expr.full_span(),
dtype,
Expand Down Expand Up @@ -539,15 +539,15 @@ impl TypeChecker {
}
typed_ast::BinaryOperator::Power => {
let exponent_type = dtype(&rhs_checked)?;
if exponent_type != DType::unity() {
if !exponent_type.is_scalar() {
return Err(TypeCheckError::NonScalarExponent(
rhs.full_span(),
exponent_type,
));
}

let base_type = dtype(&lhs_checked)?;
if base_type == DType::unity() {
if base_type.is_scalar() {
// Skip evaluating the exponent if the lhs is a scalar. This allows
// for arbitrary (decimal) exponents, if the base is a scalar.

Expand Down
5 changes: 4 additions & 1 deletion numbat/src/typed_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ use crate::{
pub type DType = BaseRepresentation;

impl DType {
pub fn is_scalar(&self) -> bool {
self == &DType::unity()
}
pub fn to_readable_type(&self, registry: &DimensionRegistry) -> m::Markup {
if self == &DType::unity() {
if self.is_scalar() {
return m::type_identifier("Scalar");
}

Expand Down

0 comments on commit 341ca6d

Please sign in to comment.