Skip to content

Commit

Permalink
🦄 refactor: Redesign ast 18
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Dec 1, 2024
1 parent c3a404f commit 4e1cf0c
Show file tree
Hide file tree
Showing 12 changed files with 519 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

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

import com.caoccao.javet.buddy.ts2java.ast.memo.Ts2JavaMemoDynamicType;
import com.caoccao.javet.buddy.ts2java.ast.memo.Ts2JavaMemo;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstDecl;

public interface ITs2JavaAstDecl<AST extends ISwc4jAstDecl, Memo extends Ts2JavaMemoDynamicType>
public interface ITs2JavaAstDecl<AST extends ISwc4jAstDecl, Memo extends Ts2JavaMemo>
extends ITs2JavaAstStmt<AST, Memo> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,28 @@
package com.caoccao.javet.buddy.ts2java.ast.interfaces;

import com.caoccao.javet.buddy.ts2java.ast.memo.Ts2JavaMemo;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstExpr;
import com.caoccao.javet.buddy.ts2java.ast.memo.Ts2JavaMemoFunction;
import com.caoccao.javet.buddy.ts2java.ast.pat.Ts2JavaAstBindingIdent;
import com.caoccao.javet.buddy.ts2java.exceptions.Ts2JavaAstException;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstPat;
import com.caoccao.javet.swc4j.ast.pat.Swc4jAstBindingIdent;
import com.caoccao.javet.utils.SimpleFreeMarkerFormat;
import com.caoccao.javet.utils.SimpleMap;

