macro processor: add `defined` operator
parent
8ebd2a1735
commit
b77f6ecd94
|
@ -861,6 +861,8 @@ UnaryOp::eval()
|
||||||
return argbt->normpdf();
|
return argbt->normpdf();
|
||||||
case codes::UnaryOp::normcdf:
|
case codes::UnaryOp::normcdf:
|
||||||
return argbt->normcdf();
|
return argbt->normcdf();
|
||||||
|
case codes::UnaryOp::defined:
|
||||||
|
return argbt->defined();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (StackTrace &ex)
|
catch (StackTrace &ex)
|
||||||
|
@ -1203,6 +1205,8 @@ UnaryOp::to_string() const noexcept
|
||||||
return "normpdf(" + retval + ")";
|
return "normpdf(" + retval + ")";
|
||||||
case codes::UnaryOp::normcdf:
|
case codes::UnaryOp::normcdf:
|
||||||
return "normcdf(" + retval + ")";
|
return "normcdf(" + retval + ")";
|
||||||
|
case codes::UnaryOp::defined:
|
||||||
|
return "defined(" + retval + ")";
|
||||||
}
|
}
|
||||||
// Suppress GCC warning
|
// Suppress GCC warning
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
|
@ -1463,6 +1467,9 @@ UnaryOp::print(ostream &output, bool matlab_output) const noexcept
|
||||||
case codes::UnaryOp::normcdf:
|
case codes::UnaryOp::normcdf:
|
||||||
output << "normcdf(";
|
output << "normcdf(";
|
||||||
break;
|
break;
|
||||||
|
case codes::UnaryOp::defined:
|
||||||
|
output << "defined(";
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
arg->print(output, matlab_output);
|
arg->print(output, matlab_output);
|
||||||
|
|
|
@ -191,6 +191,7 @@ namespace macro
|
||||||
virtual StringPtr cast_string() const { throw StackTrace("This type cannot be cast to a string"); }
|
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"); }
|
virtual TuplePtr cast_tuple() const { throw StackTrace("This type cannot be cast to a tuple"); }
|
||||||
virtual ArrayPtr cast_array() const { throw StackTrace("This type cannot be cast to an array"); }
|
virtual ArrayPtr cast_array() const { throw StackTrace("This type cannot be cast to an array"); }
|
||||||
|
virtual BoolPtr defined() const { throw StackTrace("Operator `defined` does not exist for this type"); }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -358,6 +359,10 @@ namespace macro
|
||||||
{
|
{
|
||||||
return make_shared<Array>(vector<ExpressionPtr>{make_shared<String>(value, env)}, env);
|
return make_shared<Array>(vector<ExpressionPtr>{make_shared<String>(value, env)}, env);
|
||||||
}
|
}
|
||||||
|
inline BoolPtr defined() const override
|
||||||
|
{
|
||||||
|
return make_shared<Bool>(env.isSymbolDefined(value), env);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -104,7 +104,8 @@ namespace macro
|
||||||
lgamma,
|
lgamma,
|
||||||
round,
|
round,
|
||||||
normpdf,
|
normpdf,
|
||||||
normcdf
|
normcdf,
|
||||||
|
defined
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class BinaryOp
|
enum class BinaryOp
|
||||||
|
|
|
@ -70,6 +70,8 @@ using namespace macro;
|
||||||
|
|
||||||
%token BOOL REAL STRING TUPLE ARRAY
|
%token BOOL REAL STRING TUPLE ARRAY
|
||||||
|
|
||||||
|
%token DEFINED
|
||||||
|
|
||||||
%left OR
|
%left OR
|
||||||
%left AND
|
%left AND
|
||||||
%left EQUAL_EQUAL NOT_EQUAL
|
%left EQUAL_EQUAL NOT_EQUAL
|
||||||
|
@ -392,6 +394,8 @@ primary_expr : LPAREN expr RPAREN
|
||||||
{ $$ = make_shared<TrinaryOp>(codes::TrinaryOp::normpdf, $3, $5, $7, driver.env, @$); }
|
{ $$ = make_shared<TrinaryOp>(codes::TrinaryOp::normpdf, $3, $5, $7, driver.env, @$); }
|
||||||
| NORMCDF LPAREN expr COMMA expr COMMA expr RPAREN
|
| NORMCDF LPAREN expr COMMA expr COMMA expr RPAREN
|
||||||
{ $$ = make_shared<TrinaryOp>(codes::TrinaryOp::normcdf, $3, $5, $7, driver.env, @$); }
|
{ $$ = make_shared<TrinaryOp>(codes::TrinaryOp::normcdf, $3, $5, $7, driver.env, @$); }
|
||||||
|
| DEFINED LPAREN NAME RPAREN
|
||||||
|
{ $$ = make_shared<UnaryOp>(codes::UnaryOp::defined, make_shared<String>($3, driver.env, @3), driver.env, @$); }
|
||||||
;
|
;
|
||||||
|
|
||||||
oper_expr : primary_expr
|
oper_expr : primary_expr
|
||||||
|
|
|
@ -156,6 +156,8 @@ CONT \\\\{SPC}*
|
||||||
<expr,eval>tuple { return token::TUPLE; }
|
<expr,eval>tuple { return token::TUPLE; }
|
||||||
<expr,eval>array { return token::ARRAY; }
|
<expr,eval>array { return token::ARRAY; }
|
||||||
|
|
||||||
|
<expr,eval>defined { return token::DEFINED; }
|
||||||
|
|
||||||
<expr,eval>((([0-9]*\.[0-9]+)|([0-9]+\.))([ed][-+]?[0-9]+)?)|([0-9]+([ed][-+]?[0-9]+)?)|nan|inf {
|
<expr,eval>((([0-9]*\.[0-9]+)|([0-9]+\.))([ed][-+]?[0-9]+)?)|([0-9]+([ed][-+]?[0-9]+)?)|nan|inf {
|
||||||
yylval->build<string>(yytext);
|
yylval->build<string>(yytext);
|
||||||
return token::NUMBER;
|
return token::NUMBER;
|
||||||
|
|
Loading…
Reference in New Issue