Skip to content

Commit

Permalink
🦄 refactor: Redesign ast 12
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Nov 29, 2024
1 parent 0068ecd commit bb3a2c0
Show file tree
Hide file tree
Showing 14 changed files with 347 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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.BaseTs2JavaAst;
import com.caoccao.javet.buddy.ts2java.ast.interfaces.*;
import com.caoccao.javet.buddy.ts2java.ast.memo.Ts2JavaMemoFunction;
import com.caoccao.javet.buddy.ts2java.compiler.JavaLocalVariable;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstIdent;
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.implementation.bytecode.StackManipulation;
import net.bytebuddy.implementation.bytecode.member.MethodVariableAccess;
import net.bytebuddy.jar.asm.MethodVisitor;

public class Ts2JavaAstIdent
extends BaseTs2JavaAst<Swc4jAstIdent, Ts2JavaMemoFunction>
implements ITs2JavaAstExpr<Swc4jAstIdent, Ts2JavaMemoFunction>,
ITs2JavaAstProp<Swc4jAstIdent, Ts2JavaMemoFunction>,
ITs2JavaAstTsModuleRef<Swc4jAstIdent, Ts2JavaMemoFunction>,
ITs2JavaAstModuleExportName<Swc4jAstIdent, Ts2JavaMemoFunction>,
ITs2JavaAstTsEntityName<Swc4jAstIdent, Ts2JavaMemoFunction>,
ITs2JavaAstTsModuleName<Swc4jAstIdent, Ts2JavaMemoFunction>,
ITs2JavaAstJsxObject<Swc4jAstIdent, Ts2JavaMemoFunction>,
ITs2JavaAstJsxElementName<Swc4jAstIdent, Ts2JavaMemoFunction>,
ITs2JavaAstTsThisTypeOrIdent<Swc4jAstIdent, Ts2JavaMemoFunction>,
ITs2JavaAstTsEnumMemberId<Swc4jAstIdent, Ts2JavaMemoFunction> {
protected final boolean optional;
protected final String sym;

public Ts2JavaAstIdent(
ITs2JavaAst<?, ?> parent,
Swc4jAstIdent ast,
Ts2JavaMemoFunction memo) {
super(parent, ast, memo);
optional = ast.isOptional();
sym = ast.getSym();
}

@Override
public Size apply(MethodVisitor methodVisitor, Implementation.Context context) {
visitLineNumber(methodVisitor);
JavaLocalVariable localVariable = memo.getLocalVariable(sym);
MethodVariableAccess methodVariableAccess = MethodVariableAccess.of(localVariable.getType());
StackManipulation stackManipulation = methodVariableAccess.loadFrom(localVariable.getOffset());
return stackManipulation.apply(methodVisitor, context);
}

@Override
public void compile() {
}

public String getSym() {
return sym;
}

public boolean isOptional() {
return optional;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@

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

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;
import com.caoccao.javet.buddy.ts2java.ast.expr.lit.Ts2JavaAstNumber;
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.expr.Swc4jAstParenExpr;
import com.caoccao.javet.swc4j.ast.expr.Swc4jAstUnaryExpr;
import com.caoccao.javet.swc4j.ast.expr.lit.Swc4jAstBool;
Expand All @@ -40,14 +42,15 @@ public interface ITs2JavaAstExpr<AST extends ISwc4jAstExpr, Memo extends Ts2Java
switch (ast.getType()) {
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), null, memo);
case ParenExpr:
return cast(parent, ast.as(Swc4jAstParenExpr.class).unParenExpr(), memo);
case BinExpr:
case Ident:
case UnaryExpr:
return new Ts2JavaAstUnaryExpr(parent, ast.as(Swc4jAstUnaryExpr.class), memo);
case BinExpr:
default:
throw new Ts2JavaAstException(
ast,
Expand Down
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.ISwc4jAstJsxElementName;

public interface ITs2JavaAstJsxElementName<AST extends ISwc4jAstJsxElementName, Memo extends Ts2JavaMemo>
extends ITs2JavaAst<AST, Memo> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.ISwc4jAstJsxObject;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstTsModuleRef;

public interface ITs2JavaAstJsxObject<AST extends ISwc4jAstJsxObject, 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.ISwc4jAstModuleExportName;

public interface ITs2JavaAstModuleExportName<AST extends ISwc4jAstModuleExportName, 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.ISwc4jAstProp;

public interface ITs2JavaAstProp<AST extends ISwc4jAstProp, Memo extends Ts2JavaMemo>
extends ITs2JavaAstPropOrSpread<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.ISwc4jAstPropOrSpread;

public interface ITs2JavaAstPropOrSpread<AST extends ISwc4jAstPropOrSpread, 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.ISwc4jAstTsEntityName;

public interface ITs2JavaAstTsEntityName<AST extends ISwc4jAstTsEntityName, Memo extends Ts2JavaMemo>
extends ITs2JavaAstTsModuleRef<AST, Memo>, ITs2JavaAstTsTypeQueryExpr<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.ISwc4jAstTsEnumMemberId;

public interface ITs2JavaAstTsEnumMemberId<AST extends ISwc4jAstTsEnumMemberId, Memo extends Ts2JavaMemo>
extends ITs2JavaAst<AST, Memo> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.ISwc4jAstTsModuleName;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstTsModuleRef;

public interface ITs2JavaAstTsModuleName<AST extends ISwc4jAstTsModuleName, 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.ISwc4jAstTsModuleRef;

public interface ITs2JavaAstTsModuleRef<AST extends ISwc4jAstTsModuleRef, Memo extends Ts2JavaMemo>
extends ITs2JavaAst<AST, Memo> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.ISwc4jAstTsModuleRef;
import com.caoccao.javet.swc4j.ast.interfaces.ISwc4jAstTsThisTypeOrIdent;

public interface ITs2JavaAstTsThisTypeOrIdent<AST extends ISwc4jAstTsThisTypeOrIdent, 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.ISwc4jAstTsTypeQueryExpr;

public interface ITs2JavaAstTsTypeQueryExpr<AST extends ISwc4jAstTsTypeQueryExpr, Memo extends Ts2JavaMemo>
extends ITs2JavaAst<AST, Memo> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ public void testNumber() throws Exception {
assertEquals(1, tsClass.invoke());
tsClass = new TsClassX("return +(-1);", int.class);
assertEquals(-1, tsClass.invoke());
tsClass = new TsClassX("return a;", int.class, TsMethodArgument.of("a", int.class));
assertEquals(1, tsClass.invoke(1));
}

@Test
Expand Down

0 comments on commit bb3a2c0

Please sign in to comment.