Skip to content

Commit

Permalink
🦄 refactor: Resign ast 2
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Nov 26, 2024
1 parent 76746cb commit 80acd44
Show file tree
Hide file tree
Showing 9 changed files with 309 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,14 @@

public abstract class BaseTs2JavaAst<AST extends ISwc4jAst>
implements ITs2JavaAst<AST> {
protected final Ts2JavaDynamicTypeBuilderStore builderStore;
protected AST ast;

public BaseTs2JavaAst(Ts2JavaDynamicTypeBuilderStore builderStore, AST ast) {
public BaseTs2JavaAst(AST ast) {
this.ast = Objects.requireNonNull(ast);
this.builderStore = Objects.requireNonNull(builderStore);
}

@Override
public AST getAst() {
return ast;
}

@Override
public Ts2JavaDynamicTypeBuilderStore getBuilderStore() {
return builderStore;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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;

import com.caoccao.javet.buddy.ts2java.ast.interfaces.ITs2JavaBuilderStore;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAst;

import java.util.Objects;

public abstract class BaseTs2JavaAstWithBuilderStore<AST extends ISwc4jAst>
extends BaseTs2JavaAst<AST>
implements ITs2JavaBuilderStore {
protected final Ts2JavaDynamicTypeBuilderStore builderStore;

public BaseTs2JavaAstWithBuilderStore(Ts2JavaDynamicTypeBuilderStore builderStore, AST ast) {
super(ast);
this.builderStore = Objects.requireNonNull(builderStore);
}

@Override
public Ts2JavaDynamicTypeBuilderStore getBuilderStore() {
return builderStore;
}
}
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.clazz;

import com.caoccao.javet.buddy.ts2java.ast.BaseTs2JavaAstWithBuilderStore;
import com.caoccao.javet.buddy.ts2java.ast.Ts2JavaDynamicTypeBuilderStore;
import com.caoccao.javet.buddy.ts2java.ast.interfaces.ITs2JavaAstClassMember;
import com.caoccao.javet.buddy.ts2java.exceptions.Ts2JavaAstException;
import com.caoccao.javet.swc4j.ast.clazz.Swc4jAstClass;
import com.caoccao.javet.swc4j.ast.clazz.Swc4jAstClassMethod;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstClassMember;
import com.caoccao.javet.utils.SimpleFreeMarkerFormat;
import com.caoccao.javet.utils.SimpleMap;
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.jar.asm.MethodVisitor;

import java.util.ArrayList;
import java.util.List;

public class Ts2JavaAstClass
extends BaseTs2JavaAstWithBuilderStore<Swc4jAstClass> {
protected final List<ITs2JavaAstClassMember<?>> body;

public Ts2JavaAstClass(Ts2JavaDynamicTypeBuilderStore builderStore, Swc4jAstClass ast) {
super(builderStore, ast);
body = new ArrayList<>();
for (ISwc4jAstClassMember classMember : ast.getBody()) {
switch (classMember.getType()) {
case ClassMethod:
body.add(new Ts2JavaAstClassMethod(builderStore, classMember.as(Swc4jAstClassMethod.class)));
break;
default:
throw new Ts2JavaAstException(
classMember,
SimpleFreeMarkerFormat.format("Class body type ${type} is not supported.",
SimpleMap.of("type", classMember.getType().name())));
}
}
}

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

@Override
public void compile() {
body.forEach(ITs2JavaAstClassMember::compile);
}

public List<ITs2JavaAstClassMember<?>> getBody() {
return body;
}
}
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.clazz;

import com.caoccao.javet.buddy.ts2java.ast.BaseTs2JavaAstWithBuilderStore;
import com.caoccao.javet.buddy.ts2java.ast.Ts2JavaDynamicTypeBuilderStore;
import com.caoccao.javet.buddy.ts2java.ast.interfaces.ITs2JavaAstClassMember;
import com.caoccao.javet.buddy.ts2java.exceptions.Ts2JavaAstException;
import com.caoccao.javet.swc4j.ast.clazz.Swc4jAstClassMethod;
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstAccessibility;
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstMethodKind;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstIdentName;
import com.caoccao.javet.utils.SimpleFreeMarkerFormat;
import com.caoccao.javet.utils.SimpleMap;
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.jar.asm.MethodVisitor;

public class Ts2JavaAstClassMethod
extends BaseTs2JavaAstWithBuilderStore<Swc4jAstClassMethod>
implements ITs2JavaAstClassMember<Swc4jAstClassMethod> {
protected final Ts2JavaAstFunction function;

public Ts2JavaAstClassMethod(Ts2JavaDynamicTypeBuilderStore builderStore, Swc4jAstClassMethod ast) {
super(builderStore, ast);
if (ast.isStatic()) {
throw new Ts2JavaAstException(
ast,
"Static method is not implemented");
}
if (ast.getKind() != Swc4jAstMethodKind.Method) {
throw new Ts2JavaAstException(
ast,
SimpleFreeMarkerFormat.format("ClassMethod kind ${kind} is not supported.",
SimpleMap.of("kind", ast.getKind().name())));
}
if (!(ast.getKey() instanceof Swc4jAstIdentName)) {
throw new Ts2JavaAstException(
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(builderStore, ast.getFunction(), name, _static, accessibility);
}

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

@Override
public void compile() {
function.compile();
}

public Ts2JavaAstFunction getFunction() {
return function;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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.clazz;

import com.caoccao.javet.buddy.ts2java.ast.BaseTs2JavaAstWithBuilderStore;
import com.caoccao.javet.buddy.ts2java.ast.Ts2JavaDynamicTypeBuilderStore;
import com.caoccao.javet.swc4j.ast.clazz.Swc4jAstFunction;
import com.caoccao.javet.swc4j.ast.enums.Swc4jAstAccessibility;
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.jar.asm.MethodVisitor;

public class Ts2JavaAstFunction
extends BaseTs2JavaAstWithBuilderStore<Swc4jAstFunction> {
protected final boolean _static;
protected final Swc4jAstAccessibility accessibility;
protected final String name;

public Ts2JavaAstFunction(
Ts2JavaDynamicTypeBuilderStore builderStore,
Swc4jAstFunction ast,
String name,
boolean _static,
Swc4jAstAccessibility accessibility) {
super(builderStore, ast);
this._static = _static;
this.accessibility = accessibility;
this.name = name;
}

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

@Override
public void compile() {

}

public Swc4jAstAccessibility getAccessibility() {
return accessibility;
}

public String getName() {
return name;
}

public boolean isStatic() {
return _static;
}
}
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.swc4j.ast.interfaces.ISwc4jAstClassMember;

public interface ITs2JavaAstClassMember<AST extends ISwc4jAstClassMember>
extends ITs2JavaAst<AST> {

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

import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstDecl;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstStmt;

public interface ITs2JavaAstDecl<AST extends ISwc4jAstDecl>
extends ITs2JavaAstStmt<AST> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* 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.Ts2JavaDynamicTypeBuilderStore;

public interface ITs2JavaBuilderStore {
Ts2JavaDynamicTypeBuilderStore getBuilderStore();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,27 @@

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

import com.caoccao.javet.buddy.ts2java.ast.BaseTs2JavaAst;
import com.caoccao.javet.buddy.ts2java.ast.BaseTs2JavaAstWithBuilderStore;
import com.caoccao.javet.buddy.ts2java.ast.Ts2JavaDynamicTypeBuilderStore;
import com.caoccao.javet.buddy.ts2java.ast.clazz.Ts2JavaAstClass;
import com.caoccao.javet.buddy.ts2java.ast.interfaces.ITs2JavaAstDecl;
import com.caoccao.javet.swc4j.ast.stmt.Swc4jAstClassDecl;
import com.caoccao.javet.utils.StringUtils;
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.jar.asm.MethodVisitor;

public class Ts2JavaAstClassDecl
extends BaseTs2JavaAst<Swc4jAstClassDecl>
extends BaseTs2JavaAstWithBuilderStore<Swc4jAstClassDecl>
implements ITs2JavaAstDecl<Swc4jAstClassDecl> {
protected final Ts2JavaAstClass clazz;
protected final String packageName;

public Ts2JavaAstClassDecl(
Ts2JavaDynamicTypeBuilderStore builderStore,
Swc4jAstClassDecl ast,
String packageName) {
super(builderStore, ast);
clazz = new Ts2JavaAstClass(builderStore, ast.getClazz());
this.packageName = packageName;
}

Expand All @@ -43,7 +47,15 @@ public Size apply(MethodVisitor methodVisitor, Implementation.Context context) {

@Override
public void compile() {
String className = StringUtils.isEmpty(packageName)
? ast.getIdent().getSym()
: packageName + "." + ast.getIdent().getSym();
builderStore.setBuilder(builderStore.getBuilder().name(className));
clazz.compile();
}

public Ts2JavaAstClass getClazz() {
return clazz;
}

public String getPackageName() {
Expand Down

0 comments on commit 80acd44

Please sign in to comment.