Skip to content

Commit

Permalink
🦄 refactor: Redesign ast 30
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Dec 3, 2024
1 parent 18ef4bd commit 97783bd
Show file tree
Hide file tree
Showing 29 changed files with 233 additions and 54 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/caoccao/javet/buddy/ts2java/Ts2Java.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void transpile() throws Swc4jCoreException {
for (Swc4jAstClassDecl classDecl : classDecls) {
DynamicType.Builder<?> builder = new ByteBuddy()
.subclass(Object.class, ConstructorStrategy.Default.DEFAULT_CONSTRUCTOR);
Ts2JavaAstClassDecl ts2JavaAstClassDecl = new Ts2JavaAstClassDecl(
Ts2JavaAstClassDecl ts2JavaAstClassDecl = Ts2JavaAstClassDecl.create(
null,
classDecl,
new Ts2JavaMemoDynamicType(builder),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,23 @@ public class Ts2JavaAstClass
extends BaseTs2JavaAst<Swc4jAstClass, Ts2JavaMemoDynamicType> {
protected final List<ITs2JavaAstClassMember<?, ?>> body;

public Ts2JavaAstClass(ITs2JavaAst<?, ?> parent, Swc4jAstClass ast, Ts2JavaMemoDynamicType memo) {
protected Ts2JavaAstClass(
ITs2JavaAst<?, ?> parent,
Swc4jAstClass ast,
Ts2JavaMemoDynamicType memo) {
super(parent, ast, memo);
body = ast.getBody().stream()
.map(classMember -> ITs2JavaAstClassMember.create(this, classMember, memo))
.collect(Collectors.toList());
}

public static Ts2JavaAstClass create(
ITs2JavaAst<?, ?> parent,
Swc4jAstClass ast,
Ts2JavaMemoDynamicType memo) {
return new Ts2JavaAstClass(parent, ast, memo);
}

@Override
public Size apply(MethodVisitor methodVisitor, Implementation.Context context) {
return body.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class Ts2JavaAstClassMethod
implements ITs2JavaAstClassMember<Swc4jAstClassMethod, Ts2JavaMemoDynamicType> {
protected final Ts2JavaAstFunction function;

public Ts2JavaAstClassMethod(
protected Ts2JavaAstClassMethod(
ITs2JavaAst<?, ?> parent,
Swc4jAstClassMethod ast,
Ts2JavaMemoDynamicType memo) {
Expand All @@ -56,14 +56,20 @@ public Ts2JavaAstClassMethod(
ast,
SimpleFreeMarkerFormat.format("ClassMethod key type ${keyType} is not supported.",
SimpleMap.of("keyType", ast.getKey().getClass().getSimpleName())));

}
String name = ast.getKey().as(Swc4jAstIdentName.class).getSym();
boolean _static = ast.isStatic();
Swc4jAstAccessibility accessibility = ast.getAccessibility().orElse(Swc4jAstAccessibility.Public);
function = new Ts2JavaAstFunction(this, ast.getFunction(), memo, name, _static, accessibility);
}

public static Ts2JavaAstClassMethod create(
ITs2JavaAst<?, ?> parent,
Swc4jAstClassMethod ast,
Ts2JavaMemoDynamicType memo) {
return new Ts2JavaAstClassMethod(parent, ast, memo);
}

@Override
public Size apply(MethodVisitor methodVisitor, Implementation.Context context) {
return function.apply(methodVisitor, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public Ts2JavaAstFunction(
.setStatic(_static)
.setReturnType(type);
this.name = name;
body = ast.getBody().map(stmt -> new Ts2JavaAstBlockStmt(this, stmt, memoFunction));
body = ast.getBody().map(stmt -> Ts2JavaAstBlockStmt.create(this, stmt, memoFunction));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstBinExpr;
import com.caoccao.javet.utils.SimpleFreeMarkerFormat;
import com.caoccao.javet.utils.SimpleMap;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.jar.asm.MethodVisitor;

Expand All @@ -36,16 +35,12 @@ public abstract class Ts2JavaAstBinExpr
protected final ITs2JavaAstExpr<?, ?> right;
protected Swc4jAstBinaryOp op;

public Ts2JavaAstBinExpr(
protected Ts2JavaAstBinExpr(
ITs2JavaAst<?, ?> parent,
Swc4jAstBinExpr ast,
Ts2JavaMemoFunction memo) {
super(parent, ast, memo);
op = ast.getOp();
// TODO
if (op.isLogicalOperator()) {
type = TypeDescription.ForLoadedType.of(boolean.class);
}
left = ITs2JavaAstExpr.create(parent, ast.getLeft(), memo);
right = ITs2JavaAstExpr.create(parent, ast.getRight(), memo);
}
Expand All @@ -54,8 +49,11 @@ public static Ts2JavaAstBinExpr create(
ITs2JavaAst<?, ?> parent,
Swc4jAstBinExpr ast,
Ts2JavaMemoFunction memo) {
if (ast.getOp().isArithmeticOperator()) {
Swc4jAstBinaryOp op = ast.getOp();
if (op.isArithmeticOperator()) {
return new Ts2JavaAstBinExprArithmetic(parent, ast, memo);
} else if (op.isLogicalCompareOperator()) {
return new Ts2JavaAstBinExprLogicalCompare(parent, ast, memo);
}
throw new Ts2JavaAstException(
ast,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import java.util.Optional;

public class Ts2JavaAstBinExprArithmetic extends Ts2JavaAstBinExpr {
public Ts2JavaAstBinExprArithmetic(
protected Ts2JavaAstBinExprArithmetic(
ITs2JavaAst<?, ?> parent,
Swc4jAstBinExpr ast,
Ts2JavaMemoFunction memo) {
Expand All @@ -44,20 +44,20 @@ public Ts2JavaAstBinExprArithmetic(
public Size apply(MethodVisitor methodVisitor, Implementation.Context context) {
super.apply(methodVisitor, context);
Size sizeLoadLeft = left.apply(methodVisitor, context);
TypeDescription newType = left.getType();
TypeDescription upCastType = left.getType();
Size sizeCastLeft = Size.ZERO;
TypeDescription rightTargetType = op == Swc4jAstBinaryOp.Exp ? type : right.getType();
Optional<StackManipulation> optionalStackManipulation =
JavaClassCast.getUpCastStackManipulation(left.getType(), rightTargetType);
if (optionalStackManipulation.isPresent()) {
sizeCastLeft = optionalStackManipulation.get().apply(methodVisitor, context);
newType = rightTargetType;
upCastType = rightTargetType;
}
Size sizeLoadRight = right.apply(methodVisitor, context);
Size sizeCastRight = JavaClassCast.getUpCastStackManipulation(right.getType(), newType)
Size sizeCastRight = JavaClassCast.getUpCastStackManipulation(right.getType(), upCastType)
.map(s -> s.apply(methodVisitor, context))
.orElse(Size.ZERO);
Size sizeOp = Ts2JavaAstBinaryOp.getArithmeticStackManipulation(op, newType).apply(methodVisitor, context);
Size sizeOp = Ts2JavaAstBinaryOp.getArithmeticStackManipulation(op, upCastType).apply(methodVisitor, context);
// TODO
// } else if (op.isLogicalOperator()) {
// switch (op) {
Expand All @@ -74,7 +74,7 @@ public Size apply(MethodVisitor methodVisitor, Implementation.Context context) {
// SimpleFreeMarkerFormat.format("Bin expr op ${op} is not supported.",
// SimpleMap.of("op", op.name())));
// }
Size sizeCastResult = JavaClassCast.getUpCastStackManipulation(newType, type)
Size sizeCastResult = JavaClassCast.getUpCastStackManipulation(upCastType, type)
.map(s -> s.apply(methodVisitor, context))
.orElse(Size.ZERO);
return aggregateSize(sizeLoadLeft, sizeCastLeft, sizeLoadRight, sizeCastRight, sizeOp, sizeCastResult);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.enums.Ts2JavaAstBinaryOp;
import com.caoccao.javet.buddy.ts2java.ast.interfaces.ITs2JavaAst;
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;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstBinExpr;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.implementation.bytecode.StackManipulation;
import net.bytebuddy.jar.asm.MethodVisitor;

import java.util.Optional;

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

@Override
public Size apply(MethodVisitor methodVisitor, Implementation.Context context) {
super.apply(methodVisitor, context);
Size sizeLoadLeft = left.apply(methodVisitor, context);
TypeDescription upCastType = left.getType();
Size sizeCastLeft = Size.ZERO;
TypeDescription rightTargetType = op == Swc4jAstBinaryOp.Exp ? type : right.getType();
Optional<StackManipulation> optionalStackManipulation =
JavaClassCast.getUpCastStackManipulation(left.getType(), rightTargetType);
if (optionalStackManipulation.isPresent()) {
sizeCastLeft = optionalStackManipulation.get().apply(methodVisitor, context);
upCastType = rightTargetType;
}
Size sizeLoadRight = right.apply(methodVisitor, context);
Size sizeCastRight = JavaClassCast.getUpCastStackManipulation(right.getType(), upCastType)
.map(s -> s.apply(methodVisitor, context))
.orElse(Size.ZERO);
// TODO
Size sizeOp = Ts2JavaAstBinaryOp.getArithmeticStackManipulation(op, upCastType).apply(methodVisitor, context);
return aggregateSize(sizeLoadLeft, sizeCastLeft, sizeLoadRight, sizeCastRight, sizeOp);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class Ts2JavaAstIdent
protected final String sym;
protected JavaLocalVariable localVariable;

public Ts2JavaAstIdent(
protected Ts2JavaAstIdent(
ITs2JavaAst<?, ?> parent,
Swc4jAstIdent ast,
Ts2JavaMemoFunction memo) {
Expand All @@ -53,6 +53,13 @@ public Ts2JavaAstIdent(
sym = ast.getSym();
}

public static Ts2JavaAstIdent create(
ITs2JavaAst<?, ?> parent,
Swc4jAstIdent ast,
Ts2JavaMemoFunction memo) {
return new Ts2JavaAstIdent(parent, ast, memo);
}

@Override
public Size apply(MethodVisitor methodVisitor, Implementation.Context context) {
visitLineNumber(methodVisitor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class Ts2JavaAstIdentName
ITs2JavaAstJsxAttrName<Swc4jAstIdentName, Ts2JavaMemoFunction> {
protected final String sym;

public Ts2JavaAstIdentName(
protected Ts2JavaAstIdentName(
ITs2JavaAst<?, ?> parent,
Swc4jAstIdentName ast,
TypeDescription type,
Expand All @@ -42,6 +42,14 @@ public Ts2JavaAstIdentName(
this.type = type;
}

public static Ts2JavaAstIdentName create(
ITs2JavaAst<?, ?> parent,
Swc4jAstIdentName ast,
TypeDescription type,
Ts2JavaMemoFunction memo) {
return new Ts2JavaAstIdentName(parent, ast, type, memo);
}

@Override
public Size apply(MethodVisitor methodVisitor, Implementation.Context context) {
visitLineNumber(methodVisitor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,19 @@ public class Ts2JavaAstParenExpr
ITs2JavaAstSimpleAssignTarget<Swc4jAstParenExpr, Ts2JavaMemoFunction> {
protected final ITs2JavaAstExpr<?, ?> expr;

public Ts2JavaAstParenExpr(
protected Ts2JavaAstParenExpr(
ITs2JavaAst<?, ?> parent,
Swc4jAstParenExpr ast,
TypeDescription type,
Ts2JavaMemoFunction memo) {
super(parent, ast, memo);
expr = ITs2JavaAstExpr.create(this, ast.getExpr(), memo);
this.type = type;
}

public static Ts2JavaAstParenExpr create(
ITs2JavaAst<?, ?> parent,
Swc4jAstParenExpr ast,
Ts2JavaMemoFunction memo) {
return new Ts2JavaAstParenExpr(parent, ast, memo);
}

@Override
Expand All @@ -51,6 +56,7 @@ public Size apply(MethodVisitor methodVisitor, Implementation.Context context) {
@Override
public void compile() {
expr.compile();
type = expr.getType();
}

public ITs2JavaAstExpr<?, ?> getExpr() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class Ts2JavaAstUnaryExpr
protected final ITs2JavaAstExpr<?, ?> arg;
protected Swc4jAstUnaryOp op;

public Ts2JavaAstUnaryExpr(
protected Ts2JavaAstUnaryExpr(
ITs2JavaAst<?, ?> parent,
Swc4jAstUnaryExpr ast,
Ts2JavaMemoFunction memo) {
Expand All @@ -47,6 +47,13 @@ public Ts2JavaAstUnaryExpr(
op = ast.getOp();
}

public static Ts2JavaAstUnaryExpr create(
ITs2JavaAst<?, ?> parent,
Swc4jAstUnaryExpr ast,
Ts2JavaMemoFunction memo) {
return new Ts2JavaAstUnaryExpr(parent, ast, memo);
}

@Override
public Size apply(MethodVisitor methodVisitor, Implementation.Context context) {
visitLineNumber(methodVisitor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
import com.caoccao.javet.buddy.ts2java.ast.interfaces.ITs2JavaAstTsLit;
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.exceptions.Ts2JavaAstException;
import com.caoccao.javet.swc4j.ast.expr.lit.Swc4jAstBool;
import com.caoccao.javet.utils.SimpleFreeMarkerFormat;
import com.caoccao.javet.utils.SimpleMap;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.implementation.bytecode.StackManipulation;
Expand All @@ -38,7 +35,7 @@ public class Ts2JavaAstBool
ITs2JavaAstLit<Swc4jAstBool, Ts2JavaMemoFunction>, ITs2JavaAstTsLit<Swc4jAstBool, Ts2JavaMemoFunction> {
protected boolean value;

public Ts2JavaAstBool(
protected Ts2JavaAstBool(
ITs2JavaAst<?, ?> parent,
Swc4jAstBool ast,
Ts2JavaMemoFunction memo) {
Expand All @@ -47,6 +44,13 @@ public Ts2JavaAstBool(
type = TypeDescription.ForLoadedType.of(boolean.class);
}

public static Ts2JavaAstBool create(
ITs2JavaAst<?, ?> parent,
Swc4jAstBool ast,
Ts2JavaMemoFunction memo) {
return new Ts2JavaAstBool(parent, ast, memo);
}

@Override
public Size apply(MethodVisitor methodVisitor, Implementation.Context context) {
visitLineNumber(methodVisitor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,21 @@ public class Ts2JavaAstNumber
ITs2JavaAstTsLit<Swc4jAstNumber, Ts2JavaMemoFunction> {
protected boolean negative;

public Ts2JavaAstNumber(
protected Ts2JavaAstNumber(
ITs2JavaAst<?, ?> parent,
Swc4jAstNumber ast,
Ts2JavaMemoFunction memo) {
super(parent, ast, memo);
negative = false;
}

public static Ts2JavaAstNumber create(
ITs2JavaAst<?, ?> parent,
Swc4jAstNumber ast,
Ts2JavaMemoFunction memo) {
return new Ts2JavaAstNumber(parent, ast, memo);
}

@Override
public Size apply(MethodVisitor methodVisitor, Implementation.Context context) {
visitLineNumber(methodVisitor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public interface ITs2JavaAstClassMember<AST extends ISwc4jAstClassMember, Memo e
Ts2JavaMemoDynamicType memo) {
switch (ast.getType()) {
case ClassMethod:
return new Ts2JavaAstClassMethod(parent, ast.as(Swc4jAstClassMethod.class), memo);
return Ts2JavaAstClassMethod.create(parent, ast.as(Swc4jAstClassMethod.class), memo);
default:
throw new Ts2JavaAstException(
ast,
Expand Down
Loading

0 comments on commit 97783bd

Please sign in to comment.