Revert "Get rid of mutable state, hopefully fix #307"

This reverts commit bc61fc2e1d.
This commit is contained in:
Daniel Holle 2024-03-27 09:57:21 +01:00
parent 8fdfbf875b
commit 3b14cd609f
2 changed files with 101 additions and 101 deletions

View File

@ -37,6 +37,8 @@ public class ASTToTargetAST {
public final JavaTXCompiler compiler; public final JavaTXCompiler compiler;
protected ClassOrInterface currentClass; // TODO This is only needed because of SuperCall, maybe there's
public List<GenericsResult> txGenerics() { public List<GenericsResult> txGenerics() {
return all.stream().map(generics -> new GenericsResult(generics.txGenerics)).toList(); return all.stream().map(generics -> new GenericsResult(generics.txGenerics)).toList();
} }
@ -45,8 +47,8 @@ public class ASTToTargetAST {
return all.stream().map(generics -> new GenericsResult(generics.javaGenerics)).toList(); return all.stream().map(generics -> new GenericsResult(generics.javaGenerics)).toList();
} }
public TargetExpression convert(ClassOrInterface clazz, Pattern pattern) { public TargetExpression convert(Pattern pattern) {
var converter = new StatementToTargetExpression(this, clazz); var converter = new StatementToTargetExpression(this);
pattern.accept(converter); pattern.accept(converter);
return converter.result; return converter.result;
} }
@ -130,6 +132,7 @@ public class ASTToTargetAST {
} }
public TargetStructure convert(ClassOrInterface input) { public TargetStructure convert(ClassOrInterface input) {
currentClass = input;
Set<TargetGeneric> javaGenerics = new HashSet<>(); Set<TargetGeneric> javaGenerics = new HashSet<>();
Set<TargetGeneric> txGenerics = new HashSet<>(); Set<TargetGeneric> txGenerics = new HashSet<>();
@ -153,17 +156,17 @@ public class ASTToTargetAST {
TargetBlock fieldInitializer = null; TargetBlock fieldInitializer = null;
if (input.getfieldInitializations().isPresent()) if (input.getfieldInitializations().isPresent())
fieldInitializer = convert(input, input.getfieldInitializations().get().block); fieldInitializer = convert(input.getfieldInitializations().get().block);
TargetBlock finalFieldInitializer = fieldInitializer; TargetBlock finalFieldInitializer = fieldInitializer;
var superInterfaces = input.getSuperInterfaces().stream().map(clazz -> convert(clazz, generics.javaGenerics)).toList(); var superInterfaces = input.getSuperInterfaces().stream().map(clazz -> convert(clazz, generics.javaGenerics)).toList();
var constructors = input.getConstructors().stream().map(constructor -> this.convert(input, constructor, finalFieldInitializer)).flatMap(List::stream).toList(); var constructors = input.getConstructors().stream().map(constructor -> this.convert(constructor, finalFieldInitializer)).flatMap(List::stream).toList();
var fields = input.getFieldDecl().stream().map(this::convert).toList(); var fields = input.getFieldDecl().stream().map(this::convert).toList();
var methods = groupOverloads(input.getMethods()).stream().map(m -> convert(input, m)).flatMap(List::stream).toList(); var methods = groupOverloads(input.getMethods()).stream().map(this::convert).flatMap(List::stream).toList();
TargetMethod staticConstructor = null; TargetMethod staticConstructor = null;
if (input.getStaticInitializer().isPresent()) if (input.getStaticInitializer().isPresent())
staticConstructor = this.convert(input, input.getStaticInitializer().get()).get(0); staticConstructor = this.convert(input.getStaticInitializer().get()).get(0);
if (input instanceof Record) if (input instanceof Record)
return new TargetRecord(input.getModifiers(), input.getClassName(), javaGenerics, txGenerics, superInterfaces, constructors, staticConstructor, fields, methods); return new TargetRecord(input.getModifiers(), input.getClassName(), javaGenerics, txGenerics, superInterfaces, constructors, staticConstructor, fields, methods);
@ -172,9 +175,9 @@ public class ASTToTargetAST {
else return new TargetClass(input.getModifiers(), input.getClassName(), convert(input.getSuperClass(), generics.javaGenerics), javaGenerics, txGenerics, superInterfaces, constructors, staticConstructor, fields, methods); else return new TargetClass(input.getModifiers(), input.getClassName(), convert(input.getSuperClass(), generics.javaGenerics), javaGenerics, txGenerics, superInterfaces, constructors, staticConstructor, fields, methods);
} }
private List<MethodParameter> convert(ClassOrInterface clazz, ParameterList input, GenerateGenerics generics) { private List<MethodParameter> convert(ParameterList input, GenerateGenerics generics) {
return input.getFormalparalist().stream().map(param -> return input.getFormalparalist().stream().map(param ->
new MethodParameter((TargetPattern) convert(clazz, param)) new MethodParameter((TargetPattern) convert(param))
).toList(); ).toList();
} }
@ -182,7 +185,7 @@ public class ASTToTargetAST {
return generics.stream().anyMatch(g -> g.name().equals(type.getParsedName())); return generics.stream().anyMatch(g -> g.name().equals(type.getParsedName()));
} }
private Set<TargetGeneric> collectMethodGenerics(ClassOrInterface currentClass, GenerateGenerics generateGenerics, Set<GenerateGenerics.Pair> generics, Method input) { private Set<TargetGeneric> collectMethodGenerics(GenerateGenerics generateGenerics, Set<GenerateGenerics.Pair> generics, Method input) {
var convertedGenerics = new HashSet<>(convert(generics, generateGenerics)); var convertedGenerics = new HashSet<>(convert(generics, generateGenerics));
outer: for (GenericTypeVar typeVar : input.getGenerics()) { outer: for (GenericTypeVar typeVar : input.getGenerics()) {
for (var classGeneric : currentClass.getGenerics()) { for (var classGeneric : currentClass.getGenerics()) {
@ -200,22 +203,22 @@ public class ASTToTargetAST {
return convertedGenerics; return convertedGenerics;
} }
private List<TargetConstructor> convert(ClassOrInterface clazz, Constructor input, TargetBlock fieldInitializer) { private List<TargetConstructor> convert(Constructor input, TargetBlock fieldInitializer) {
generics = all.get(0); generics = all.get(0);
List<TargetConstructor> result = new ArrayList<>(); List<TargetConstructor> result = new ArrayList<>();
Set<List<MethodParameter>> parameterSet = new HashSet<>(); Set<List<MethodParameter>> parameterSet = new HashSet<>();
for (var s : all) { for (var s : all) {
generics = s; generics = s;
var javaGenerics = this.generics.javaGenerics.generics(clazz, input); var javaGenerics = this.generics.javaGenerics.generics(currentClass, input);
var txGenerics = this.generics.txGenerics.generics(clazz, input); var txGenerics = this.generics.txGenerics.generics(currentClass, input);
List<MethodParameter> params = convert(clazz, input.getParameterList(), this.generics.javaGenerics); List<MethodParameter> params = convert(input.getParameterList(), this.generics.javaGenerics);
if (parameterSet.stream().noneMatch(p -> p.equals(params))) { if (parameterSet.stream().noneMatch(p -> p.equals(params))) {
List<MethodParameter> txParams = convert(clazz, input.getParameterList(), this.generics.txGenerics); List<MethodParameter> txParams = convert(input.getParameterList(), this.generics.txGenerics);
var javaMethodGenerics = collectMethodGenerics(clazz, generics.javaGenerics(), javaGenerics, input); var javaMethodGenerics = collectMethodGenerics(generics.javaGenerics(), javaGenerics, input);
var txMethodGenerics = collectMethodGenerics(clazz, generics.txGenerics(), txGenerics, input); var txMethodGenerics = collectMethodGenerics(generics.txGenerics(), txGenerics, input);
result.add(new TargetConstructor(input.modifier, javaMethodGenerics, txMethodGenerics, params, txParams, convert(clazz, input.block), fieldInitializer)); result.add(new TargetConstructor(input.modifier, javaMethodGenerics, txMethodGenerics, params, txParams, convert(input.block), fieldInitializer));
parameterSet.add(params); parameterSet.add(params);
} }
} }
@ -288,9 +291,9 @@ public class ASTToTargetAST {
return res.toString(); return res.toString();
} }
private List<TargetMethod> convert(ClassOrInterface currentClass, List<Method> overloadedMethods) { private List<TargetMethod> convert(List<Method> overloadedMethods) {
if (overloadedMethods.size() == 1) { if (overloadedMethods.size() == 1) {
return convert(currentClass, overloadedMethods.get(0)); return convert(overloadedMethods.get(0));
} }
var res = new ArrayList<Method>(); var res = new ArrayList<Method>();
for (var method : overloadedMethods) { for (var method : overloadedMethods) {
@ -327,7 +330,7 @@ public class ASTToTargetAST {
var entryPoint = new Method(template.modifier, template.name, template.getReturnType(), params, block, template.getGenerics(), new NullToken()); var entryPoint = new Method(template.modifier, template.name, template.getReturnType(), params, block, template.getGenerics(), new NullToken());
res.add(entryPoint); // TODO*/ res.add(entryPoint); // TODO*/
return res.stream().map(m -> this.convert(currentClass, m)).flatMap(List::stream).toList(); return res.stream().map(this::convert).flatMap(List::stream).toList();
} }
private Expression makeRecordSwitch(RefTypeOrTPHOrWildcardOrGeneric returnType, ParameterList params, List<Method> overloadedMethods) { private Expression makeRecordSwitch(RefTypeOrTPHOrWildcardOrGeneric returnType, ParameterList params, List<Method> overloadedMethods) {
@ -351,7 +354,7 @@ public class ASTToTargetAST {
return swtch; return swtch;
} }
private Optional<Method> findSuperMethodToOverride(ClassOrInterface currentClass, String name, List<MethodParameter> params) { private Optional<Method> findSuperMethodToOverride(String name, List<MethodParameter> params) {
var superClass = compiler.getClass(currentClass.getSuperClass().getName()); var superClass = compiler.getClass(currentClass.getSuperClass().getName());
var methodStream = superClass.getMethods().stream(); var methodStream = superClass.getMethods().stream();
for (var superInterface : currentClass.getSuperInterfaces()) { for (var superInterface : currentClass.getSuperInterfaces()) {
@ -372,7 +375,7 @@ public class ASTToTargetAST {
}).findFirst(); }).findFirst();
} }
private List<TargetMethod> convert(ClassOrInterface currentClass, Method method) { private List<TargetMethod> convert(Method method) {
generics = all.get(0); generics = all.get(0);
List<TargetMethod> result = new ArrayList<>(); List<TargetMethod> result = new ArrayList<>();
Set<List<MethodParameter>> parameterSet = new HashSet<>(); Set<List<MethodParameter>> parameterSet = new HashSet<>();
@ -381,58 +384,58 @@ public class ASTToTargetAST {
generics = s; generics = s;
var javaGenerics = this.generics.javaGenerics.generics(currentClass, method); var javaGenerics = this.generics.javaGenerics.generics(currentClass, method);
var txGenerics = this.generics.txGenerics.generics(currentClass, method); var txGenerics = this.generics.txGenerics.generics(currentClass, method);
List<MethodParameter> params = convert(currentClass, method.getParameterList(), this.generics.javaGenerics); List<MethodParameter> params = convert(method.getParameterList(), this.generics.javaGenerics);
var returnType = convert(method.getReturnType(), this.generics.javaGenerics); var returnType = convert(method.getReturnType(), this.generics.javaGenerics);
var superMethod = findSuperMethodToOverride(currentClass, method.getName(), params); var superMethod = findSuperMethodToOverride(method.getName(), params);
if (superMethod.isPresent()) { if (superMethod.isPresent()) {
// If we find a super method to override, use its parameters and return types // If we find a super method to override, use its parameters and return types
returnType = convert(superMethod.get().getReturnType(), this.generics.javaGenerics); returnType = convert(superMethod.get().getReturnType(), this.generics.javaGenerics);
params = convert(currentClass, superMethod.get().getParameterList(), method.getParameterList(), this.generics.javaGenerics); params = convert(superMethod.get().getParameterList(), method.getParameterList(), this.generics.javaGenerics);
} }
List<MethodParameter> finalParams = params; List<MethodParameter> finalParams = params;
if (parameterSet.stream().noneMatch(p -> p.equals(finalParams))) { if (parameterSet.stream().noneMatch(p -> p.equals(finalParams))) {
List<MethodParameter> txParams = convert(currentClass, method.getParameterList(), this.generics.txGenerics); List<MethodParameter> txParams = convert(method.getParameterList(), this.generics.txGenerics);
var javaMethodGenerics = collectMethodGenerics(currentClass, generics.javaGenerics(), javaGenerics, method); var javaMethodGenerics = collectMethodGenerics(generics.javaGenerics(), javaGenerics, method);
var txMethodGenerics = collectMethodGenerics(currentClass, generics.txGenerics(), txGenerics, method); var txMethodGenerics = collectMethodGenerics(generics.txGenerics(), txGenerics, method);
var javaSignature = new TargetMethod.Signature(javaMethodGenerics, params, returnType); var javaSignature = new TargetMethod.Signature(javaMethodGenerics, params, returnType);
var txSignature = new TargetMethod.Signature(txMethodGenerics, txParams, convert(method.getReturnType(), this.generics.txGenerics)); var txSignature = new TargetMethod.Signature(txMethodGenerics, txParams, convert(method.getReturnType(), this.generics.txGenerics));
result.add(new TargetMethod(method.modifier, method.name, convert(currentClass, method.block), javaSignature, txSignature)); result.add(new TargetMethod(method.modifier, method.name, convert(method.block), javaSignature, txSignature));
parameterSet.add(params); parameterSet.add(params);
} }
} }
return result; return result;
} }
private List<MethodParameter> convert(ClassOrInterface currentClass, ParameterList superList, ParameterList paraList, JavaGenerics generics) { private List<MethodParameter> convert(ParameterList superList, ParameterList paraList, JavaGenerics generics) {
var list = new ArrayList<MethodParameter>(); var list = new ArrayList<MethodParameter>();
for (var i = 0; i < paraList.getFormalparalist().size(); i++) { for (var i = 0; i < paraList.getFormalparalist().size(); i++) {
var param = paraList.getParameterAt(i); var param = paraList.getParameterAt(i);
list.add(new MethodParameter((TargetPattern) convert(currentClass, param)).withType(convert(superList.getParameterAt(i).getType(), generics))); list.add(new MethodParameter((TargetPattern) convert(param)).withType(convert(superList.getParameterAt(i).getType(), generics)));
} }
return list; return list;
} }
protected TargetSwitch.Case convert(ClassOrInterface clazz, SwitchBlock block) { protected TargetSwitch.Case convert(SwitchBlock block) {
return new TargetSwitch.Case(block.getLabels().stream().map(x -> convert(clazz, x)).toList(), convert(clazz, (Block) block), block.isExpression); return new TargetSwitch.Case(block.getLabels().stream().map(this::convert).toList(), convert((Block) block), block.isExpression);
} }
protected TargetBlock convert(ClassOrInterface clazz, Block block) { protected TargetBlock convert(Block block) {
if (block == null) return null; if (block == null) return null;
return new TargetBlock(block.statements.stream().map(x -> convert(clazz, x)).toList()); return new TargetBlock(block.statements.stream().map(this::convert).toList());
} }
protected TargetBlock convertWrapInBlock(ClassOrInterface clazz, Expression expression) { protected TargetBlock convertWrapInBlock(Expression expression) {
var res = convert(clazz, expression); var res = convert(expression);
if (!(res instanceof TargetBlock)) if (!(res instanceof TargetBlock))
return new TargetBlock(List.of(res)); return new TargetBlock(List.of(res));
return (TargetBlock) res; return (TargetBlock) res;
} }
protected TargetExpression convert(ClassOrInterface currentClass, Expression expr) { protected TargetExpression convert(Expression expr) {
var converter = new StatementToTargetExpression(this, currentClass); var converter = new StatementToTargetExpression(this);
expr.accept(converter); expr.accept(converter);
return converter.result; return converter.result;
} }

View File

@ -20,11 +20,8 @@ import java.util.stream.StreamSupport;
public class StatementToTargetExpression implements ASTVisitor { public class StatementToTargetExpression implements ASTVisitor {
private final ClassOrInterface currentClass; public StatementToTargetExpression(ASTToTargetAST converter) {
public StatementToTargetExpression(ASTToTargetAST converter, ClassOrInterface currentClass) {
this.converter = converter; this.converter = converter;
this.currentClass = currentClass;
} }
public TargetExpression result; public TargetExpression result;
@ -84,38 +81,38 @@ public class StatementToTargetExpression implements ASTVisitor {
} // Don't look at lambda expressions } // Don't look at lambda expressions
}); });
result = new TargetLambdaExpression(converter.convert(lambdaExpression.getType()), captures, parameters, converter.convert(lambdaExpression.getReturnType()), converter.convert(currentClass, lambdaExpression.methodBody)); result = new TargetLambdaExpression(converter.convert(lambdaExpression.getType()), captures, parameters, converter.convert(lambdaExpression.getReturnType()), converter.convert(lambdaExpression.methodBody));
} }
@Override @Override
public void visit(Assign assign) { public void visit(Assign assign) {
TargetExpression left; TargetExpression left;
if (assign.lefSide instanceof AssignToLocal) { if (assign.lefSide instanceof AssignToLocal) {
left = converter.convert(currentClass, ((AssignToLocal) assign.lefSide).localVar); left = converter.convert(((AssignToLocal) assign.lefSide).localVar);
} else { } else {
left = converter.convert(currentClass, ((AssignToField) assign.lefSide).field); left = converter.convert(((AssignToField) assign.lefSide).field);
} }
result = new TargetAssign(converter.convert(assign.getType()), left, converter.convert(currentClass, assign.rightSide)); result = new TargetAssign(converter.convert(assign.getType()), left, converter.convert(assign.rightSide));
} }
@Override @Override
public void visit(BinaryExpr binary) { public void visit(BinaryExpr binary) {
result = switch (binary.operation) { result = switch (binary.operation) {
case ADD -> new TargetBinaryOp.Add(converter.convert(binary.getType()), converter.convert(currentClass, binary.lexpr), converter.convert(currentClass, binary.rexpr)); case ADD -> new TargetBinaryOp.Add(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr));
case SUB -> new TargetBinaryOp.Sub(converter.convert(binary.getType()), converter.convert(currentClass, binary.lexpr), converter.convert(currentClass, binary.rexpr)); case SUB -> new TargetBinaryOp.Sub(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr));
case MUL -> new TargetBinaryOp.Mul(converter.convert(binary.getType()), converter.convert(currentClass, binary.lexpr), converter.convert(currentClass, binary.rexpr)); case MUL -> new TargetBinaryOp.Mul(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr));
case MOD -> new TargetBinaryOp.Rem(converter.convert(binary.getType()), converter.convert(currentClass, binary.lexpr), converter.convert(currentClass, binary.rexpr)); case MOD -> new TargetBinaryOp.Rem(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr));
case AND -> new TargetBinaryOp.BAnd(converter.convert(binary.getType()), converter.convert(currentClass, binary.lexpr), converter.convert(currentClass, binary.rexpr)); case AND -> new TargetBinaryOp.BAnd(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr));
case OR -> new TargetBinaryOp.BOr(converter.convert(binary.getType()), converter.convert(currentClass, binary.lexpr), converter.convert(currentClass, binary.rexpr)); case OR -> new TargetBinaryOp.BOr(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr));
case XOR -> new TargetBinaryOp.XOr(converter.convert(binary.getType()), converter.convert(currentClass, binary.lexpr), converter.convert(currentClass, binary.rexpr)); case XOR -> new TargetBinaryOp.XOr(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr));
case DIV -> new TargetBinaryOp.Div(converter.convert(binary.getType()), converter.convert(currentClass, binary.lexpr), converter.convert(currentClass, binary.rexpr)); case DIV -> new TargetBinaryOp.Div(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr));
case LESSTHAN -> new TargetBinaryOp.Less(converter.convert(binary.getType()), converter.convert(currentClass, binary.lexpr), converter.convert(currentClass, binary.rexpr)); case LESSTHAN -> new TargetBinaryOp.Less(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr));
case BIGGERTHAN -> new TargetBinaryOp.Greater(converter.convert(binary.getType()), converter.convert(currentClass, binary.lexpr), converter.convert(currentClass, binary.rexpr)); case BIGGERTHAN -> new TargetBinaryOp.Greater(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr));
case LESSEQUAL -> new TargetBinaryOp.LessOrEqual(converter.convert(binary.getType()), converter.convert(currentClass, binary.lexpr), converter.convert(currentClass, binary.rexpr)); case LESSEQUAL -> new TargetBinaryOp.LessOrEqual(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr));
case BIGGEREQUAL -> new TargetBinaryOp.GreaterOrEqual(converter.convert(binary.getType()), converter.convert(currentClass, binary.lexpr), converter.convert(currentClass, binary.rexpr)); case BIGGEREQUAL -> new TargetBinaryOp.GreaterOrEqual(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr));
case EQUAL -> new TargetBinaryOp.Equal(converter.convert(binary.getType()), converter.convert(currentClass, binary.lexpr), converter.convert(currentClass, binary.rexpr)); case EQUAL -> new TargetBinaryOp.Equal(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr));
case NOTEQUAL -> new TargetBinaryOp.NotEqual(converter.convert(binary.getType()), converter.convert(currentClass, binary.lexpr), converter.convert(currentClass, binary.rexpr)); case NOTEQUAL -> new TargetBinaryOp.NotEqual(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr));
}; };
} }
@ -126,12 +123,12 @@ public class StatementToTargetExpression implements ASTVisitor {
@Override @Override
public void visit(Block block) { public void visit(Block block) {
result = converter.convert(currentClass, block); result = converter.convert(block);
} }
@Override @Override
public void visit(CastExpr castExpr) { public void visit(CastExpr castExpr) {
result = new TargetCast(converter.convert(castExpr.getType()), converter.convert(currentClass, castExpr.expr)); result = new TargetCast(converter.convert(castExpr.getType()), converter.convert(castExpr.expr));
} }
@Override @Override
@ -141,32 +138,32 @@ public class StatementToTargetExpression implements ASTVisitor {
@Override @Override
public void visit(FieldVar fieldVar) { public void visit(FieldVar fieldVar) {
result = new TargetFieldVar(converter.convert(fieldVar.getType()), converter.convert(fieldVar.receiver.getType()), fieldVar.isStatic, converter.convert(currentClass, fieldVar.receiver), fieldVar.fieldVarName); result = new TargetFieldVar(converter.convert(fieldVar.getType()), converter.convert(fieldVar.receiver.getType()), fieldVar.isStatic, converter.convert(fieldVar.receiver), fieldVar.fieldVarName);
} }
@Override @Override
public void visit(ForStmt forStmt) { public void visit(ForStmt forStmt) {
result = new TargetFor( result = new TargetFor(
forStmt.initializer.stream().map(c -> converter.convert(currentClass, c)).toList(), forStmt.initializer.stream().map(converter::convert).toList(),
forStmt.condition != null ? converter.convert(currentClass, forStmt.condition) : null, forStmt.condition != null ? converter.convert(forStmt.condition) : null,
forStmt.loopExpr.stream().map(c -> converter.convert(currentClass, c)).toList(), forStmt.loopExpr.stream().map(converter::convert).toList(),
converter.convertWrapInBlock(currentClass, forStmt.block) converter.convertWrapInBlock(forStmt.block)
); );
} }
@Override @Override
public void visit(ForEachStmt forEachStmt) { public void visit(ForEachStmt forEachStmt) {
result = new TargetForEach(converter.convert(currentClass, forEachStmt.statement), converter.convert(currentClass, forEachStmt.expression), converter.convertWrapInBlock(currentClass, forEachStmt.block)); result = new TargetForEach(converter.convert(forEachStmt.statement), converter.convert(forEachStmt.expression), converter.convertWrapInBlock(forEachStmt.block));
} }
@Override @Override
public void visit(IfStmt ifStmt) { public void visit(IfStmt ifStmt) {
result = new TargetIf(converter.convert(currentClass, ifStmt.expr), converter.convertWrapInBlock(currentClass, ifStmt.then_block), ifStmt.else_block != null ? converter.convertWrapInBlock(currentClass, ifStmt.else_block) : null); result = new TargetIf(converter.convert(ifStmt.expr), converter.convertWrapInBlock(ifStmt.then_block), ifStmt.else_block != null ? converter.convertWrapInBlock(ifStmt.else_block) : null);
} }
@Override @Override
public void visit(InstanceOf instanceOf) { public void visit(InstanceOf instanceOf) {
result = new TargetInstanceOf(converter.convert(currentClass, instanceOf.getExpression()), converter.convert(currentClass, instanceOf.getPattern())); result = new TargetInstanceOf(converter.convert(instanceOf.getExpression()), converter.convert(instanceOf.getPattern()));
} }
@Override @Override
@ -206,10 +203,10 @@ public class StatementToTargetExpression implements ASTVisitor {
var isPrivate = false; var isPrivate = false;
var signature = methodCall.signatureArguments().stream().map(converter::convert).toList(); var signature = methodCall.signatureArguments().stream().map(converter::convert).toList();
var receiverClass = currentClass; var receiverClass = converter.currentClass;
if (methodCall.receiver instanceof ExpressionReceiver expressionReceiver && expressionReceiver.expr instanceof This) { if (methodCall.receiver instanceof ExpressionReceiver expressionReceiver && expressionReceiver.expr instanceof This) {
var thisMethod = converter.findMethod(currentClass, methodCall.name, signature); var thisMethod = converter.findMethod(converter.currentClass, methodCall.name, signature);
foundMethod = thisMethod.orElseGet(() -> findMethod(currentClass.getSuperClass().getName(), methodCall.name, signature).orElseThrow()); foundMethod = thisMethod.orElseGet(() -> findMethod(converter.currentClass.getSuperClass().getName(), methodCall.name, signature).orElseThrow());
} else if (!isFunNType) { } else if (!isFunNType) {
receiverClass = converter.compiler.getClass(receiverName); receiverClass = converter.compiler.getClass(receiverName);
foundMethod = findMethod(receiverName, methodCall.name, signature).orElseThrow(); foundMethod = findMethod(receiverName, methodCall.name, signature).orElseThrow();
@ -223,7 +220,7 @@ public class StatementToTargetExpression implements ASTVisitor {
isInterface = receiverClass.isInterface(); isInterface = receiverClass.isInterface();
} }
result = new TargetMethodCall(converter.convert(methodCall.getType()), returnType, argList, converter.convert(currentClass, methodCall.receiver), methodCall.getArgumentList().getArguments().stream().map(c -> converter.convert(currentClass, c)).toList(), receiverType, methodCall.name, isStatic, isInterface, isPrivate); result = new TargetMethodCall(converter.convert(methodCall.getType()), returnType, argList, converter.convert(methodCall.receiver), methodCall.getArgumentList().getArguments().stream().map(converter::convert).toList(), receiverType, methodCall.name, isStatic, isInterface, isPrivate);
} }
@Override @Override
@ -231,7 +228,7 @@ public class StatementToTargetExpression implements ASTVisitor {
var receiverName = new JavaClassName(newClass.name); var receiverName = new JavaClassName(newClass.name);
var ctor = converter.findConstructor(converter.compiler.getClass(receiverName), newClass.signatureArguments().stream().map(converter::convert).toList()); var ctor = converter.findConstructor(converter.compiler.getClass(receiverName), newClass.signatureArguments().stream().map(converter::convert).toList());
var signature = ctor.orElseThrow().getParameterList().getFormalparalist().stream().map(e -> converter.convert(e.getType())).toList(); var signature = ctor.orElseThrow().getParameterList().getFormalparalist().stream().map(e -> converter.convert(e.getType())).toList();
result = new TargetNew(new TargetRefType(newClass.name), signature, newClass.getArgumentList().getArguments().stream().map(c -> converter.convert(currentClass, c)).toList()); result = new TargetNew(new TargetRefType(newClass.name), signature, newClass.getArgumentList().getArguments().stream().map(converter::convert).toList());
} }
@Override @Override
@ -242,7 +239,7 @@ public class StatementToTargetExpression implements ASTVisitor {
@Override @Override
public void visit(Return aReturn) { public void visit(Return aReturn) {
result = new TargetReturn(converter.convert(currentClass, aReturn.retexpr)); result = new TargetReturn(converter.convert(aReturn.retexpr));
} }
@Override @Override
@ -272,7 +269,7 @@ public class StatementToTargetExpression implements ASTVisitor {
@Override @Override
public void visit(WhileStmt whileStmt) { public void visit(WhileStmt whileStmt) {
result = new TargetWhile(converter.convert(currentClass, whileStmt.expr), converter.convert(currentClass, whileStmt.loopBlock)); result = new TargetWhile(converter.convert(whileStmt.expr), converter.convert(whileStmt.loopBlock));
} }
@Override @Override
@ -283,47 +280,47 @@ public class StatementToTargetExpression implements ASTVisitor {
// TODO These two might not be necessary // TODO These two might not be necessary
@Override @Override
public void visit(AssignToField assignLeftSide) { public void visit(AssignToField assignLeftSide) {
result = converter.convert(currentClass, assignLeftSide.field); result = converter.convert(assignLeftSide.field);
} }
@Override @Override
public void visit(AssignToLocal assignLeftSide) { public void visit(AssignToLocal assignLeftSide) {
result = converter.convert(currentClass, assignLeftSide.localVar); result = converter.convert(assignLeftSide.localVar);
} }
@Override @Override
public void visit(SuperCall superCall) { public void visit(SuperCall superCall) {
var aSuper = converter.convert(currentClass.getSuperClass()); var aSuper = converter.convert(converter.currentClass.getSuperClass());
var type = converter.convert(superCall.getType()); var type = converter.convert(superCall.getType());
var parameters = superCall.arglist.getArguments().stream().map(par -> converter.convert(par.getType())).toList(); var parameters = superCall.arglist.getArguments().stream().map(par -> converter.convert(par.getType())).toList();
result = new TargetMethodCall(type, type, parameters, new TargetSuper(aSuper), superCall.getArgumentList().getArguments().stream().map(c -> converter.convert(currentClass, c)).toList(), aSuper, superCall.name, false, false, false); result = new TargetMethodCall(type, type, parameters, new TargetSuper(aSuper), superCall.getArgumentList().getArguments().stream().map(converter::convert).toList(), aSuper, superCall.name, false, false, false);
} }
@Override @Override
public void visit(ThisCall thisCall) { public void visit(ThisCall thisCall) {
var aThis = converter.convert(new RefType(currentClass.getClassName(), new NullToken())); var aThis = converter.convert(new RefType(converter.currentClass.getClassName(), new NullToken()));
var type = converter.convert(thisCall.getType()); var type = converter.convert(thisCall.getType());
var parameters = thisCall.arglist.getArguments().stream().map(par -> converter.convert(par.getType())).toList(); var parameters = thisCall.arglist.getArguments().stream().map(par -> converter.convert(par.getType())).toList();
result = new TargetMethodCall(type, type, parameters, new TargetThis(aThis), thisCall.getArgumentList().getArguments().stream().map(c -> converter.convert(currentClass, c)).toList(), aThis, thisCall.name, false, false, false); result = new TargetMethodCall(type, type, parameters, new TargetThis(aThis), thisCall.getArgumentList().getArguments().stream().map(converter::convert).toList(), aThis, thisCall.name, false, false, false);
} }
@Override @Override
public void visit(ExpressionReceiver expressionReceiver) { public void visit(ExpressionReceiver expressionReceiver) {
result = converter.convert(currentClass, expressionReceiver.expr); result = converter.convert(expressionReceiver.expr);
} }
@Override @Override
public void visit(UnaryExpr unaryExpr) { public void visit(UnaryExpr unaryExpr) {
result = switch (unaryExpr.operation) { result = switch (unaryExpr.operation) {
case NOT -> new TargetUnaryOp.Not(converter.convert(unaryExpr.getType()), converter.convert(currentClass, unaryExpr.expr)); case NOT -> new TargetUnaryOp.Not(converter.convert(unaryExpr.getType()), converter.convert(unaryExpr.expr));
case MINUS -> new TargetUnaryOp.Negate(converter.convert(unaryExpr.getType()), converter.convert(currentClass, unaryExpr.expr)); case MINUS -> new TargetUnaryOp.Negate(converter.convert(unaryExpr.getType()), converter.convert(unaryExpr.expr));
case PREINCREMENT -> new TargetUnaryOp.PreIncrement(converter.convert(unaryExpr.getType()), converter.convert(currentClass, unaryExpr.expr)); case PREINCREMENT -> new TargetUnaryOp.PreIncrement(converter.convert(unaryExpr.getType()), converter.convert(unaryExpr.expr));
case PREDECREMENT -> new TargetUnaryOp.PreDecrement(converter.convert(unaryExpr.getType()), converter.convert(currentClass, unaryExpr.expr)); case PREDECREMENT -> new TargetUnaryOp.PreDecrement(converter.convert(unaryExpr.getType()), converter.convert(unaryExpr.expr));
case POSTINCREMENT -> new TargetUnaryOp.PostIncrement(converter.convert(unaryExpr.getType()), converter.convert(currentClass, unaryExpr.expr)); case POSTINCREMENT -> new TargetUnaryOp.PostIncrement(converter.convert(unaryExpr.getType()), converter.convert(unaryExpr.expr));
case PLUS -> new TargetUnaryOp.Add(converter.convert(unaryExpr.getType()), converter.convert(currentClass, unaryExpr.expr)); case PLUS -> new TargetUnaryOp.Add(converter.convert(unaryExpr.getType()), converter.convert(unaryExpr.expr));
case POSTDECREMENT -> new TargetUnaryOp.PostDecrement(converter.convert(unaryExpr.getType()), converter.convert(currentClass, unaryExpr.expr)); case POSTDECREMENT -> new TargetUnaryOp.PostDecrement(converter.convert(unaryExpr.getType()), converter.convert(unaryExpr.expr));
}; };
} }
@ -351,20 +348,20 @@ public class StatementToTargetExpression implements ASTVisitor {
@Override @Override
public void visit(Throw aThrow) { public void visit(Throw aThrow) {
result = new TargetThrow(converter.convert(currentClass, aThrow.expr)); result = new TargetThrow(converter.convert(aThrow.expr));
} }
@Override @Override
public void visit(Switch switchStmt) { public void visit(Switch switchStmt) {
var cases = switchStmt.getBlocks().stream().filter(s -> !s.isDefault()).map(c -> converter.convert(currentClass, c)).toList(); var cases = switchStmt.getBlocks().stream().filter(s -> !s.isDefault()).map(converter::convert).toList();
TargetSwitch.Case default_ = null; TargetSwitch.Case default_ = null;
for (var block : switchStmt.getBlocks()) { for (var block : switchStmt.getBlocks()) {
if (block.isDefault()) { if (block.isDefault()) {
default_ = new TargetSwitch.Case(converter.convert(currentClass, (Block) block), block.isExpression); default_ = new TargetSwitch.Case(converter.convert((Block) block), block.isExpression);
} }
} }
result = new TargetSwitch(converter.convert(currentClass, switchStmt.getSwitch()), cases, default_ , converter.convert(switchStmt.getType()), !switchStmt.getStatement()); result = new TargetSwitch(converter.convert(switchStmt.getSwitch()), cases, default_ , converter.convert(switchStmt.getType()), !switchStmt.getStatement());
} }
@Override @Override
@ -372,12 +369,12 @@ public class StatementToTargetExpression implements ASTVisitor {
@Override @Override
public void visit(SwitchLabel switchLabel) { public void visit(SwitchLabel switchLabel) {
result = converter.convert(currentClass, switchLabel.getPattern()); result = converter.convert(switchLabel.getPattern());
} }
@Override @Override
public void visit(Yield aYield) { public void visit(Yield aYield) {
result = new TargetYield(converter.convert(currentClass, aYield.retexpr)); result = new TargetYield(converter.convert(aYield.retexpr));
} }
@Override @Override
@ -452,7 +449,7 @@ public class StatementToTargetExpression implements ASTVisitor {
@Override @Override
public void visit(ExpressionPattern aPattern) { public void visit(ExpressionPattern aPattern) {
result = converter.convert(currentClass, aPattern.getExpression()); result = converter.convert(aPattern.getExpression());
} }
@Override @Override
@ -460,12 +457,12 @@ public class StatementToTargetExpression implements ASTVisitor {
result = new TargetComplexPattern( result = new TargetComplexPattern(
converter.convert(aRecordPattern.getType()), converter.convert(aRecordPattern.getType()),
aRecordPattern.getName(), aRecordPattern.getName(),
aRecordPattern.getSubPattern().stream().map(x -> (TargetPattern) converter.convert(currentClass, x)).toList() aRecordPattern.getSubPattern().stream().map(x -> (TargetPattern) converter.convert(x)).toList()
); );
} }
@Override @Override
public void visit(GuardedPattern aGuardedPattern) { public void visit(GuardedPattern aGuardedPattern) {
result = new TargetGuard((TargetPattern) converter.convert(currentClass, aGuardedPattern.getNestedPattern()), converter.convert(currentClass, aGuardedPattern.getCondition())); result = new TargetGuard((TargetPattern) converter.convert(aGuardedPattern.getNestedPattern()), converter.convert(aGuardedPattern.getCondition()));
} }
} }