From f500eff4464c593abf1d8a5901feb2f7c02b00ed Mon Sep 17 00:00:00 2001 From: Boolean-true Date: Sat, 29 Jun 2024 22:10:35 +0200 Subject: [PATCH] fix wrong field attribute --- .../java/de/maishai/ExpressionGenerator.java | 22 ++++++++++++++----- .../java/de/maishai/StatementGenerator.java | 21 +++++++++++------- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/main/java/de/maishai/ExpressionGenerator.java b/src/main/java/de/maishai/ExpressionGenerator.java index 89421d8..ae2a638 100644 --- a/src/main/java/de/maishai/ExpressionGenerator.java +++ b/src/main/java/de/maishai/ExpressionGenerator.java @@ -46,7 +46,8 @@ public class ExpressionGenerator extends DecafBaseVisitor { Expression recipient = null; if (ctx.fieldVarAccess().recipient() != null) { List 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 recursiveOwnerChain = null; if (ctx.methCall().recipient() != null) { List 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 args = new ArrayList<>(); if (ctx.methCall().methName().args() != null) { @@ -115,7 +116,10 @@ public class ExpressionGenerator extends DecafBaseVisitor { 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 { } - public static Expression generateRecursiveOwnerChain(List ctxList, Expression recipient) { + public static Expression generateRecursiveOwnerChain(List ctxList, Expression recipient, Boolean isField) { if (ctxList.isEmpty()) { return recipient; } @@ -132,7 +136,10 @@ public class ExpressionGenerator extends DecafBaseVisitor { 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 args = new ArrayList<>(); @@ -142,7 +149,10 @@ public class ExpressionGenerator extends DecafBaseVisitor { 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(); diff --git a/src/main/java/de/maishai/StatementGenerator.java b/src/main/java/de/maishai/StatementGenerator.java index 2e2ac86..206472d 100644 --- a/src/main/java/de/maishai/StatementGenerator.java +++ b/src/main/java/de/maishai/StatementGenerator.java @@ -101,9 +101,13 @@ public class StatementGenerator extends DecafBaseVisitor> { } 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> { @Override public List visitMethodCall(DecafParser.MethodCallContext ctx) { boolean isField = ctx.methCall().THIS() != null; - Expression recursiveOwnerChain = null; - if (ctx.methCall().recipient() != null) { - List recipientList = ctx.methCall().recipient(); - recursiveOwnerChain = ExpressionGenerator.generateRecursiveOwnerChain(recipientList, ctx.methCall().newCall() != null ? generateNew(ctx.methCall().newCall()) : null); - } List 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> { args.add(astExpr); } } + Expression recursiveOwnerChain = null; + if (ctx.methCall().recipient() != null) { + List 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)); }