metron-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From justinleet <...@git.apache.org>
Subject [GitHub] metron pull request #606: METRON-980: Short circuit operations for Stellar
Date Thu, 08 Jun 2017 16:10:39 GMT
Github user justinleet commented on a diff in the pull request:

    https://github.com/apache/metron/pull/606#discussion_r120926071
  
    --- Diff: metron-platform/metron-common/src/main/java/org/apache/metron/common/stellar/StellarCompiler.java
---
    @@ -76,14 +93,80 @@ public Expression(Deque<Token<?>> tokenDeque) {
     
         public Object apply(ExpressionState state) {
           Deque<Token<?>> instanceDeque = new ArrayDeque<>();
    -      for(Iterator<Token<?>> it = getTokenDeque().descendingIterator();it.hasNext();)
{
    -        Token<?> token = it.next();
    -        if(token.getUnderlyingType() == DeferredFunction.class) {
    -          DeferredFunction func = (DeferredFunction) token.getValue();
    -          func.apply(instanceDeque, state);
    -        }
    -        else {
    -          instanceDeque.push(token);
    +      {
    +        boolean skipElse = false;
    +        Token<?> token = null;
    +        for (Iterator<Token<?>> it = getTokenDeque().descendingIterator();
it.hasNext(); ) {
    +          token = it.next();
    +          //if we've skipped an else previously, then we need to skip the deferred tokens
associated with the else.
    +          if(skipElse && token.getUnderlyingType() == ElseExpr.class) {
    +            while(it.hasNext()) {
    +              token = it.next();
    +              if(token.getUnderlyingType() == EndConditional.class) {
    +                break;
    +              }
    +            }
    +            skipElse = false;
    +          }
    +          /*
    +          curr is the current value on the stack.  This is the non-deferred actual evaluation
for this expression
    +          and with the current context.
    +           */
    +          Token<?> curr = instanceDeque.peek();
    +          if( curr != null
    +           && curr.getValue() != null && curr.getValue() instanceof Boolean
    +           && ShortCircuitOp.class.isAssignableFrom(token.getUnderlyingType())
    +                  ) {
    +            //if we have a boolean as the current value and the next non-contextual token
is a short circuit op
    +            //then we need to short circuit possibly
    +            if(token.getUnderlyingType() == BooleanArg.class) {
    +              if (curr.getMultiArgContext() != null
    +                      && curr.getMultiArgContext().getVariety() == FrameContext.BOOLEAN_OR
    +                      && (Boolean) (curr.getValue())
    +                      ) {
    +                //short circuit the or
    +                FrameContext.Context context = curr.getMultiArgContext();
    +                shortCircuit(it, context);
    +              } else if (curr.getMultiArgContext() != null
    +                      && curr.getMultiArgContext().getVariety() == FrameContext.BOOLEAN_AND
    +                      && !(Boolean) (curr.getValue())
    +                      ) {
    +                //short circuit the and
    +                FrameContext.Context context = curr.getMultiArgContext();
    +                shortCircuit(it, context);
    +              }
    +            }
    +            else if(token.getUnderlyingType() == IfExpr.class) {
    +              //short circuit the if/then/else
    +              instanceDeque.pop();
    +              if((Boolean)curr.getValue()) {
    +                //choose then
    +                skipElse = true;
    +              }
    +              else {
    +                //choose else
    +                while(it.hasNext()) {
    +                  Token<?> t = it.next();
    +                  if(t.getUnderlyingType() == ElseExpr.class) {
    +                    break;
    +                  }
    +                }
    +              }
    +            }
    +          }
    +          if (token.getUnderlyingType() == DeferredFunction.class) {
    +            DeferredFunction func = (DeferredFunction) token.getValue();
    +            func.apply(instanceDeque, state);
    +          }
    +          else if(token.getUnderlyingType() == ShortCircuitFrame.class
    +               || ShortCircuitOp.class.isAssignableFrom(token.getUnderlyingType())
    +                  ) {
    +            continue;
    --- End diff --
    
    `continue;` can be dropped here. There's nothing afterwards to skip


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message