public interface ITs2JavaAstPat<AST extends ISwc4jAstPat, Memo extends Ts2JavaMemo>
extends ITs2JavaAstAssignTarget<AST, Memo>, ITs2JavaAstForHead<AST, Memo> {
static ITs2JavaAstPat<?, ?> cast(
ITs2JavaAst<?, ?> parent,
ISwc4jAstPat ast,
Ts2JavaMemoFunction memo) {
switch (ast.getType()) {
case BindingIdent:
return new Ts2JavaAstBindingIdent(parent, ast.as(Swc4jAstBindingIdent.class), memo);
default:
throw new Ts2JavaAstException(
ast,
SimpleFreeMarkerFormat.format("Pat type ${type} is not supported.",
SimpleMap.of("type", ast.getType().name())));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,29 @@

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

import com.caoccao.javet.buddy.ts2java.ast.expr.Ts2JavaAstIdent;
import com.caoccao.javet.buddy.ts2java.ast.memo.Ts2JavaMemo;
import com.caoccao.javet.buddy.ts2java.ast.memo.Ts2JavaMemoFunction;
import com.caoccao.javet.buddy.ts2java.exceptions.Ts2JavaAstException;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstIdent;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstTsEntityName;
import com.caoccao.javet.utils.SimpleFreeMarkerFormat;
import com.caoccao.javet.utils.SimpleMap;

public interface ITs2JavaAstTsEntityName<AST extends ISwc4jAstTsEntityName, Memo extends Ts2JavaMemo>
extends ITs2JavaAstTsModuleRef<AST, Memo>, ITs2JavaAstTsTypeQueryExpr<AST, Memo> {
static ITs2JavaAstTsEntityName<?, ?> cast(
ITs2JavaAst<?, ?> parent,
ISwc4jAstTsEntityName ast,
Ts2JavaMemoFunction memo) {
switch (ast.getType()) {
case Ident:
return new Ts2JavaAstIdent(parent, ast.as(Swc4jAstIdent.class), null, memo);
default:
throw new Ts2JavaAstException(
ast,
SimpleFreeMarkerFormat.format("Ts entity name ${type} is not supported.",
SimpleMap.of("type", ast.getType().name())));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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.interfaces;

import com.caoccao.javet.buddy.ts2java.ast.memo.Ts2JavaMemo;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstTsFnParam;

public interface ITs2JavaAstTsFnParam<AST extends ISwc4jAstTsFnParam, Memo extends Ts2JavaMemo>
extends ITs2JavaAst<AST, Memo> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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.interfaces;

import com.caoccao.javet.buddy.ts2java.ast.memo.Ts2JavaMemo;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstTsParamPropParam;

public interface ITs2JavaAstTsParamPropParam<AST extends ISwc4jAstTsParamPropParam, Memo extends Ts2JavaMemo>
extends ITs2JavaAst<AST, Memo> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.interfaces;

import com.caoccao.javet.buddy.ts2java.ast.memo.Ts2JavaMemo;
import com.caoccao.javet.buddy.ts2java.ast.memo.Ts2JavaMemoFunction;
import com.caoccao.javet.buddy.ts2java.ast.ts.Ts2JavaAstTsKeywordType;
import com.caoccao.javet.buddy.ts2java.ast.ts.Ts2JavaAstTsTypeRef;
import com.caoccao.javet.buddy.ts2java.exceptions.Ts2JavaAstException;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstTsType;
import com.caoccao.javet.swc4j.ast.ts.Swc4jAstTsKeywordType;
import com.caoccao.javet.swc4j.ast.ts.Swc4jAstTsTypeRef;
import com.caoccao.javet.utils.SimpleFreeMarkerFormat;
import com.caoccao.javet.utils.SimpleMap;

public interface ITs2JavaAstTsType<AST extends ISwc4jAstTsType, Memo extends Ts2JavaMemo>
extends ITs2JavaAst<AST, Memo> {
static ITs2JavaAstTsType<?, ?> cast(
ITs2JavaAst<?, ?> parent,
ISwc4jAstTsType ast,
Ts2JavaMemoFunction memo) {
switch (ast.getType()) {
case TsKeywordType:
return new Ts2JavaAstTsKeywordType(parent, ast.as(Swc4jAstTsKeywordType.class), memo);
case TsTypeRef:
return new Ts2JavaAstTsTypeRef(parent, ast.as(Swc4jAstTsTypeRef.class), memo);
default:
throw new Ts2JavaAstException(
ast,
SimpleFreeMarkerFormat.format("Ts type ${type} is not supported.",
SimpleMap.of("type", ast.getType().name())));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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.pat;

import com.caoccao.javet.buddy.ts2java.ast.BaseTs2JavaAst;
import com.caoccao.javet.buddy.ts2java.ast.expr.Ts2JavaAstIdent;
import com.caoccao.javet.buddy.ts2java.ast.interfaces.*;
import com.caoccao.javet.buddy.ts2java.ast.memo.Ts2JavaMemoFunction;
import com.caoccao.javet.buddy.ts2java.ast.ts.Ts2JavaAstTsTypeAnn;
import com.caoccao.javet.swc4j.ast.pat.Swc4jAstBindingIdent;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.jar.asm.MethodVisitor;

import java.util.Optional;

public class Ts2JavaAstBindingIdent
extends BaseTs2JavaAst<Swc4jAstBindingIdent, Ts2JavaMemoFunction>
implements ITs2JavaAstPat<Swc4jAstBindingIdent, Ts2JavaMemoFunction>,
ITs2JavaAstTsFnParam<Swc4jAstBindingIdent, Ts2JavaMemoFunction>,
ITs2JavaAstTsParamPropParam<Swc4jAstBindingIdent, Ts2JavaMemoFunction>,
ITs2JavaAstSimpleAssignTarget<Swc4jAstBindingIdent, Ts2JavaMemoFunction> {
protected final Ts2JavaAstIdent id;
protected final Optional<Ts2JavaAstTsTypeAnn> typeAnn;

public Ts2JavaAstBindingIdent(
ITs2JavaAst<?, ?> parent,
Swc4jAstBindingIdent ast,
Ts2JavaMemoFunction memo) {
super(parent, ast, memo);
typeAnn = ast.getTypeAnn().map(t -> new Ts2JavaAstTsTypeAnn(this, t, memo));
type = typeAnn.map(Ts2JavaAstTsTypeAnn::getType).orElse(TypeDescription.ForLoadedType.of(void.class));
id = new Ts2JavaAstIdent(this, ast.getId(), type, memo);
}

@Override
public Size apply(MethodVisitor methodVisitor, Implementation.Context context) {
visitLineNumber(methodVisitor);
return Size.ZERO;
}

@Override
public void compile() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.stmt;

import com.caoccao.javet.buddy.ts2java.ast.BaseTs2JavaAst;
import com.caoccao.javet.buddy.ts2java.ast.interfaces.ITs2JavaAst;
import com.caoccao.javet.buddy.ts2java.ast.interfaces.ITs2JavaAstDecl;
import com.caoccao.javet.buddy.ts2java.ast.interfaces.ITs2JavaAstForHead;
import com.caoccao.javet.buddy.ts2java.ast.interfaces.ITs2JavaAstVarDeclOrExpr;
import com.caoccao.javet.buddy.ts2java.ast.memo.Ts2JavaMemoFunction;
import com.caoccao.javet.swc4j.ast.stmt.Swc4jAstVarDecl;
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.jar.asm.MethodVisitor;

import java.util.List;
import java.util.stream.Collectors;

public class Ts2JavaAstVarDecl
extends BaseTs2JavaAst<Swc4jAstVarDecl, Ts2JavaMemoFunction>
implements ITs2JavaAstDecl<Swc4jAstVarDecl, Ts2JavaMemoFunction>,
ITs2JavaAstVarDeclOrExpr<Swc4jAstVarDecl, Ts2JavaMemoFunction>,
ITs2JavaAstForHead<Swc4jAstVarDecl, Ts2JavaMemoFunction> {
protected final List<Ts2JavaAstVarDeclarator> decls;

public Ts2JavaAstVarDecl(
ITs2JavaAst<?, ?> parent,
Swc4jAstVarDecl ast,
Ts2JavaMemoFunction memo) {
super(parent, ast, memo);
decls = ast.getDecls().stream()
.map(decl -> new Ts2JavaAstVarDeclarator(this, decl, memo))
.collect(Collectors.toList());
}

@Override
public Size apply(MethodVisitor methodVisitor, Implementation.Context context) {
visitLineNumber(methodVisitor);
return Size.ZERO;
}

@Override
public void compile() {
decls.forEach(Ts2JavaAstVarDeclarator::compile);
}

public List<Ts2JavaAstVarDeclarator> getDecls() {
return decls;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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.stmt;

import com.caoccao.javet.buddy.ts2java.ast.BaseTs2JavaAst;
import com.caoccao.javet.buddy.ts2java.ast.interfaces.ITs2JavaAst;
import com.caoccao.javet.buddy.ts2java.ast.interfaces.ITs2JavaAstDecl;
import com.caoccao.javet.buddy.ts2java.ast.interfaces.ITs2JavaAstExpr;
import com.caoccao.javet.buddy.ts2java.ast.interfaces.ITs2JavaAstPat;
import com.caoccao.javet.buddy.ts2java.ast.memo.Ts2JavaMemoFunction;
import com.caoccao.javet.swc4j.ast.stmt.Swc4jAstVarDeclarator;
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.jar.asm.MethodVisitor;

import java.util.Optional;

public class Ts2JavaAstVarDeclarator
extends BaseTs2JavaAst<Swc4jAstVarDeclarator, Ts2JavaMemoFunction>
implements ITs2JavaAstDecl<Swc4jAstVarDeclarator, Ts2JavaMemoFunction> {
protected final Optional<ITs2JavaAstExpr> init;
protected final ITs2JavaAstPat name;

public Ts2JavaAstVarDeclarator(
ITs2JavaAst<?, ?> parent,
Swc4jAstVarDeclarator ast,
Ts2JavaMemoFunction memo) {
super(parent, ast, memo);
name = ITs2JavaAstPat.cast(this, ast.getName(), memo);
type = name.getType();
init = ast.getInit().map(expr -> ITs2JavaAstExpr.cast(this, expr, type, memo));
}

@Override
public Size apply(MethodVisitor methodVisitor, Implementation.Context context) {
visitLineNumber(methodVisitor);
return Size.ZERO;
}

@Override
public void compile() {
}

public Optional<ITs2JavaAstExpr> getInit() {
return init;
}

public ITs2JavaAstPat getName() {
return name;
}
}
Loading

0 comments on commit 4e1cf0c

Please sign in to comment.