From 618bf7c987c30d13e34b2cfc5b610e3e6cfa9e08 Mon Sep 17 00:00:00 2001
From: Houtan Bastani <houtan@dynare.org>
Date: Tue, 6 Aug 2019 14:10:11 -0400
Subject: [PATCH] macro processor: remove cast to int

---
 doc/macroprocessor/macroprocessor.tex    |  1 -
 src/macro/Expressions.cc                 | 37 ------------------------
 src/macro/Expressions.hh                 |  8 +----
 src/macro/ForwardDeclarationsAndEnums.hh |  1 -
 src/macro/Parser.yy                      |  6 ++--
 src/macro/Tokenizer.ll                   |  1 -
 6 files changed, 3 insertions(+), 51 deletions(-)

diff --git a/doc/macroprocessor/macroprocessor.tex b/doc/macroprocessor/macroprocessor.tex
index 787e3d68..26ac1528 100644
--- a/doc/macroprocessor/macroprocessor.tex
+++ b/doc/macroprocessor/macroprocessor.tex
@@ -152,7 +152,6 @@
 \item Variables/literals of the types listed above can be cast to other types
   \begin{itemize}
   \item \texttt{(bool) 3.9} $\rightarrow$ \texttt{true}
-  \item \texttt{(int) 3.9} $\rightarrow$ \texttt{3}
   \item \texttt{(double) ``3.9''} $\rightarrow$ \texttt{3.9}
   \item \texttt{(array) 3.9} $\rightarrow$ \texttt{[3.9]}
   \item \texttt{(double) [3.9]} $\rightarrow$ \texttt{3.9}
diff --git a/src/macro/Expressions.cc b/src/macro/Expressions.cc
index 93ce47c7..859a6028 100644
--- a/src/macro/Expressions.cc
+++ b/src/macro/Expressions.cc
@@ -310,19 +310,6 @@ String::cast_bool() const
     }
 }
 
-DoublePtr
-String::cast_int() const
-{
-  try
-    {
-      return make_shared<Double>(stoi(value), env);
-    }
-  catch (...)
-    {
-      throw StackTrace(R"(")" + value + R"(" cannot be converted to an int)");
-    }
-}
-
 DoublePtr
 String::cast_double() const
 {
@@ -539,14 +526,6 @@ Array::cast_bool() const
   return arr.at(0)->eval()->cast_bool();
 }
 
-DoublePtr
-Array::cast_int() const
-{
-  if (arr.size() != 1)
-    throw StackTrace("Array must be of size 1 to be cast to an int");
-  return arr.at(0)->eval()->cast_int();
-}
-
 DoublePtr
 Array::cast_double() const
 {
@@ -597,14 +576,6 @@ Tuple::cast_bool() const
   return tup.at(0)->eval()->cast_bool();
 }
 
