Browse Source

Apply review suggestions

Andrew Chin 1 year ago
parent
commit
5917d89288
2 changed files with 12 additions and 21 deletions
  1. 3 3
      numbat/src/typechecker.rs
  2. 9 18
      numbat/src/typed_ast.rs

+ 3 - 3
numbat/src/typechecker.rs

@@ -522,7 +522,7 @@ impl TypeChecker {
                 if lhs_checked.get_type() == Type::DateTime {
                 if lhs_checked.get_type() == Type::DateTime {
                     let rhs_is_time = dtype(&rhs_checked)
                     let rhs_is_time = dtype(&rhs_checked)
                         .ok()
                         .ok()
-                        .map(|t| t.is_time_dimension(&self.registry))
+                        .map(|t| t.is_time_dimension())
                         .unwrap_or(false);
                         .unwrap_or(false);
                     let rhs_is_datetime = rhs_checked.get_type() == Type::DateTime;
                     let rhs_is_datetime = rhs_checked.get_type() == Type::DateTime;
 
 
@@ -541,8 +541,8 @@ impl TypeChecker {
                             Type::Dimension(time),
                             Type::Dimension(time),
                             true,
                             true,
                         )
                         )
-                    } else if *op == BinaryOperator::Add
-                        || *op == BinaryOperator::Sub && rhs_is_time
+                    } else if (*op == BinaryOperator::Add || *op == BinaryOperator::Sub)
+                        && rhs_is_time
                     {
                     {
                         typed_ast::Expression::BinaryOperatorForDate(
                         typed_ast::Expression::BinaryOperatorForDate(
                             *span_op,
                             *span_op,

+ 9 - 18
numbat/src/typed_ast.rs

@@ -1,15 +1,15 @@
 use itertools::Itertools;
 use itertools::Itertools;
 
 
-use crate::arithmetic::Exponent;
+use crate::arithmetic::{Exponent, Rational};
 use crate::ast::ProcedureKind;
 use crate::ast::ProcedureKind;
 pub use crate::ast::{BinaryOperator, DimensionExpression, UnaryOperator};
 pub use crate::ast::{BinaryOperator, DimensionExpression, UnaryOperator};
 use crate::dimension::DimensionRegistry;
 use crate::dimension::DimensionRegistry;
-use crate::markup as m;
 use crate::{
 use crate::{
     decorator::Decorator, markup::Markup, number::Number, prefix::Prefix,
     decorator::Decorator, markup::Markup, number::Number, prefix::Prefix,
     prefix_parser::AcceptsPrefix, pretty_print::PrettyPrint, registry::BaseRepresentation,
     prefix_parser::AcceptsPrefix, pretty_print::PrettyPrint, registry::BaseRepresentation,
     span::Span,
     span::Span,
 };
 };
+use crate::{markup as m, BaseRepresentationFactor};
 
 
 /// Dimension type
 /// Dimension type
 pub type DType = BaseRepresentation;
 pub type DType = BaseRepresentation;
@@ -42,22 +42,13 @@ impl DType {
     }
     }
     /// Is the current dimension type the Time dimension?
     /// Is the current dimension type the Time dimension?
     ///
     ///
-    /// This ia special helper that's useful when dealing with DateTimes
-    pub fn is_time_dimension(&self, registry: &DimensionRegistry) -> bool {
-        let mut names = vec![];
-
-        let factors: Vec<_> = self.iter().collect();
-        if factors.len() == 1 && factors[0].1 == Exponent::from_integer(1) {
-            names.push(factors[0].0.clone());
-        }
-
-        names.extend(registry.get_derived_entry_names_for(self));
-
-        match &names[..] {
-            [] => false,
-            [single] if single == "Time" => true,
-            _ => false,
-        }
+    /// This is special helper that's useful when dealing with DateTimes
+    pub fn is_time_dimension(&self) -> bool {
+        *self
+            == BaseRepresentation::from_factor(BaseRepresentationFactor(
+                "Time".into(),
+                Rational::from_integer(1),
+            ))
     }
     }
 }
 }