-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🦄 refactor: Add unary expr bang, unary expr minus
- Loading branch information
Showing
3 changed files
with
146 additions
and
80 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
68 changes: 68 additions & 0 deletions
68
src/main/java/com/caoccao/javet/buddy/ts2java/ast/expr/Ts2JavaAstUnaryExprBang.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,68 @@ | ||
/* | ||
* 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.enums.Swc4jAstUnaryOp; | ||
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstUnaryExpr; | ||
import net.bytebuddy.implementation.Implementation; | ||
import net.bytebuddy.jar.asm.MethodVisitor; | ||
|
||
public class Ts2JavaAstUnaryExprBang | ||
extends Ts2JavaAstUnaryExpr | ||
implements ITs2JavaBangFlippable { | ||
protected Ts2JavaAstUnaryExprBang( | ||
ITs2JavaAst<?, ?> parent, | ||
Swc4jAstUnaryExpr ast, | ||
Ts2JavaMemoFunction memo) { | ||
super(parent, ast, memo); | ||
op = Swc4jAstUnaryOp.Bang; | ||
} | ||
|
||
@Override | ||
public Size apply(MethodVisitor methodVisitor, Implementation.Context context) { | ||
Size size = super.apply(methodVisitor, context); | ||
if (!isBangFlippable()) { | ||
final int opcode = getOpcodeNegative(); | ||
methodVisitor.visitInsn(opcode); | ||
} | ||
return size; | ||
} | ||
|
||
@Override | ||
public void compile() { | ||
super.compile(); | ||
flipBang(); | ||
} | ||
|
||
@Override | ||
public void flipBang() { | ||
if (isBangFlippable()) { | ||
arg.as(ITs2JavaBangFlippable.class).flipBang(); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isBangFlippable() { | ||
if (arg instanceof ITs2JavaBangFlippable) { | ||
return arg.as(ITs2JavaBangFlippable.class).isBangFlippable(); | ||
} | ||
return false; | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/main/java/com/caoccao/javet/buddy/ts2java/ast/expr/Ts2JavaAstUnaryExprMinus.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,68 @@ | ||
/* | ||
* 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.ITs2JavaMinusFlippable; | ||
import com.caoccao.javet.buddy.ts2java.ast.memo.Ts2JavaMemoFunction; | ||
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstUnaryOp; | ||
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstUnaryExpr; | ||
import net.bytebuddy.implementation.Implementation; | ||
import net.bytebuddy.jar.asm.MethodVisitor; | ||
|
||
public class Ts2JavaAstUnaryExprMinus | ||
extends Ts2JavaAstUnaryExpr | ||
implements ITs2JavaMinusFlippable { | ||
protected Ts2JavaAstUnaryExprMinus( | ||
ITs2JavaAst<?, ?> parent, | ||
Swc4jAstUnaryExpr ast, | ||
Ts2JavaMemoFunction memo) { | ||
super(parent, ast, memo); | ||
op = Swc4jAstUnaryOp.Minus; | ||
} | ||
|
||
@Override | ||
public Size apply(MethodVisitor methodVisitor, Implementation.Context context) { | ||
Size size = super.apply(methodVisitor, context); | ||
if (!isMinusFlippable()) { | ||
final int opcode = getOpcodeNegative(); | ||
methodVisitor.visitInsn(opcode); | ||
} | ||
return size; | ||
} | ||
|
||
@Override | ||
public void compile() { | ||
super.compile(); | ||
flipMinus(); | ||
} | ||
|
||
@Override | ||
public void flipMinus() { | ||
if (isMinusFlippable()) { | ||
arg.as(ITs2JavaMinusFlippable.class).flipMinus(); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isMinusFlippable() { | ||
if (arg instanceof ITs2JavaMinusFlippable) { | ||
return arg.as(ITs2JavaMinusFlippable.class).isMinusFlippable(); | ||
} | ||
return false; | ||
} | ||
} |