-DoublePtr
-Tuple::cast_int() const
-{
-  if (tup.size() != 1)
-    throw StackTrace("Tuple must be of size 1 to be cast to an int");
-  return tup.at(0)->eval()->cast_int();
-}
-
 DoublePtr
 Tuple::cast_double() const
 {
@@ -798,8 +769,6 @@ UnaryOp::eval()
         {
         case codes::UnaryOp::cast_bool:
           return argbt->cast_bool();
-        case codes::UnaryOp::cast_int:
-          return argbt->cast_int();
         case codes::UnaryOp::cast_double:
           return argbt->cast_double();
         case codes::UnaryOp::cast_string:
@@ -1130,8 +1099,6 @@ UnaryOp::to_string() const noexcept
     {
     case codes::UnaryOp::cast_bool:
       return "(bool)" + retval;
-    case codes::UnaryOp::cast_int:
-      return "(int)" + retval;
     case codes::UnaryOp::cast_double:
       return "(double)" + retval;
     case codes::UnaryOp::cast_string:
@@ -1343,9 +1310,6 @@ UnaryOp::print(ostream &output, bool matlab_output) const noexcept
     case codes::UnaryOp::cast_bool:
       output << "(bool)";
       break;
-    case codes::UnaryOp::cast_int:
-      output << "(int)";
-      break;
     case codes::UnaryOp::cast_double:
       output << "(double)";
       break;
@@ -1444,7 +1408,6 @@ UnaryOp::print(ostream &output, bool matlab_output) const noexcept
   arg->print(output, matlab_output);
 
   if (op_code != codes::UnaryOp::cast_bool
-      && op_code != codes::UnaryOp::cast_int
       && op_code != codes::UnaryOp::cast_double
       && op_code != codes::UnaryOp::cast_string
       && op_code != codes::UnaryOp::cast_tuple
diff --git a/src/macro/Expressions.hh b/src/macro/Expressions.hh
index 7d7143ff..4f705510 100644
--- a/src/macro/Expressions.hh
+++ b/src/macro/Expressions.hh
@@ -181,7 +181,6 @@ namespace macro
     virtual DoublePtr normcdf() const { throw StackTrace("Operator `normcdf` does not exist for this type"); }
     virtual DoublePtr normcdf(const BaseTypePtr &btp1, const BaseTypePtr &btp2) const { throw StackTrace("Operator `normcdf` does not exist for this type"); }
     virtual BoolPtr cast_bool() const { throw StackTrace("This type cannot be cast to a boolean"); }
-    virtual DoublePtr cast_int() const { throw StackTrace("This type cannot be cast to an integer"); }
     virtual DoublePtr cast_double() const { throw StackTrace("This type cannot be cast to a double"); }
     virtual StringPtr cast_string() const { throw StackTrace("This type cannot be cast to a string"); }
     virtual TuplePtr cast_tuple() const { throw StackTrace("This type cannot be cast to a tuple"); }
@@ -209,8 +208,7 @@ namespace macro
     BoolPtr logical_or(const BaseTypePtr &btp) const override;
     BoolPtr logical_not() const override;
     inline BoolPtr cast_bool() const override { return make_shared<Bool>(value, env); }
-    inline DoublePtr cast_int() const override { return value ? make_shared<Double>(1, env) : make_shared<Double>(0, env); }
-    inline DoublePtr cast_double() const override { return cast_int(); }
+    inline DoublePtr cast_double() const override { return value ? make_shared<Double>(1, env) : make_shared<Double>(0, env); }
     inline StringPtr cast_string() const override { return make_shared<String>(this->to_string(), env); }
     inline TuplePtr cast_tuple() const override
     {
@@ -305,7 +303,6 @@ namespace macro
     }
     DoublePtr normcdf(const BaseTypePtr &btp1, const BaseTypePtr &btp2) const override;
     inline BoolPtr cast_bool() const override { return make_shared<Bool>(static_cast<bool>(value), env); }
-    inline DoublePtr cast_int() const override { return make_shared<Double>(static_cast<int>(value), env); }
     inline DoublePtr cast_double() const override { return make_shared<Double>(value, env); }
     inline StringPtr cast_string() const override { return make_shared<String>(this->to_string(), env); }
     inline TuplePtr cast_tuple() const override
@@ -341,7 +338,6 @@ namespace macro
     BoolPtr is_equal(const BaseTypePtr &btp) const override;
     inline DoublePtr length() const override { return make_shared<Double>(value.size(), env); }
     BoolPtr cast_bool() const override;
-    DoublePtr cast_int() const override;
     DoublePtr cast_double() const override;
     inline StringPtr cast_string() const override { return make_shared<String>(value, env); }
     inline TuplePtr cast_tuple() const override
@@ -378,7 +374,6 @@ namespace macro
     BoolPtr contains(const BaseTypePtr &btp) const override;
     inline DoublePtr length() const override { return make_shared<Double>(tup.size(), env); }
     BoolPtr cast_bool() const override;
-    DoublePtr cast_int() const override;
     DoublePtr cast_double() const override;
     inline StringPtr cast_string() const override { return make_shared<String>(this->to_string(), env); }
     inline TuplePtr cast_tuple() const override { return make_shared<Tuple>(tup, env); }
@@ -425,7 +420,6 @@ namespace macro
     inline DoublePtr length() const override { return make_shared<Double>(arr.size(), env); }
     DoublePtr sum() const override;
     BoolPtr cast_bool() const override;
-    DoublePtr cast_int() const override;
     DoublePtr cast_double() const override;
     inline StringPtr cast_string() const override { return make_shared<String>(this->to_string(), env); }
     inline TuplePtr cast_tuple() const override { return make_shared<Tuple>(arr, env); }
diff --git a/src/macro/ForwardDeclarationsAndEnums.hh b/src/macro/ForwardDeclarationsAndEnums.hh
index 09eb1065..9c2b01b1 100644
--- a/src/macro/ForwardDeclarationsAndEnums.hh
+++ b/src/macro/ForwardDeclarationsAndEnums.hh
@@ -68,7 +68,6 @@ namespace macro
     enum class UnaryOp
       {
        cast_bool,
-       cast_int,
        cast_double,
        cast_string,
        cast_tuple,
diff --git a/src/macro/Parser.yy b/src/macro/Parser.yy
index 36ff4903..e27c88a8 100644
--- a/src/macro/Parser.yy
+++ b/src/macro/Parser.yy
@@ -65,7 +65,7 @@ using namespace macro;
 %token SQRT CBRT SIGN MAX MIN FLOOR CEIL TRUNC SUM MOD
 %token ERF ERFC GAMMA LGAMMA ROUND NORMPDF NORMCDF LENGTH
 
-%token BOOL INT DOUBLE STRING TUPLE ARRAY
+%token BOOL DOUBLE STRING TUPLE ARRAY
 
 %left OR
 %left AND
@@ -78,7 +78,7 @@ using namespace macro;
 %left PLUS MINUS
 %left TIMES DIVIDE
 %precedence UMINUS UPLUS NOT
-%precedence CAST_BOOL CAST_INT CAST_DOUBLE CAST_STRING CAST_TUPLE CAST_ARRAY
+%precedence CAST_BOOL CAST_DOUBLE CAST_STRING CAST_TUPLE CAST_ARRAY
 %nonassoc POWER
 
 %token <string> NAME TEXT QUOTED_STRING NUMBER EOL
@@ -324,8 +324,6 @@ expr : LPAREN expr RPAREN
        { $$ = make_shared<Comprehension>($2, $4, $6, $8, driver.env, @$); }
      | LPAREN BOOL RPAREN expr %prec CAST_BOOL
        { $$ = make_shared<UnaryOp>(codes::UnaryOp::cast_bool, $4, driver.env, @$); }
-     | LPAREN INT RPAREN expr %prec CAST_INT
-       { $$ = make_shared<UnaryOp>(codes::UnaryOp::cast_int, $4, driver.env, @$); }
      | LPAREN DOUBLE RPAREN expr %prec CAST_DOUBLE
        { $$ = make_shared<UnaryOp>(codes::UnaryOp::cast_double, $4, driver.env, @$); }
      | LPAREN STRING RPAREN expr %prec CAST_STRING
diff --git a/src/macro/Tokenizer.ll b/src/macro/Tokenizer.ll
index a9939128..f957f1d7 100644
--- a/src/macro/Tokenizer.ll
+++ b/src/macro/Tokenizer.ll
@@ -143,7 +143,6 @@ CONT \\\\{SPC}*
 <expr,eval>normcdf         { return token::NORMCDF; }
 
 <expr,eval>bool            { return token::BOOL; }
-<expr,eval>int             { return token::INT; }
 <expr,eval>double          { return token::DOUBLE; }
 <expr,eval>string          { return token::STRING; }
 <expr,eval>tuple           { return token::TUPLE; }
-- 
GitLab