macro processor: remove cast to int

issue#70
Houtan Bastani 2019-08-06 14:10:11 -04:00
parent 72c216fd7c
commit 618bf7c987
No known key found for this signature in database
GPG Key ID: 000094FB955BE169
6 changed files with 3 additions and 51 deletions

View File

@ -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}

View File

@ -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

View File

@ -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); }

View File

@ -68,7 +68,6 @@ namespace macro
enum class UnaryOp
{
cast_bool,
cast_int,
cast_double,
cast_string,
cast_tuple,

View File

@ -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

View File

@ -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; }