Skip to content

Commit

Permalink
🦄 refactor: Add bang count cache for bin expr
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Nov 20, 2024
1 parent a68841e commit 21e58ba
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 34 deletions.
57 changes: 37 additions & 20 deletions src/main/java/com/caoccao/javet/swc4j/ast/expr/Swc4jAstBinExpr.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@
public class Swc4jAstBinExpr
extends Swc4jAst
implements ISwc4jAstExpr {
/**
* The Bang count is a local cache of the bang count through the AST.
*
* @since 1.3.0
*/
@Jni2RustField(ignore = true)
protected Optional<Integer> bangCount;
@Jni2RustField(box = true)
protected ISwc4jAstExpr left;
protected Swc4jAstBinaryOp op;
Expand All @@ -57,6 +64,7 @@ public Swc4jAstBinExpr(
ISwc4jAstExpr right,
Swc4jSpan span) {
super(span);
resetBangCount();
setLeft(left);
setOp(op);
setRight(right);
Expand All @@ -66,25 +74,6 @@ public static Swc4jAstBinExpr create(Swc4jAstBinaryOp op, ISwc4jAstExpr left, IS
return new Swc4jAstBinExpr(op, left, right, Swc4jSpan.DUMMY);
}

protected static int getBangCount(ISwc4jAst ast) {
switch (ast.getType()) {
case BinExpr:
if (ast.as(Swc4jAstBinExpr.class).getOp().isLogicalOperator()) {
return getBangCount(ast.getParent());
}
return 0;
case ParenExpr:
return getBangCount(ast.getParent());
case UnaryExpr:
if (ast.as(Swc4jAstUnaryExpr.class).getOp() == Swc4jAstUnaryOp.Bang) {
return getBangCount(ast.getParent()) + 1;
}
return 0;
default:
return 0;
}
}

@Override
public Optional<ISwc4jAst> eval() {
ISwc4jAstExpr left = this.left.unParenExpr();
Expand Down Expand Up @@ -230,8 +219,31 @@ public Optional<ISwc4jAst> eval() {
return super.eval();
}

protected int getBangCount(ISwc4jAst ast) {
switch (ast.getType()) {
case BinExpr:
Swc4jAstBinExpr binExpr = ast.as(Swc4jAstBinExpr.class);
if (binExpr.getOp().isLogicalOperator()) {
return binExpr.getBangCount();
}
return 0;
case ParenExpr:
return getBangCount(ast.getParent());
case UnaryExpr:
if (ast.as(Swc4jAstUnaryExpr.class).getOp() == Swc4jAstUnaryOp.Bang) {
return getBangCount(ast.getParent()) + 1;
}
return 0;
default:
return 0;
}
}

public int getBangCount() {
return getBangCount(getParent());
if (!bangCount.isPresent()) {
bangCount = Optional.of(getBangCount(getParent()));
}
return bangCount.get();
}

@Override
Expand Down Expand Up @@ -272,6 +284,11 @@ public boolean replaceNode(ISwc4jAst oldNode, ISwc4jAst newNode) {
return false;
}

public Swc4jAstBinExpr resetBangCount() {
bangCount = Optional.empty();
return this;
}

public Swc4jAstBinExpr setLeft(ISwc4jAstExpr left) {
this.left = AssertionUtils.notNull(left, "Left");
this.left.setParent(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,6 @@ public static Swc4jAstNumber createNaN() {
return create(Double.NaN, null);
}

protected static int getMinusCount(ISwc4jAst ast) {
switch (ast.getType()) {
case ParenExpr:
return getMinusCount(ast.getParent());
case UnaryExpr:
if (ast.as(Swc4jAstUnaryExpr.class).getOp() == Swc4jAstUnaryOp.Minus) {
return getMinusCount(ast.getParent()) + 1;
}
return 0;
default:
return 0;
}
}

protected static String normalize(String raw) {
Matcher matcher = PATTERN_SCIENTIFIC_NOTATION_WITH_FRACTION.matcher(raw);
if (matcher.matches()) {
Expand Down Expand Up @@ -192,6 +178,20 @@ public List<ISwc4jAst> getChildNodes() {
return EMPTY_CHILD_NODES;
}

protected int getMinusCount(ISwc4jAst ast) {
switch (ast.getType()) {
case ParenExpr:
return getMinusCount(ast.getParent());
case UnaryExpr:
if (ast.as(Swc4jAstUnaryExpr.class).getOp() == Swc4jAstUnaryOp.Minus) {
return getMinusCount(ast.getParent()) + 1;
}
return 0;
default:
return 0;
}
}

public int getMinusCount() {
return getMinusCount(getParent());
}
Expand Down

0 comments on commit 21e58ba

Please sign in to comment.