Commit 273c54f7 authored by Houtan Bastani's avatar Houtan Bastani

macro processor: introduce functions to test types of variables and literals

parent d35f2609
Pipeline #1584 passed with stage
in 1 minute and 25 seconds
......@@ -805,6 +805,16 @@ UnaryOp::eval()
return argbt->length();
case codes::UnaryOp::isempty:
return argbt->isempty();
case codes::UnaryOp::isboolean:
return argbt->isboolean();
case codes::UnaryOp::isreal:
return argbt->isreal();
case codes::UnaryOp::isstring:
return argbt->isstring();
case codes::UnaryOp::istuple:
return argbt->istuple();
case codes::UnaryOp::isarray:
return argbt->isarray();
case codes::UnaryOp::exp:
return argbt->exp();
case codes::UnaryOp::ln:
......@@ -1137,6 +1147,16 @@ UnaryOp::to_string() const noexcept
return "length(" + retval + ")";
case codes::UnaryOp::isempty:
return "isempty(" + retval + ")";
case codes::UnaryOp::isboolean:
return "isboolean(" + retval + ")";
case codes::UnaryOp::isreal:
return "isreal(" + retval + ")";
case codes::UnaryOp::isstring:
return "isstring(" + retval + ")";
case codes::UnaryOp::istuple:
return "istuple(" + retval + ")";
case codes::UnaryOp::isarray:
return "isarray(" + retval + ")";
case codes::UnaryOp::exp:
return "exp(" + retval + ")";
case codes::UnaryOp::ln:
......@@ -1359,6 +1379,21 @@ UnaryOp::print(ostream &output, bool matlab_output) const noexcept
case codes::UnaryOp::isempty:
output << "isempty(";
break;
case codes::UnaryOp::isboolean:
output << "isboolean(";
break;
case codes::UnaryOp::isreal:
output << "isreal(";
break;
case codes::UnaryOp::isstring:
output << "isstring(";
break;
case codes::UnaryOp::istuple:
output << "istuple(";
break;
case codes::UnaryOp::isarray:
output << "isarray(";
break;
case codes::UnaryOp::exp:
output << "exp(";
break;
......
......@@ -149,6 +149,11 @@ namespace macro
virtual BoolPtr contains(const BaseTypePtr &btp) const { throw StackTrace("Second argument of `in` operator must be an array"); }
virtual RealPtr length() const { throw StackTrace("Operator `length` does not exist for this type"); }
virtual BoolPtr isempty() const { throw StackTrace("Operator `isempty` does not exist for this type"); }
virtual BoolPtr isboolean() const { return make_shared<Bool>(false, env, location); }
virtual BoolPtr isreal() const { return make_shared<Bool>(false, env, location); }
virtual BoolPtr isstring() const { return make_shared<Bool>(false, env, location); }
virtual BoolPtr istuple() const { return make_shared<Bool>(false, env, location); }
virtual BoolPtr isarray() const { return make_shared<Bool>(false, env, location); }
virtual RealPtr max(const BaseTypePtr &btp) const { throw StackTrace("Operator `max` does not exist for this type"); }
virtual RealPtr min(const BaseTypePtr &btp) const { throw StackTrace("Operator `min` does not exist for this type"); }
virtual RealPtr mod(const BaseTypePtr &btp) const { throw StackTrace("Operator `mod` does not exist for this type"); }
......@@ -208,6 +213,7 @@ namespace macro
BoolPtr logical_and(const BaseTypePtr &btp) const override;
BoolPtr logical_or(const BaseTypePtr &btp) const override;
BoolPtr logical_not() const override;
inline BoolPtr isboolean() const override { return make_shared<Bool>(true, env, location); }
inline BoolPtr cast_bool() const override { return make_shared<Bool>(value, env); }
inline RealPtr cast_real() const override { return value ? make_shared<Real>(1, env) : make_shared<Real>(0, env); }
inline StringPtr cast_string() const override { return make_shared<String>(this->to_string(), env); }
......@@ -260,6 +266,7 @@ namespace macro
BoolPtr is_less_equal(const BaseTypePtr &btp) const override;
BoolPtr is_greater_equal(const BaseTypePtr &btp) const override;
BoolPtr is_equal(const BaseTypePtr &btp) const override;
inline BoolPtr isreal() const override { return make_shared<Bool>(true, env, location); }
BoolPtr logical_and(const BaseTypePtr &btp) const override;
BoolPtr logical_or(const BaseTypePtr &btp) const override;
BoolPtr logical_not() const override;
......@@ -337,6 +344,7 @@ namespace macro
BoolPtr is_less_equal(const BaseTypePtr &btp) const override;
BoolPtr is_greater_equal(const BaseTypePtr &btp) const override;
BoolPtr is_equal(const BaseTypePtr &btp) const override;
inline BoolPtr isstring() const override { return make_shared<Bool>(true, env, location); }
inline RealPtr length() const override { return make_shared<Real>(value.size(), env); }
inline BoolPtr isempty() const override { return make_shared<Bool>(value.empty(), env); }
BoolPtr cast_bool() const override;
......@@ -373,6 +381,7 @@ namespace macro
inline vector<ExpressionPtr> getValue() const { return tup; }
inline ExpressionPtr at(int i) const { return tup.at(i); }
BoolPtr is_equal(const BaseTypePtr &btp) const override;
inline BoolPtr istuple() const override { return make_shared<Bool>(true, env, location); }
BoolPtr contains(const BaseTypePtr &btp) const override;
inline RealPtr length() const override { return make_shared<Real>(tup.size(), env); }
inline BoolPtr isempty() const override { return make_shared<Bool>(empty(), env); }
......@@ -417,6 +426,7 @@ namespace macro
BaseTypePtr times(const BaseTypePtr &bt) const override;
BaseTypePtr power(const BaseTypePtr &btp) const override;
BoolPtr is_equal(const BaseTypePtr &btp) const override;
inline BoolPtr isarray() const override { return make_shared<Bool>(true, env, location); }
ArrayPtr set_union(const BaseTypePtr &btp) const override;
ArrayPtr set_intersection(const BaseTypePtr &btp) const override;
BoolPtr contains(const BaseTypePtr &btp) const override;
......
......@@ -77,6 +77,11 @@ namespace macro
unary_plus,
length,
isempty,
isboolean,
isreal,
isstring,
istuple,
isarray,
exp,
ln,
log10,
......
......@@ -66,6 +66,7 @@ using namespace macro;
%token ERF ERFC GAMMA LGAMMA ROUND NORMPDF NORMCDF LENGTH
%token ISEMPTY
%token ISBOOLEAN ISREAL ISSTRING ISTUPLE ISARRAY
%token BOOL REAL STRING TUPLE ARRAY
......@@ -344,6 +345,16 @@ expr : LPAREN expr RPAREN
{ $$ = make_shared<UnaryOp>(codes::UnaryOp::length, $3, driver.env, @$); }
| ISEMPTY LPAREN expr RPAREN
{ $$ = make_shared<UnaryOp>(codes::UnaryOp::isempty, $3, driver.env, @$); }
| ISBOOLEAN LPAREN expr RPAREN
{ $$ = make_shared<UnaryOp>(codes::UnaryOp::isboolean, $3, driver.env, @$); }
| ISREAL LPAREN expr RPAREN
{ $$ = make_shared<UnaryOp>(codes::UnaryOp::isreal, $3, driver.env, @$); }
| ISSTRING LPAREN expr RPAREN
{ $$ = make_shared<UnaryOp>(codes::UnaryOp::isstring, $3, driver.env, @$); }
| ISTUPLE LPAREN expr RPAREN
{ $$ = make_shared<UnaryOp>(codes::UnaryOp::istuple, $3, driver.env, @$); }
| ISARRAY LPAREN expr RPAREN
{ $$ = make_shared<UnaryOp>(codes::UnaryOp::isarray, $3, driver.env, @$); }
| EXP LPAREN expr RPAREN
{ $$ = make_shared<UnaryOp>(codes::UnaryOp::exp, $3, driver.env, @$); }
| LOG LPAREN expr RPAREN
......
......@@ -141,7 +141,13 @@ CONT \\\\{SPC}*
<expr,eval>length { return token::LENGTH; }
<expr,eval>normpdf { return token::NORMPDF; }
<expr,eval>normcdf { return token::NORMCDF; }
<expr,eval>isempty { return token::ISEMPTY; }
<expr,eval>isboolean { return token::ISBOOLEAN; }
<expr,eval>isreal { return token::ISREAL; }
<expr,eval>isstring { return token::ISSTRING; }
<expr,eval>istuple { return token::ISTUPLE; }
<expr,eval>isarray { return token::ISARRAY; }
<expr,eval>bool { return token::BOOL; }
<expr,eval>real { return token::REAL; }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment