fix wrong field attribute

This commit is contained in:
Boolean-true 2024-06-29 22:10:35 +02:00
parent 878794ffa7
commit f500eff446
2 changed files with 29 additions and 14 deletions

View File

@ -46,7 +46,8 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
Expression recipient = null;
if (ctx.fieldVarAccess().recipient() != null) {
List<DecafParser.RecipientContext> recipientList = ctx.fieldVarAccess().recipient();
recipient = ExpressionGenerator.generateRecursiveOwnerChain(recipientList, ctx.fieldVarAccess().newCall() != null ? StatementGenerator.generateNew(ctx.fieldVarAccess().newCall()) : null);
recipient = ExpressionGenerator.generateRecursiveOwnerChain(recipientList, ctx.fieldVarAccess().newCall() != null ? StatementGenerator.generateNew(ctx.fieldVarAccess().newCall()) : null, isField);
return new FieldVarAccess(true, recipient, ctx.fieldVarAccess().id().IDENTIFIER().getText());
}
return new FieldVarAccess(isField, recipient, ctx.fieldVarAccess().id().IDENTIFIER().getText());
}
@ -106,7 +107,7 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
Expression recursiveOwnerChain = null;
if (ctx.methCall().recipient() != null) {
List<DecafParser.RecipientContext> recipientList = ctx.methCall().recipient();
recursiveOwnerChain = ExpressionGenerator.generateRecursiveOwnerChain(recipientList, ctx.methCall().newCall() != null ? StatementGenerator.generateNew(ctx.methCall().newCall()) : null);
recursiveOwnerChain = ExpressionGenerator.generateRecursiveOwnerChain(recipientList, ctx.methCall().newCall() != null ? StatementGenerator.generateNew(ctx.methCall().newCall()) : null, isField);
}
List<Expression> args = new ArrayList<>();
if (ctx.methCall().methName().args() != null) {
@ -115,7 +116,10 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
args.add(astExpr);
}
}
return new MethodCall(new FieldVarAccess(isField, recursiveOwnerChain, ctx.methCall().methName().id().IDENTIFIER().getText()), args);
if (recursiveOwnerChain == null) {
return new MethodCall(new FieldVarAccess(isField, null, ctx.methCall().methName().id().IDENTIFIER().getText()), args);
}
return new MethodCall(new FieldVarAccess(true, recursiveOwnerChain, ctx.methCall().methName().id().IDENTIFIER().getText()), args);
}
@Override
@ -124,7 +128,7 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
}
public static Expression generateRecursiveOwnerChain(List<DecafParser.RecipientContext> ctxList, Expression recipient) {
public static Expression generateRecursiveOwnerChain(List<DecafParser.RecipientContext> ctxList, Expression recipient, Boolean isField) {
if (ctxList.isEmpty()) {
return recipient;
}
@ -132,7 +136,10 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
DecafParser.RecipientContext ctx = ctxList.get(lastElement);
ctxList.remove(lastElement);
if (ctx.id() != null) {
return new FieldVarAccess(true, generateRecursiveOwnerChain(ctxList, recipient), ctx.id().IDENTIFIER().getText());
if (ctxList.isEmpty()) {
return new FieldVarAccess(isField, recipient, ctx.id().IDENTIFIER().getText());
}
return new FieldVarAccess(true, generateRecursiveOwnerChain(ctxList, recipient, isField), ctx.id().IDENTIFIER().getText());
}
if (ctx.methName() != null) {
List<Expression> args = new ArrayList<>();
@ -142,7 +149,10 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
args.add(astExpr);
}
}
return new MethodCall(new FieldVarAccess(true, generateRecursiveOwnerChain(ctxList, recipient), ctx.methName().id().IDENTIFIER().getText()), args);
if (ctxList.isEmpty()) {
return new MethodCall(new FieldVarAccess(isField, recipient, ctx.methName().id().IDENTIFIER().getText()), args);
}
return new MethodCall(new FieldVarAccess(true, generateRecursiveOwnerChain(ctxList, recipient, isField), ctx.methName().id().IDENTIFIER().getText()), args);
}
throw new RuntimeException();

View File

@ -101,9 +101,13 @@ public class StatementGenerator extends DecafBaseVisitor<List<Statement>> {
}
private FieldVarAccess generateField(DecafParser.FieldVarAccessContext fieldIdContext) {
return new FieldVarAccess(fieldIdContext.THIS() != null,
ExpressionGenerator.generateRecursiveOwnerChain(fieldIdContext.recipient(),
null), fieldIdContext.id().IDENTIFIER().getText());
Boolean isField = fieldIdContext.THIS() != null;
Expression recipient = null;
if (fieldIdContext.recipient() != null) {
recipient = ExpressionGenerator.generateRecursiveOwnerChain(fieldIdContext.recipient(), null, isField);
return new FieldVarAccess(true, recipient, fieldIdContext.id().IDENTIFIER().getText());
}
return new FieldVarAccess(isField, recipient, fieldIdContext.id().IDENTIFIER().getText());
}
@Override
@ -115,11 +119,6 @@ public class StatementGenerator extends DecafBaseVisitor<List<Statement>> {
@Override
public List<Statement> visitMethodCall(DecafParser.MethodCallContext ctx) {
boolean isField = ctx.methCall().THIS() != null;
Expression recursiveOwnerChain = null;
if (ctx.methCall().recipient() != null) {
List<DecafParser.RecipientContext> recipientList = ctx.methCall().recipient();
recursiveOwnerChain = ExpressionGenerator.generateRecursiveOwnerChain(recipientList, ctx.methCall().newCall() != null ? generateNew(ctx.methCall().newCall()) : null);
}
List<Expression> args = new ArrayList<>();
if (ctx.methCall().methName().args() != null) {
for (var expr : ctx.methCall().methName().args().expr()) {
@ -127,6 +126,12 @@ public class StatementGenerator extends DecafBaseVisitor<List<Statement>> {
args.add(astExpr);
}
}
Expression recursiveOwnerChain = null;
if (ctx.methCall().recipient() != null) {
List<DecafParser.RecipientContext> recipientList = ctx.methCall().recipient();
recursiveOwnerChain = ExpressionGenerator.generateRecursiveOwnerChain(recipientList, ctx.methCall().newCall() != null ? generateNew(ctx.methCall().newCall()) : null, isField);
return List.of(new MethodCall(new FieldVarAccess(true, recursiveOwnerChain, ctx.methCall().methName().id().IDENTIFIER().getText()), args));
}
return List.of(new MethodCall(new FieldVarAccess(isField, recursiveOwnerChain, ctx.methCall().methName().id().IDENTIFIER().getText()), args));
}