Skip to content

Commit

Permalink
🦄 refactor: Redesign ast 29
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Dec 2, 2024
1 parent 8530a8a commit 18ef4bd
Show file tree
Hide file tree
Showing 16 changed files with 38 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class Ts2JavaAstClass
public Ts2JavaAstClass(ITs2JavaAst<?, ?> parent, Swc4jAstClass ast, Ts2JavaMemoDynamicType memo) {
super(parent, ast, memo);
body = ast.getBody().stream()
.map(classMember -> ITs2JavaAstClassMember.cast(this, classMember, memo))
.map(classMember -> ITs2JavaAstClassMember.create(this, classMember, memo))
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
import com.caoccao.javet.buddy.ts2java.ast.interfaces.ITs2JavaAst;
import com.caoccao.javet.buddy.ts2java.ast.interfaces.ITs2JavaAstExpr;
import com.caoccao.javet.buddy.ts2java.ast.memo.Ts2JavaMemoFunction;
import com.caoccao.javet.buddy.ts2java.exceptions.Ts2JavaAstException;
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstBinaryOp;
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 @@ -43,8 +46,21 @@ public Ts2JavaAstBinExpr(
if (op.isLogicalOperator()) {
type = TypeDescription.ForLoadedType.of(boolean.class);
}
left = ITs2JavaAstExpr.cast(parent, ast.getLeft(), memo);
right = ITs2JavaAstExpr.cast(parent, ast.getRight(), memo);
left = ITs2JavaAstExpr.create(parent, ast.getLeft(), memo);
right = ITs2JavaAstExpr.create(parent, ast.getRight(), memo);
}

public static Ts2JavaAstBinExpr create(
ITs2JavaAst<?, ?> parent,
Swc4jAstBinExpr ast,
Ts2JavaMemoFunction memo) {
if (ast.getOp().isArithmeticOperator()) {
return new Ts2JavaAstBinExprArithmetic(parent, ast, memo);
}
throw new Ts2JavaAstException(
ast,
SimpleFreeMarkerFormat.format("Bin expr op ${op} is not supported.",
SimpleMap.of("op", ast.getOp().name())));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public Ts2JavaAstParenExpr(
TypeDescription type,
Ts2JavaMemoFunction memo) {
super(parent, ast, memo);
expr = ITs2JavaAstExpr.cast(this, ast.getExpr(), memo);
expr = ITs2JavaAstExpr.create(this, ast.getExpr(), memo);
this.type = type;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstUnaryExpr;
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;
import net.bytebuddy.jar.asm.Opcodes;
Expand All @@ -44,7 +43,7 @@ public Ts2JavaAstUnaryExpr(
Swc4jAstUnaryExpr ast,
Ts2JavaMemoFunction memo) {
super(parent, ast, memo);
arg = ITs2JavaAstExpr.cast(this, ast.getArg(), memo);
arg = ITs2JavaAstExpr.create(this, ast.getArg(), memo);
op = ast.getOp();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

public interface ITs2JavaAstClassMember<AST extends ISwc4jAstClassMember, Memo extends Ts2JavaMemoDynamicType>
extends ITs2JavaAst<AST, Memo> {
static ITs2JavaAstClassMember<?, ?> cast(
static ITs2JavaAstClassMember<?, ?> create(
ITs2JavaAst<?, ?> parent,
ISwc4jAstClassMember ast,
Ts2JavaMemoDynamicType memo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

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

import com.caoccao.javet.buddy.ts2java.ast.expr.Ts2JavaAstBinExprArithmetic;
import com.caoccao.javet.buddy.ts2java.ast.expr.Ts2JavaAstBinExpr;
import com.caoccao.javet.buddy.ts2java.ast.expr.Ts2JavaAstIdent;
import com.caoccao.javet.buddy.ts2java.ast.expr.Ts2JavaAstUnaryExpr;
import com.caoccao.javet.buddy.ts2java.ast.expr.lit.Ts2JavaAstBool;
Expand All @@ -37,29 +37,21 @@
public interface ITs2JavaAstExpr<AST extends ISwc4jAstExpr, Memo extends Ts2JavaMemo>
extends ITs2JavaAstVarDeclOrExpr<AST, Memo>, ITs2JavaAstPat<AST, Memo>, ITs2JavaAstJsxExpr<AST, Memo>,
ITs2JavaAstCallee<AST, Memo>, ITs2JavaAstBlockStmtOrExpr<AST, Memo>, ITs2JavaAstAssignTarget<AST, Memo> {
static ITs2JavaAstExpr<?, ?> cast(
static ITs2JavaAstExpr<?, ?> create(
ITs2JavaAst<?, ?> parent,
ISwc4jAstExpr ast,
Ts2JavaMemoFunction memo) {
switch (ast.getType()) {
case BinExpr: {
Swc4jAstBinExpr binExpr = ast.as(Swc4jAstBinExpr.class);
if (binExpr.getOp().isArithmeticOperator()) {
return new Ts2JavaAstBinExprArithmetic(parent, binExpr, memo);
}
throw new Ts2JavaAstException(
ast,
SimpleFreeMarkerFormat.format("Bin expr op ${op} is not supported.",
SimpleMap.of("op", binExpr.getOp().name())));
}
case BinExpr:
return Ts2JavaAstBinExpr.create(parent, ast.as(Swc4jAstBinExpr.class), memo);
case Bool:
return new Ts2JavaAstBool(parent, ast.as(Swc4jAstBool.class), memo);
case Ident:
return new Ts2JavaAstIdent(parent, ast.as(Swc4jAstIdent.class), memo);
case Number:
return new Ts2JavaAstNumber(parent, ast.as(Swc4jAstNumber.class), memo);
case ParenExpr:
return cast(parent, ast.as(Swc4jAstParenExpr.class).unParenExpr(), memo);
return create(parent, ast.as(Swc4jAstParenExpr.class).unParenExpr(), memo);
case UnaryExpr:
return new Ts2JavaAstUnaryExpr(parent, ast.as(Swc4jAstUnaryExpr.class), memo);
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

public interface ITs2JavaAstPat<AST extends ISwc4jAstPat, Memo extends Ts2JavaMemo>
extends ITs2JavaAstAssignTarget<AST, Memo>, ITs2JavaAstForHead<AST, Memo> {
static ITs2JavaAstPat<?, ?> cast(
static ITs2JavaAstPat<?, ?> create(
ITs2JavaAst<?, ?> parent,
ISwc4jAstPat ast,
Ts2JavaMemoFunction memo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

public interface ITs2JavaAstStmt<AST extends ISwc4jAstStmt, Memo extends Ts2JavaMemo>
extends ITs2JavaAstModuleItem<AST, Memo> {
static ITs2JavaAstStmt<?, ?> cast(
static ITs2JavaAstStmt<?, ?> create(
ITs2JavaAst<?, ?> parent,
ISwc4jAstStmt ast,
Ts2JavaMemoFunction memo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

public interface ITs2JavaAstTsEntityName<AST extends ISwc4jAstTsEntityName, Memo extends Ts2JavaMemo>
extends ITs2JavaAstTsModuleRef<AST, Memo>, ITs2JavaAstTsTypeQueryExpr<AST, Memo> {
static ITs2JavaAstTsEntityName<?, ?> cast(
static ITs2JavaAstTsEntityName<?, ?> create(
ITs2JavaAst<?, ?> parent,
ISwc4jAstTsEntityName ast,
Ts2JavaMemoFunction memo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

public interface ITs2JavaAstTsType<AST extends ISwc4jAstTsType, Memo extends Ts2JavaMemo>
extends ITs2JavaAst<AST, Memo> {
static ITs2JavaAstTsType<?, ?> cast(
static ITs2JavaAstTsType<?, ?> create(
ITs2JavaAst<?, ?> parent,
ISwc4jAstTsType ast,
Ts2JavaMemoFunction memo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public Ts2JavaAstBlockStmt(
Ts2JavaMemoFunction memo) {
super(parent, ast, memo);
stmts = ast.getStmts().stream()
.map(stmt -> ITs2JavaAstStmt.cast(this, stmt, memo))
.map(stmt -> ITs2JavaAstStmt.create(this, stmt, memo))
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public Ts2JavaAstReturnStmt(
Swc4jAstReturnStmt ast,
Ts2JavaMemoFunction memo) {
super(parent, ast, memo);
arg = ast.getArg().map(arg -> ITs2JavaAstExpr.cast(this, arg, memo));
arg = ast.getArg().map(arg -> ITs2JavaAstExpr.create(this, arg, memo));
type = memo.getReturnType();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ public Ts2JavaAstVarDeclarator(
Swc4jAstVarDeclarator ast,
Ts2JavaMemoFunction memo) {
super(parent, ast, memo);
name = ITs2JavaAstPat.cast(this, ast.getName(), memo);
name = ITs2JavaAstPat.create(this, ast.getName(), memo);
type = name.getType();
init = ast.getInit().map(expr -> ITs2JavaAstExpr.cast(this, expr, memo));
init = ast.getInit().map(expr -> ITs2JavaAstExpr.create(this, expr, memo));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public Ts2JavaAstTsQualifiedName(
TypeDescription type,
Ts2JavaMemoFunction memo) {
super(parent, ast, memo);
left = ITs2JavaAstTsEntityName.cast(parent, ast.getLeft(), memo);
left = ITs2JavaAstTsEntityName.create(parent, ast.getLeft(), memo);
right = new Ts2JavaAstIdentName(parent, ast.getRight(), type, memo);
this.type = type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public Ts2JavaAstTsTypeAnn(
Swc4jAstTsTypeAnn ast,
Ts2JavaMemoFunction memo) {
super(parent, ast, memo);
typeAnn = ITs2JavaAstTsType.cast(this, ast.getTypeAnn(), memo);
typeAnn = ITs2JavaAstTsType.create(this, ast.getTypeAnn(), memo);
type = typeAnn.getType();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public Ts2JavaAstTsTypeRef(
Swc4jAstTsTypeRef ast,
Ts2JavaMemoFunction memo) {
super(parent, ast, memo);
typeName = ITs2JavaAstTsEntityName.cast(this, ast.getTypeName(), memo);
typeName = ITs2JavaAstTsEntityName.create(this, ast.getTypeName(), memo);
String entityTypeName = typeName.getTypeName();
type = TS_TYPE_REF_MAP.get(entityTypeName);
if (type == null) {
Expand Down

0 comments on commit 18ef4bd

Please sign in to comment.