Skip to content

Commit

Permalink
🦄 refactor: Redesign ast 33
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Dec 5, 2024
1 parent bd432a6 commit 1c0c926
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2024. caoccao.com Sam Cao
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.caoccao.javet.buddy.ts2java.ast.expr;

import com.caoccao.javet.buddy.ts2java.ast.interfaces.ITs2JavaAst;
import com.caoccao.javet.buddy.ts2java.ast.interfaces.abilities.ITs2JavaBangFlippable;
import com.caoccao.javet.buddy.ts2java.ast.memo.Ts2JavaMemoFunction;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstBinExpr;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.jar.asm.Label;

public abstract class Ts2JavaAstBinExprLogical extends Ts2JavaAstBinExpr
implements ITs2JavaBangFlippable {
protected Label labelFalse;
protected Label labelTrue;

protected Ts2JavaAstBinExprLogical(
ITs2JavaAst<?, ?> parent,
Swc4jAstBinExpr ast,
Ts2JavaMemoFunction memo) {
super(parent, ast, memo);
labelFalse = null;
labelTrue = null;
type = TypeDescription.ForLoadedType.of(boolean.class);
}

@Override
public void compile() {
super.compile();
if (labelFalse == null) {
labelFalse = new Label();
}
}

@Override
public void flipBang() {
op = op.getOppositeOperator();
}

public Label getLabelFalse() {
return labelFalse;
}

public Label getLabelTrue() {
return labelTrue;
}

@Override
public boolean isBangFlippable() {
return true;
}

public Ts2JavaAstBinExprLogical setLabelFalse(Label labelFalse) {
this.labelFalse = labelFalse;
return this;
}

public Ts2JavaAstBinExprLogical setLabelTrue(Label labelTrue) {
this.labelTrue = labelTrue;
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import com.caoccao.javet.buddy.ts2java.ast.enums.Ts2JavaAstBinaryOp;
import com.caoccao.javet.buddy.ts2java.ast.interfaces.ITs2JavaAst;
import com.caoccao.javet.buddy.ts2java.ast.interfaces.abilities.ITs2JavaBangFlippable;
import com.caoccao.javet.buddy.ts2java.ast.memo.Ts2JavaMemoFunction;
import com.caoccao.javet.buddy.ts2java.compiler.JavaClassCast;
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstBinaryOp;
Expand All @@ -30,20 +29,15 @@
import net.bytebuddy.jar.asm.MethodVisitor;
import net.bytebuddy.jar.asm.Opcodes;

import java.util.Objects;
import java.util.Optional;

public class Ts2JavaAstBinExprLogicalCompare extends Ts2JavaAstBinExpr
implements ITs2JavaBangFlippable {
protected Label label;
public class Ts2JavaAstBinExprLogicalCompare extends Ts2JavaAstBinExprLogical {

protected Ts2JavaAstBinExprLogicalCompare(
ITs2JavaAst<?, ?> parent,
Swc4jAstBinExpr ast,
Ts2JavaMemoFunction memo) {
super(parent, ast, memo);
label = new Label();
type = TypeDescription.ForLoadedType.of(boolean.class);
}

@Override
Expand All @@ -63,38 +57,19 @@ public Size apply(MethodVisitor methodVisitor, Implementation.Context context) {
Size sizeCastRight = JavaClassCast.getUpCastStackManipulation(right.getType(), upCastType)
.map(s -> s.apply(methodVisitor, context))
.orElse(Size.ZERO);
Size sizeOp = Ts2JavaAstBinaryOp.getLogicalCompareStackManipulation(ast, op, upCastType, label)
Size sizeOp = Ts2JavaAstBinaryOp.getLogicalCompareStackManipulation(ast, op, upCastType, labelFalse)
.apply(methodVisitor, context);
if (!(parent instanceof Ts2JavaAstBinExpr)) {
// This is the top bin expr. Let's close it.
Label labelClose = new Label();
methodVisitor.visitInsn(Opcodes.ICONST_1);
methodVisitor.visitJumpInsn(Opcodes.GOTO, labelClose);
methodVisitor.visitLabel(label);
methodVisitor.visitLabel(labelFalse);
methodVisitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
methodVisitor.visitInsn(Opcodes.ICONST_0);
methodVisitor.visitLabel(labelClose);
methodVisitor.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[]{Opcodes.INTEGER});
}
return aggregateSize(sizeLoadLeft, sizeCastLeft, sizeLoadRight, sizeCastRight, sizeOp);
}

@Override
public void flipBang() {
op = op.getOppositeOperator();
}

public Label getLabel() {
return label;
}

@Override
public boolean isBangFlippable() {
return true;
}

public Ts2JavaAstBinExprLogicalCompare setLabel(Label label) {
this.label = Objects.requireNonNull(label);
return this;
}
}

0 comments on commit 1c0c926

Please sign in to comment.