Get rid of mutable state, hopefully fix #307
All checks were successful
Build and Test with Maven / Build-and-test-with-Maven (push) Successful in 2m57s
All checks were successful
Build and Test with Maven / Build-and-test-with-Maven (push) Successful in 2m57s
This commit is contained in:
parent
62f2e05f35
commit
bc61fc2e1d
@ -37,8 +37,6 @@ public class ASTToTargetAST {
|
||||
|
||||
public final JavaTXCompiler compiler;
|
||||
|
||||
protected ClassOrInterface currentClass; // TODO This is only needed because of SuperCall, maybe there's
|
||||
|
||||
public List<GenericsResult> txGenerics() {
|
||||
return all.stream().map(generics -> new GenericsResult(generics.txGenerics)).toList();
|
||||
}
|
||||
@ -47,8 +45,8 @@ public class ASTToTargetAST {
|
||||
return all.stream().map(generics -> new GenericsResult(generics.javaGenerics)).toList();
|
||||
}
|
||||
|
||||
public TargetExpression convert(Pattern pattern) {
|
||||
var converter = new StatementToTargetExpression(this);
|
||||
public TargetExpression convert(ClassOrInterface clazz, Pattern pattern) {
|
||||
var converter = new StatementToTargetExpression(this, clazz);
|
||||
pattern.accept(converter);
|
||||
return converter.result;
|
||||
}
|
||||
@ -132,7 +130,6 @@ public class ASTToTargetAST {
|
||||
}
|
||||
|
||||
public TargetStructure convert(ClassOrInterface input) {
|
||||
currentClass = input;
|
||||
Set<TargetGeneric> javaGenerics = new HashSet<>();
|
||||
Set<TargetGeneric> txGenerics = new HashSet<>();
|
||||
|
||||
@ -156,17 +153,17 @@ public class ASTToTargetAST {
|
||||
|
||||
TargetBlock fieldInitializer = null;
|
||||
if (input.getfieldInitializations().isPresent())
|
||||
fieldInitializer = convert(input.getfieldInitializations().get().block);
|
||||
fieldInitializer = convert(input, input.getfieldInitializations().get().block);
|
||||
TargetBlock finalFieldInitializer = fieldInitializer;
|
||||
|
||||
var superInterfaces = input.getSuperInterfaces().stream().map(clazz -> convert(clazz, generics.javaGenerics)).toList();
|
||||
var constructors = input.getConstructors().stream().map(constructor -> this.convert(constructor, finalFieldInitializer)).flatMap(List::stream).toList();
|
||||
var constructors = input.getConstructors().stream().map(constructor -> this.convert(input, constructor, finalFieldInitializer)).flatMap(List::stream).toList();
|
||||
var fields = input.getFieldDecl().stream().map(this::convert).toList();
|
||||
var methods = groupOverloads(input.getMethods()).stream().map(this::convert).flatMap(List::stream).toList();
|
||||
var methods = groupOverloads(input.getMethods()).stream().map(m -> convert(input, m)).flatMap(List::stream).toList();
|
||||
|
||||
TargetMethod staticConstructor = null;
|
||||
if (input.getStaticInitializer().isPresent())
|
||||
staticConstructor = this.convert(input.getStaticInitializer().get()).get(0);
|
||||
staticConstructor = this.convert(input, input.getStaticInitializer().get()).get(0);
|
||||
|
||||
if (input instanceof Record)
|
||||
return new TargetRecord(input.getModifiers(), input.getClassName(), javaGenerics, txGenerics, superInterfaces, constructors, staticConstructor, fields, methods);
|
||||
@ -175,9 +172,9 @@ public class ASTToTargetAST {
|
||||
else return new TargetClass(input.getModifiers(), input.getClassName(), convert(input.getSuperClass(), generics.javaGenerics), javaGenerics, txGenerics, superInterfaces, constructors, staticConstructor, fields, methods);
|
||||
}
|
||||
|
||||
private List<MethodParameter> convert(ParameterList input, GenerateGenerics generics) {
|
||||
private List<MethodParameter> convert(ClassOrInterface clazz, ParameterList input, GenerateGenerics generics) {
|
||||
return input.getFormalparalist().stream().map(param ->
|
||||
new MethodParameter((TargetPattern) convert(param))
|
||||
new MethodParameter((TargetPattern) convert(clazz, param))
|
||||
).toList();
|
||||
}
|
||||
|
||||
@ -185,7 +182,7 @@ public class ASTToTargetAST {
|
||||
return generics.stream().anyMatch(g -> g.name().equals(type.getParsedName()));
|
||||
}
|
||||
|
||||
private Set<TargetGeneric> collectMethodGenerics(GenerateGenerics generateGenerics, Set<GenerateGenerics.Pair> generics, Method input) {
|
||||
private Set<TargetGeneric> collectMethodGenerics(ClassOrInterface currentClass, GenerateGenerics generateGenerics, Set<GenerateGenerics.Pair> generics, Method input) {
|
||||
var convertedGenerics = new HashSet<>(convert(generics, generateGenerics));
|
||||
outer: for (GenericTypeVar typeVar : input.getGenerics()) {
|
||||
for (var classGeneric : currentClass.getGenerics()) {
|
||||
@ -203,22 +200,22 @@ public class ASTToTargetAST {
|
||||
return convertedGenerics;
|
||||
}
|
||||
|
||||
private List<TargetConstructor> convert(Constructor input, TargetBlock fieldInitializer) {
|
||||
private List<TargetConstructor> convert(ClassOrInterface clazz, Constructor input, TargetBlock fieldInitializer) {
|
||||
generics = all.get(0);
|
||||
List<TargetConstructor> result = new ArrayList<>();
|
||||
Set<List<MethodParameter>> parameterSet = new HashSet<>();
|
||||
|
||||
for (var s : all) {
|
||||
generics = s;
|
||||
var javaGenerics = this.generics.javaGenerics.generics(currentClass, input);
|
||||
var txGenerics = this.generics.txGenerics.generics(currentClass, input);
|
||||
List<MethodParameter> params = convert(input.getParameterList(), this.generics.javaGenerics);
|
||||
var javaGenerics = this.generics.javaGenerics.generics(clazz, input);
|
||||
var txGenerics = this.generics.txGenerics.generics(clazz, input);
|
||||
List<MethodParameter> params = convert(clazz, input.getParameterList(), this.generics.javaGenerics);
|
||||
if (parameterSet.stream().noneMatch(p -> p.equals(params))) {
|
||||
List<MethodParameter> txParams = convert(input.getParameterList(), this.generics.txGenerics);
|
||||
var javaMethodGenerics = collectMethodGenerics(generics.javaGenerics(), javaGenerics, input);
|
||||
var txMethodGenerics = collectMethodGenerics(generics.txGenerics(), txGenerics, input);
|
||||
List<MethodParameter> txParams = convert(clazz, input.getParameterList(), this.generics.txGenerics);
|
||||
var javaMethodGenerics = collectMethodGenerics(clazz, generics.javaGenerics(), javaGenerics, input);
|
||||
var txMethodGenerics = collectMethodGenerics(clazz, generics.txGenerics(), txGenerics, input);
|
||||
|
||||
result.add(new TargetConstructor(input.modifier, javaMethodGenerics, txMethodGenerics, params, txParams, convert(input.block), fieldInitializer));
|
||||
result.add(new TargetConstructor(input.modifier, javaMethodGenerics, txMethodGenerics, params, txParams, convert(clazz, input.block), fieldInitializer));
|
||||
parameterSet.add(params);
|
||||
}
|
||||
}
|
||||
@ -291,9 +288,9 @@ public class ASTToTargetAST {
|
||||
return res.toString();
|
||||
}
|
||||
|
||||
private List<TargetMethod> convert(List<Method> overloadedMethods) {
|
||||
private List<TargetMethod> convert(ClassOrInterface currentClass, List<Method> overloadedMethods) {
|
||||
if (overloadedMethods.size() == 1) {
|
||||
return convert(overloadedMethods.get(0));
|
||||
return convert(currentClass, overloadedMethods.get(0));
|
||||
}
|
||||
var res = new ArrayList<Method>();
|
||||
for (var method : overloadedMethods) {
|
||||
@ -330,7 +327,7 @@ public class ASTToTargetAST {
|
||||
var entryPoint = new Method(template.modifier, template.name, template.getReturnType(), params, block, template.getGenerics(), new NullToken());
|
||||
|
||||
res.add(entryPoint); // TODO*/
|
||||
return res.stream().map(this::convert).flatMap(List::stream).toList();
|
||||
return res.stream().map(m -> this.convert(currentClass, m)).flatMap(List::stream).toList();
|
||||
}
|
||||
|
||||
private Expression makeRecordSwitch(RefTypeOrTPHOrWildcardOrGeneric returnType, ParameterList params, List<Method> overloadedMethods) {
|
||||
@ -354,7 +351,7 @@ public class ASTToTargetAST {
|
||||
return swtch;
|
||||
}
|
||||
|
||||
private Optional<Method> findSuperMethodToOverride(String name, List<MethodParameter> params) {
|
||||
private Optional<Method> findSuperMethodToOverride(ClassOrInterface currentClass, String name, List<MethodParameter> params) {
|
||||
var superClass = compiler.getClass(currentClass.getSuperClass().getName());
|
||||
var methodStream = superClass.getMethods().stream();
|
||||
for (var superInterface : currentClass.getSuperInterfaces()) {
|
||||
@ -375,7 +372,7 @@ public class ASTToTargetAST {
|
||||
}).findFirst();
|
||||
}
|
||||
|
||||
private List<TargetMethod> convert(Method method) {
|
||||
private List<TargetMethod> convert(ClassOrInterface currentClass, Method method) {
|
||||
generics = all.get(0);
|
||||
List<TargetMethod> result = new ArrayList<>();
|
||||
Set<List<MethodParameter>> parameterSet = new HashSet<>();
|
||||
@ -384,58 +381,58 @@ public class ASTToTargetAST {
|
||||
generics = s;
|
||||
var javaGenerics = this.generics.javaGenerics.generics(currentClass, method);
|
||||
var txGenerics = this.generics.txGenerics.generics(currentClass, method);
|
||||
List<MethodParameter> params = convert(method.getParameterList(), this.generics.javaGenerics);
|
||||
List<MethodParameter> params = convert(currentClass, method.getParameterList(), this.generics.javaGenerics);
|
||||
var returnType = convert(method.getReturnType(), this.generics.javaGenerics);
|
||||
var superMethod = findSuperMethodToOverride(method.getName(), params);
|
||||
var superMethod = findSuperMethodToOverride(currentClass, method.getName(), params);
|
||||
if (superMethod.isPresent()) {
|
||||
// If we find a super method to override, use its parameters and return types
|
||||
returnType = convert(superMethod.get().getReturnType(), this.generics.javaGenerics);
|
||||
params = convert(superMethod.get().getParameterList(), method.getParameterList(), this.generics.javaGenerics);
|
||||
params = convert(currentClass, superMethod.get().getParameterList(), method.getParameterList(), this.generics.javaGenerics);
|
||||
}
|
||||
|
||||
List<MethodParameter> finalParams = params;
|
||||
if (parameterSet.stream().noneMatch(p -> p.equals(finalParams))) {
|
||||
List<MethodParameter> txParams = convert(method.getParameterList(), this.generics.txGenerics);
|
||||
List<MethodParameter> txParams = convert(currentClass, method.getParameterList(), this.generics.txGenerics);
|
||||
|
||||
var javaMethodGenerics = collectMethodGenerics(generics.javaGenerics(), javaGenerics, method);
|
||||
var txMethodGenerics = collectMethodGenerics(generics.txGenerics(), txGenerics, method);
|
||||
var javaMethodGenerics = collectMethodGenerics(currentClass, generics.javaGenerics(), javaGenerics, method);
|
||||
var txMethodGenerics = collectMethodGenerics(currentClass, generics.txGenerics(), txGenerics, method);
|
||||
|
||||
var javaSignature = new TargetMethod.Signature(javaMethodGenerics, params, returnType);
|
||||
var txSignature = new TargetMethod.Signature(txMethodGenerics, txParams, convert(method.getReturnType(), this.generics.txGenerics));
|
||||
result.add(new TargetMethod(method.modifier, method.name, convert(method.block), javaSignature, txSignature));
|
||||
result.add(new TargetMethod(method.modifier, method.name, convert(currentClass, method.block), javaSignature, txSignature));
|
||||
parameterSet.add(params);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private List<MethodParameter> convert(ParameterList superList, ParameterList paraList, JavaGenerics generics) {
|
||||
private List<MethodParameter> convert(ClassOrInterface currentClass, ParameterList superList, ParameterList paraList, JavaGenerics generics) {
|
||||
var list = new ArrayList<MethodParameter>();
|
||||
for (var i = 0; i < paraList.getFormalparalist().size(); i++) {
|
||||
var param = paraList.getParameterAt(i);
|
||||
list.add(new MethodParameter((TargetPattern) convert(param)).withType(convert(superList.getParameterAt(i).getType(), generics)));
|
||||
list.add(new MethodParameter((TargetPattern) convert(currentClass, param)).withType(convert(superList.getParameterAt(i).getType(), generics)));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
protected TargetSwitch.Case convert(SwitchBlock block) {
|
||||
return new TargetSwitch.Case(block.getLabels().stream().map(this::convert).toList(), convert((Block) block), block.isExpression);
|
||||
protected TargetSwitch.Case convert(ClassOrInterface clazz, SwitchBlock block) {
|
||||
return new TargetSwitch.Case(block.getLabels().stream().map(x -> convert(clazz, x)).toList(), convert(clazz, (Block) block), block.isExpression);
|
||||
}
|
||||
|
||||
protected TargetBlock convert(Block block) {
|
||||
protected TargetBlock convert(ClassOrInterface clazz, Block block) {
|
||||
if (block == null) return null;
|
||||
return new TargetBlock(block.statements.stream().map(this::convert).toList());
|
||||
return new TargetBlock(block.statements.stream().map(x -> convert(clazz, x)).toList());
|
||||
}
|
||||
|
||||
protected TargetBlock convertWrapInBlock(Expression expression) {
|
||||
var res = convert(expression);
|
||||
protected TargetBlock convertWrapInBlock(ClassOrInterface clazz, Expression expression) {
|
||||
var res = convert(clazz, expression);
|
||||
if (!(res instanceof TargetBlock))
|
||||
return new TargetBlock(List.of(res));
|
||||
return (TargetBlock) res;
|
||||
}
|
||||
|
||||
protected TargetExpression convert(Expression expr) {
|
||||
var converter = new StatementToTargetExpression(this);
|
||||
protected TargetExpression convert(ClassOrInterface currentClass, Expression expr) {
|
||||
var converter = new StatementToTargetExpression(this, currentClass);
|
||||
expr.accept(converter);
|
||||
return converter.result;
|
||||
}
|
||||
|
@ -20,8 +20,11 @@ import java.util.stream.StreamSupport;
|
||||
|
||||
public class StatementToTargetExpression implements ASTVisitor {
|
||||
|
||||
public StatementToTargetExpression(ASTToTargetAST converter) {
|
||||
private final ClassOrInterface currentClass;
|
||||
|
||||
public StatementToTargetExpression(ASTToTargetAST converter, ClassOrInterface currentClass) {
|
||||
this.converter = converter;
|
||||
this.currentClass = currentClass;
|
||||
}
|
||||
|
||||
public TargetExpression result;
|
||||
@ -81,38 +84,38 @@ public class StatementToTargetExpression implements ASTVisitor {
|
||||
} // Don't look at lambda expressions
|
||||
});
|
||||
|
||||
result = new TargetLambdaExpression(converter.convert(lambdaExpression.getType()), captures, parameters, converter.convert(lambdaExpression.getReturnType()), converter.convert(lambdaExpression.methodBody));
|
||||
result = new TargetLambdaExpression(converter.convert(lambdaExpression.getType()), captures, parameters, converter.convert(lambdaExpression.getReturnType()), converter.convert(currentClass, lambdaExpression.methodBody));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(Assign assign) {
|
||||
TargetExpression left;
|
||||
if (assign.lefSide instanceof AssignToLocal) {
|
||||
left = converter.convert(((AssignToLocal) assign.lefSide).localVar);
|
||||
left = converter.convert(currentClass, ((AssignToLocal) assign.lefSide).localVar);
|
||||
} else {
|
||||
left = converter.convert(((AssignToField) assign.lefSide).field);
|
||||
left = converter.convert(currentClass, ((AssignToField) assign.lefSide).field);
|
||||
}
|
||||
|
||||
result = new TargetAssign(converter.convert(assign.getType()), left, converter.convert(assign.rightSide));
|
||||
result = new TargetAssign(converter.convert(assign.getType()), left, converter.convert(currentClass, assign.rightSide));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(BinaryExpr binary) {
|
||||
result = switch (binary.operation) {
|
||||
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(binary.lexpr), converter.convert(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(binary.lexpr), converter.convert(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(binary.lexpr), converter.convert(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(binary.lexpr), converter.convert(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(binary.lexpr), converter.convert(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(binary.lexpr), converter.convert(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(binary.lexpr), converter.convert(binary.rexpr));
|
||||
case ADD -> new TargetBinaryOp.Add(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(currentClass, binary.lexpr), converter.convert(currentClass, binary.rexpr));
|
||||
case MUL -> new TargetBinaryOp.Mul(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(currentClass, binary.lexpr), converter.convert(currentClass, binary.rexpr));
|
||||
case AND -> new TargetBinaryOp.BAnd(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(currentClass, binary.lexpr), converter.convert(currentClass, binary.rexpr));
|
||||
case XOR -> new TargetBinaryOp.XOr(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(currentClass, binary.lexpr), converter.convert(currentClass, binary.rexpr));
|
||||
case LESSTHAN -> new TargetBinaryOp.Less(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(currentClass, binary.lexpr), converter.convert(currentClass, binary.rexpr));
|
||||
case LESSEQUAL -> new TargetBinaryOp.LessOrEqual(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(currentClass, binary.lexpr), converter.convert(currentClass, binary.rexpr));
|
||||
case EQUAL -> new TargetBinaryOp.Equal(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(currentClass, binary.lexpr), converter.convert(currentClass, binary.rexpr));
|
||||
};
|
||||
}
|
||||
|
||||
@ -123,12 +126,12 @@ public class StatementToTargetExpression implements ASTVisitor {
|
||||
|
||||
@Override
|
||||
public void visit(Block block) {
|
||||
result = converter.convert(block);
|
||||
result = converter.convert(currentClass, block);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(CastExpr castExpr) {
|
||||
result = new TargetCast(converter.convert(castExpr.getType()), converter.convert(castExpr.expr));
|
||||
result = new TargetCast(converter.convert(castExpr.getType()), converter.convert(currentClass, castExpr.expr));
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -138,32 +141,32 @@ public class StatementToTargetExpression implements ASTVisitor {
|
||||
|
||||
@Override
|
||||
public void visit(FieldVar fieldVar) {
|
||||
result = new TargetFieldVar(converter.convert(fieldVar.getType()), converter.convert(fieldVar.receiver.getType()), fieldVar.isStatic, converter.convert(fieldVar.receiver), fieldVar.fieldVarName);
|
||||
result = new TargetFieldVar(converter.convert(fieldVar.getType()), converter.convert(fieldVar.receiver.getType()), fieldVar.isStatic, converter.convert(currentClass, fieldVar.receiver), fieldVar.fieldVarName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(ForStmt forStmt) {
|
||||
result = new TargetFor(
|
||||
forStmt.initializer.stream().map(converter::convert).toList(),
|
||||
forStmt.condition != null ? converter.convert(forStmt.condition) : null,
|
||||
forStmt.loopExpr.stream().map(converter::convert).toList(),
|
||||
converter.convertWrapInBlock(forStmt.block)
|
||||
forStmt.initializer.stream().map(c -> converter.convert(currentClass, c)).toList(),
|
||||
forStmt.condition != null ? converter.convert(currentClass, forStmt.condition) : null,
|
||||
forStmt.loopExpr.stream().map(c -> converter.convert(currentClass, c)).toList(),
|
||||
converter.convertWrapInBlock(currentClass, forStmt.block)
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(ForEachStmt forEachStmt) {
|
||||
result = new TargetForEach(converter.convert(forEachStmt.statement), converter.convert(forEachStmt.expression), converter.convertWrapInBlock(forEachStmt.block));
|
||||
result = new TargetForEach(converter.convert(currentClass, forEachStmt.statement), converter.convert(currentClass, forEachStmt.expression), converter.convertWrapInBlock(currentClass, forEachStmt.block));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(IfStmt ifStmt) {
|
||||
result = new TargetIf(converter.convert(ifStmt.expr), converter.convertWrapInBlock(ifStmt.then_block), ifStmt.else_block != null ? converter.convertWrapInBlock(ifStmt.else_block) : null);
|
||||
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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(InstanceOf instanceOf) {
|
||||
result = new TargetInstanceOf(converter.convert(instanceOf.getExpression()), converter.convert(instanceOf.getPattern()));
|
||||
result = new TargetInstanceOf(converter.convert(currentClass, instanceOf.getExpression()), converter.convert(currentClass, instanceOf.getPattern()));
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -203,10 +206,10 @@ public class StatementToTargetExpression implements ASTVisitor {
|
||||
var isPrivate = false;
|
||||
var signature = methodCall.signatureArguments().stream().map(converter::convert).toList();
|
||||
|
||||
var receiverClass = converter.currentClass;
|
||||
var receiverClass = currentClass;
|
||||
if (methodCall.receiver instanceof ExpressionReceiver expressionReceiver && expressionReceiver.expr instanceof This) {
|
||||
var thisMethod = converter.findMethod(converter.currentClass, methodCall.name, signature);
|
||||
foundMethod = thisMethod.orElseGet(() -> findMethod(converter.currentClass.getSuperClass().getName(), methodCall.name, signature).orElseThrow());
|
||||
var thisMethod = converter.findMethod(currentClass, methodCall.name, signature);
|
||||
foundMethod = thisMethod.orElseGet(() -> findMethod(currentClass.getSuperClass().getName(), methodCall.name, signature).orElseThrow());
|
||||
} else if (!isFunNType) {
|
||||
receiverClass = converter.compiler.getClass(receiverName);
|
||||
foundMethod = findMethod(receiverName, methodCall.name, signature).orElseThrow();
|
||||
@ -220,7 +223,7 @@ public class StatementToTargetExpression implements ASTVisitor {
|
||||
isInterface = receiverClass.isInterface();
|
||||
}
|
||||
|
||||
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);
|
||||
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);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -228,7 +231,7 @@ public class StatementToTargetExpression implements ASTVisitor {
|
||||
var receiverName = new JavaClassName(newClass.name);
|
||||
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();
|
||||
result = new TargetNew(new TargetRefType(newClass.name), signature, newClass.getArgumentList().getArguments().stream().map(converter::convert).toList());
|
||||
result = new TargetNew(new TargetRefType(newClass.name), signature, newClass.getArgumentList().getArguments().stream().map(c -> converter.convert(currentClass, c)).toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -239,7 +242,7 @@ public class StatementToTargetExpression implements ASTVisitor {
|
||||
|
||||
@Override
|
||||
public void visit(Return aReturn) {
|
||||
result = new TargetReturn(converter.convert(aReturn.retexpr));
|
||||
result = new TargetReturn(converter.convert(currentClass, aReturn.retexpr));
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -269,7 +272,7 @@ public class StatementToTargetExpression implements ASTVisitor {
|
||||
|
||||
@Override
|
||||
public void visit(WhileStmt whileStmt) {
|
||||
result = new TargetWhile(converter.convert(whileStmt.expr), converter.convert(whileStmt.loopBlock));
|
||||
result = new TargetWhile(converter.convert(currentClass, whileStmt.expr), converter.convert(currentClass, whileStmt.loopBlock));
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -280,47 +283,47 @@ public class StatementToTargetExpression implements ASTVisitor {
|
||||
// TODO These two might not be necessary
|
||||
@Override
|
||||
public void visit(AssignToField assignLeftSide) {
|
||||
result = converter.convert(assignLeftSide.field);
|
||||
result = converter.convert(currentClass, assignLeftSide.field);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(AssignToLocal assignLeftSide) {
|
||||
result = converter.convert(assignLeftSide.localVar);
|
||||
result = converter.convert(currentClass, assignLeftSide.localVar);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(SuperCall superCall) {
|
||||
var aSuper = converter.convert(converter.currentClass.getSuperClass());
|
||||
var aSuper = converter.convert(currentClass.getSuperClass());
|
||||
var type = converter.convert(superCall.getType());
|
||||
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(converter::convert).toList(), aSuper, superCall.name, false, false, false);
|
||||
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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(ThisCall thisCall) {
|
||||
var aThis = converter.convert(new RefType(converter.currentClass.getClassName(), new NullToken()));
|
||||
var aThis = converter.convert(new RefType(currentClass.getClassName(), new NullToken()));
|
||||
var type = converter.convert(thisCall.getType());
|
||||
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(converter::convert).toList(), aThis, thisCall.name, false, false, false);
|
||||
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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(ExpressionReceiver expressionReceiver) {
|
||||
result = converter.convert(expressionReceiver.expr);
|
||||
result = converter.convert(currentClass, expressionReceiver.expr);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(UnaryExpr unaryExpr) {
|
||||
result = switch (unaryExpr.operation) {
|
||||
case NOT -> new TargetUnaryOp.Not(converter.convert(unaryExpr.getType()), converter.convert(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(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(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(unaryExpr.expr));
|
||||
case NOT -> new TargetUnaryOp.Not(converter.convert(unaryExpr.getType()), converter.convert(currentClass, unaryExpr.expr));
|
||||
case MINUS -> new TargetUnaryOp.Negate(converter.convert(unaryExpr.getType()), converter.convert(currentClass, unaryExpr.expr));
|
||||
case PREINCREMENT -> new TargetUnaryOp.PreIncrement(converter.convert(unaryExpr.getType()), converter.convert(currentClass, unaryExpr.expr));
|
||||
case PREDECREMENT -> new TargetUnaryOp.PreDecrement(converter.convert(unaryExpr.getType()), converter.convert(currentClass, unaryExpr.expr));
|
||||
case POSTINCREMENT -> new TargetUnaryOp.PostIncrement(converter.convert(unaryExpr.getType()), converter.convert(currentClass, unaryExpr.expr));
|
||||
case PLUS -> new TargetUnaryOp.Add(converter.convert(unaryExpr.getType()), converter.convert(currentClass, unaryExpr.expr));
|
||||
case POSTDECREMENT -> new TargetUnaryOp.PostDecrement(converter.convert(unaryExpr.getType()), converter.convert(currentClass, unaryExpr.expr));
|
||||
};
|
||||
}
|
||||
|
||||
@ -348,20 +351,20 @@ public class StatementToTargetExpression implements ASTVisitor {
|
||||
|
||||
@Override
|
||||
public void visit(Throw aThrow) {
|
||||
result = new TargetThrow(converter.convert(aThrow.expr));
|
||||
result = new TargetThrow(converter.convert(currentClass, aThrow.expr));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(Switch switchStmt) {
|
||||
var cases = switchStmt.getBlocks().stream().filter(s -> !s.isDefault()).map(converter::convert).toList();
|
||||
var cases = switchStmt.getBlocks().stream().filter(s -> !s.isDefault()).map(c -> converter.convert(currentClass, c)).toList();
|
||||
|
||||
TargetSwitch.Case default_ = null;
|
||||
for (var block : switchStmt.getBlocks()) {
|
||||
if (block.isDefault()) {
|
||||
default_ = new TargetSwitch.Case(converter.convert((Block) block), block.isExpression);
|
||||
default_ = new TargetSwitch.Case(converter.convert(currentClass, (Block) block), block.isExpression);
|
||||
}
|
||||
}
|
||||
result = new TargetSwitch(converter.convert(switchStmt.getSwitch()), cases, default_ , converter.convert(switchStmt.getType()), !switchStmt.getStatement());
|
||||
result = new TargetSwitch(converter.convert(currentClass, switchStmt.getSwitch()), cases, default_ , converter.convert(switchStmt.getType()), !switchStmt.getStatement());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -369,12 +372,12 @@ public class StatementToTargetExpression implements ASTVisitor {
|
||||
|
||||
@Override
|
||||
public void visit(SwitchLabel switchLabel) {
|
||||
result = converter.convert(switchLabel.getPattern());
|
||||
result = converter.convert(currentClass, switchLabel.getPattern());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(Yield aYield) {
|
||||
result = new TargetYield(converter.convert(aYield.retexpr));
|
||||
result = new TargetYield(converter.convert(currentClass, aYield.retexpr));
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -449,7 +452,7 @@ public class StatementToTargetExpression implements ASTVisitor {
|
||||
|
||||
@Override
|
||||
public void visit(ExpressionPattern aPattern) {
|
||||
result = converter.convert(aPattern.getExpression());
|
||||
result = converter.convert(currentClass, aPattern.getExpression());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -457,12 +460,12 @@ public class StatementToTargetExpression implements ASTVisitor {
|
||||
result = new TargetComplexPattern(
|
||||
converter.convert(aRecordPattern.getType()),
|
||||
aRecordPattern.getName(),
|
||||
aRecordPattern.getSubPattern().stream().map(x -> (TargetPattern) converter.convert(x)).toList()
|
||||
aRecordPattern.getSubPattern().stream().map(x -> (TargetPattern) converter.convert(currentClass, x)).toList()
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(GuardedPattern aGuardedPattern) {
|
||||
result = new TargetGuard((TargetPattern) converter.convert(aGuardedPattern.getNestedPattern()), converter.convert(aGuardedPattern.getCondition()));
|
||||
result = new TargetGuard((TargetPattern) converter.convert(currentClass, aGuardedPattern.getNestedPattern()), converter.convert(currentClass, aGuardedPattern.getCondition()));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user