-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added visitor for the expressions Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * First constant folding draft Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Constant folding for unary expressions Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * More testing Constant folding for exists expression Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Little mistake Signed-off-by: Francesco Guardiani <francescoguard@gmail.com> * Added a ParserBuilder Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>
- Loading branch information
1 parent
687e03b
commit a0b0835
Showing
33 changed files
with
431 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
sql/src/main/java/io/cloudevents/sql/impl/ConstantFoldingExpressionVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package io.cloudevents.sql.impl; | ||
|
||
import io.cloudevents.SpecVersion; | ||
import io.cloudevents.sql.EvaluationRuntime; | ||
import io.cloudevents.sql.impl.expressions.*; | ||
|
||
public class ConstantFoldingExpressionVisitor implements ExpressionInternalVisitor<ExpressionInternal> { | ||
|
||
@Override | ||
public ExpressionInternal visitExpressionInternal(ExpressionInternal expressionInternal) { | ||
return expressionInternal; | ||
} | ||
|
||
@Override | ||
public ExpressionInternal visitBaseBinaryExpression(BaseBinaryExpression baseBinaryExpression) { | ||
ExpressionInternal left = baseBinaryExpression.getLeftOperand().visit(this); | ||
ExpressionInternal right = baseBinaryExpression.getRightOperand().visit(this); | ||
|
||
if (left instanceof ValueExpression && right instanceof ValueExpression) { | ||
// I can do constant folding! | ||
return new ValueExpression( | ||
baseBinaryExpression.expressionInterval(), | ||
baseBinaryExpression.expressionText(), | ||
baseBinaryExpression.evaluate( | ||
EvaluationRuntime.getDefault(), | ||
((ValueExpression) left).getValue(), | ||
((ValueExpression) right).getValue(), | ||
FailFastExceptionThrower.getInstance() | ||
) | ||
); | ||
} | ||
|
||
baseBinaryExpression.setLeftOperand(left); | ||
baseBinaryExpression.setRightOperand(right); | ||
return baseBinaryExpression; | ||
} | ||
|
||
@Override | ||
public ExpressionInternal visitExistsExpression(ExistsExpression existsExpression) { | ||
if (SpecVersion.V1.getMandatoryAttributes().contains(existsExpression.getKey())) { | ||
// If the attribute is a mandatory attribute of the spec, there's no need to check it | ||
return new ValueExpression(existsExpression.expressionInterval(), existsExpression.expressionText(), true); | ||
} | ||
return existsExpression; | ||
} | ||
|
||
@Override | ||
public ExpressionInternal visitBaseUnaryExpression(BaseUnaryExpression baseUnaryExpression) { | ||
ExpressionInternal inner = baseUnaryExpression.getOperand().visit(this); | ||
|
||
if (inner instanceof ValueExpression) { | ||
return new ValueExpression( | ||
baseUnaryExpression.expressionInterval(), | ||
baseUnaryExpression.expressionText(), | ||
baseUnaryExpression.evaluate(EvaluationRuntime.getDefault(), ((ValueExpression) inner).getValue(), FailFastExceptionThrower.getInstance()) | ||
); | ||
} | ||
|
||
baseUnaryExpression.setOperand(inner); | ||
return baseUnaryExpression; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
sql/src/main/java/io/cloudevents/sql/impl/ParserBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package io.cloudevents.sql.impl; | ||
|
||
import io.cloudevents.sql.Parser; | ||
|
||
public class ParserBuilder { | ||
|
||
private boolean constantFolding; | ||
|
||
public ParserBuilder() { | ||
this.constantFolding = true; | ||
} | ||
|
||
/** | ||
* Disable constant folding when parsing. | ||
* | ||
* @return this | ||
*/ | ||
public ParserBuilder disableConstantFolding() { | ||
this.constantFolding = false; | ||
return this; | ||
} | ||
|
||
/** | ||
* @return the new {@link Parser} | ||
*/ | ||
public Parser build() { | ||
return new ParserImpl(this.constantFolding); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
sql/src/main/java/io/cloudevents/sql/impl/expressions/BaseUnaryExpression.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package io.cloudevents.sql.impl.expressions; | ||
|
||
import io.cloudevents.CloudEvent; | ||
import io.cloudevents.sql.EvaluationRuntime; | ||
import io.cloudevents.sql.impl.ExceptionThrower; | ||
import org.antlr.v4.runtime.misc.Interval; | ||
|
||
public abstract class BaseUnaryExpression extends BaseExpression { | ||
|
||
protected ExpressionInternal internal; | ||
|
||
public BaseUnaryExpression(Interval expressionInterval, String expressionText, ExpressionInternal internal) { | ||
super(expressionInterval, expressionText); | ||
this.internal = internal; | ||
} | ||
|
||
public abstract Object evaluate(EvaluationRuntime runtime, Object value, ExceptionThrower exceptions); | ||
|
||
@Override | ||
public Object evaluate(EvaluationRuntime runtime, CloudEvent event, ExceptionThrower thrower) { | ||
return evaluate(runtime, internal.evaluate(runtime, event, thrower), thrower); | ||
} | ||
|
||
@Override | ||
public <T> T visit(ExpressionInternalVisitor<T> visitor) { | ||
return visitor.visitBaseUnaryExpression(this); | ||
} | ||
|
||
public ExpressionInternal getOperand() { | ||
return internal; | ||
} | ||
|
||
public BaseUnaryExpression setOperand(ExpressionInternal internal) { | ||
this.internal = internal; | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.