Skip to content

Commit

Permalink
Implement support for abs() in subscription expressions
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitrii Petukhov <dpetukhov@bloomberg.net>
  • Loading branch information
bbpetukhov committed Feb 24, 2025
1 parent eb395e9 commit 28c0bcc
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 78 deletions.
29 changes: 29 additions & 0 deletions src/groups/bmq/bmqeval/bmqeval_simpleevaluator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,35 @@ bdld::Datum SimpleEvaluator::Not::evaluate(EvaluationContext& context) const
return bdld::Datum::createBoolean(!value.theBoolean());
}

// ---------------------------------
// class SimpleEvaluator::Abs
// ---------------------------------

bdld::Datum
SimpleEvaluator::Abs::evaluate(EvaluationContext& context) const
{
bdld::Datum expr = d_expression->evaluate(context);
if (context.d_stop) {
return bdld::Datum::createNull(); // RETURN
}

bsls::Types::Int64 value;
if (expr.isInteger64()) {
value = expr.theInteger64();
}

else if (expr.isInteger()) {
value = expr.theInteger();
}
else {
context.d_lastError = ErrorType::e_TYPE;
return context.stop(); // RETURN
}

value = abs(value);
return bdld::Datum::createInteger64(value, context.d_allocator);
}

// --------------------------
// class SimpleEvaluator::Exists
// --------------------------
Expand Down
36 changes: 36 additions & 0 deletions src/groups/bmq/bmqeval/bmqeval_simpleevaluator.h
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,33 @@ class SimpleEvaluator {
evaluate(EvaluationContext& context) const BSLS_KEYWORD_OVERRIDE;
};

// ----------
// Abs
// ----------

class Abs : public Expression {
private:
// DATA

// The expression to negate.
ExpressionPtr d_expression;

public:
// CREATORS

/// Create an object that implements arithmetic negation.
explicit Abs(ExpressionPtr expression);

// ACCESSORS

/// Evaluate `expression` passed to the constructor. If it is an
/// integer, return the negated value as an Int64 Datum. Otherwise,
/// set the error in the context to e_TYPE, stop the evaluation,
/// and return a null datum.
bdld::Datum
evaluate(EvaluationContext& context) const BSLS_KEYWORD_OVERRIDE;
};

// ---
// Not
// ---
Expand Down Expand Up @@ -995,6 +1022,15 @@ inline SimpleEvaluator::Not::Not(ExpressionPtr expression)
{
}

// ------------------------------------------
// template class SimpleEvaluator::Abs
// ------------------------------------------

inline SimpleEvaluator::Abs::Abs(ExpressionPtr expression)
: d_expression(expression)
{
}

// ------------------------
// class CompilationContext
// ------------------------
Expand Down
8 changes: 8 additions & 0 deletions src/groups/bmq/bmqeval/bmqeval_simpleevaluator.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class MockPropertiesReader : public PropertiesReader {
d_map["i_2"] = bdld::Datum::createInteger(2);
d_map["i_3"] = bdld::Datum::createInteger(3);
d_map["i_42"] = bdld::Datum::createInteger(42);
d_map["i_n42"] = bdld::Datum::createInteger(-42);
d_map["i64_42"] = bdld::Datum::createInteger64(42, allocator);
d_map["s_foo"] = bdld::Datum::createStringRef("foo", allocator);
d_map["exists"] = bdld::Datum::createInteger(42);
Expand Down Expand Up @@ -428,6 +429,13 @@ static void test3_evaluation()
// mixed integer types
{"i_42 == 42", true},

// abs function
{"i_n42 == -42", true},
{"-i_n42 == 42", true},
{"abs(i_n42) == 42", true},
{"abs(i_n42) == i_42", true},
{"abs(i_42) == i_42", true},

// string comparisons
{"s_foo == \"foo\"", true},
{"s_foo != \"foo\"", false},
Expand Down
8 changes: 8 additions & 0 deletions src/groups/bmq/bmqeval/bmqeval_simpleevaluatorparser.y
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
%token TIMES "*" DIVIDES "/" MODULUS "%";
%token EQ "=" NE "<>" LT "<" LE "<=" GE ">=" GT ">";
%token <bsl::string> EXISTS "exists";
%token <bsl::string> ABS "abs";

%left OR;
%left AND;
Expand Down Expand Up @@ -108,6 +109,11 @@ variable
++ctx.d_numProperties;
$$ = $1;
}
| ABS
{
++ctx.d_numProperties;
$$ = $1;
}

expression
: variable {
Expand Down Expand Up @@ -158,6 +164,8 @@ expression
{ $$ = ctx.makeBooleanBinaryExpression<SimpleEvaluator::Or>($1, $3); }
| NOT expression
{ $$ = ctx.makeUnaryExpression<SimpleEvaluator::Not>($2); }
| ABS LPAR expression RPAR
{ $$ = ctx.makeUnaryExpression<SimpleEvaluator::Abs>($3); }
| expression PLUS expression
{ $$ = ctx.makeNumBinaryExpression<bsl::plus>($1, $3); }
| expression MINUS expression
Expand Down
5 changes: 5 additions & 0 deletions src/groups/bmq/bmqeval/bmqeval_simpleevaluatorscanner.l
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
return SimpleEvaluatorParser::make_EXISTS(yytext);
}

"abs" {
updatePosition();
return SimpleEvaluatorParser::make_ABS(yytext);
}

[a-zA-Z][a-zA-Z0-9_.]* {
updatePosition();
return SimpleEvaluatorParser::make_PROPERTY(yytext);
Expand Down
14 changes: 7 additions & 7 deletions src/groups/bmq/bmqu/bmqu_operationchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,11 @@

#if BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES
// Include version that can be compiled with C++03
// Generated on Tue Oct 15 17:39:53 2024
// Generated on Fri Feb 14 17:43:15 2025
// Command line: sim_cpp11_features.pl bmqu_operationchain.h
#define COMPILING_BMQU_OPERATIONCHAIN_H
#include <bmqu_operationchain_cpp03.h>
#undef COMPILING_BMQU_OPERATIONCHAIN_H
# define COMPILING_BMQU_OPERATIONCHAIN_H
# include <bmqu_operationchain_cpp03.h>
# undef COMPILING_BMQU_OPERATIONCHAIN_H
#else

namespace BloombergLP {
Expand Down Expand Up @@ -301,7 +301,7 @@ class OperationChain_CompletionCallbackWrapper {

public:
// ACCESSORS
#if !BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES // $var-args=9
#if !BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES // $var-args=9

/// Invoke the associated completion callback with the specified `args`
/// arguments and notify the associated operation chain. Propagate any
Expand Down Expand Up @@ -806,7 +806,7 @@ inline OperationChain_CompletionCallbackWrapper<CO_CALLBACK>::
}

// ACCESSORS
#if !BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES // $var-args=9
#if !BSLS_COMPILERFEATURES_SIMULATE_CPP11_FEATURES // $var-args=9
template <class CO_CALLBACK>
template <class... ARGS>
inline void OperationChain_CompletionCallbackWrapper<CO_CALLBACK>::operator()(
Expand Down Expand Up @@ -1021,6 +1021,6 @@ inline void bmqu::swap(OperationChainLink& lhs,

} // close enterprise namespace

#endif // End C++11 code
#endif // End C++11 code

#endif
Loading

0 comments on commit 28c0bcc

Please sign in to comment.