From d4cdc45b631899e32accbfb9cc230274d65451cd Mon Sep 17 00:00:00 2001 From: laurenz Date: Wed, 15 May 2024 16:56:47 +0200 Subject: [PATCH] Bugfix: charLiteral now get's the actual char, not just the quotationmark --- src/main/java/de/maishai/Compiler.java | 21 ++++++++++++++----- .../java/de/maishai/ExpressionGenerator.java | 10 ++++++--- src/test/java/E2ETests.java | 4 ++++ 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/main/java/de/maishai/Compiler.java b/src/main/java/de/maishai/Compiler.java index 1f1ebeb..c245997 100644 --- a/src/main/java/de/maishai/Compiler.java +++ b/src/main/java/de/maishai/Compiler.java @@ -18,6 +18,17 @@ import java.util.List; */ public class Compiler { + public static void main(String[] args) { + generateAST(List.of(""" + public class E2ETests { + char ZZ; + public E2ETests() { + this.ZZ = 'z'; + } + } + """)); + } + public static Program generateAST(List fromSources) { List classes = new ArrayList<>(); for (String fromSource : fromSources) { @@ -97,9 +108,9 @@ public class Compiler { } } - public static void main(String[] args) { - generateByteCodeFileFromFile(List.of("src/main/resources/JavaTestfiles/ClassWithConstructor.java", - "src/main/resources/JavaTestfiles/ClassWithConstructorAndMethodCall.java"), - List.of("ClassWithConstructor","ClassWithConstructorAndMethodCall")); - } +// public static void main(String[] args) { +// generateByteCodeFileFromFile(List.of("src/main/resources/JavaTestfiles/ClassWithConstructor.java", +// "src/main/resources/JavaTestfiles/ClassWithConstructorAndMethodCall.java"), +// List.of("ClassWithConstructor","ClassWithConstructorAndMethodCall")); +// } } diff --git a/src/main/java/de/maishai/ExpressionGenerator.java b/src/main/java/de/maishai/ExpressionGenerator.java index 3701aec..4f4dcfc 100644 --- a/src/main/java/de/maishai/ExpressionGenerator.java +++ b/src/main/java/de/maishai/ExpressionGenerator.java @@ -57,8 +57,12 @@ public class ExpressionGenerator extends DecafBaseVisitor { return new IntLiteral(Integer.valueOf(ctx.NUMBER().getText())); if (ctx.BOOLEANLITERAL() != null) return new BoolLiteral(Boolean.valueOf(ctx.BOOLEANLITERAL().getText())); - if (ctx.CHARLITERAL() != null) - return new CharLiteral(ctx.CHARLITERAL().getText().charAt(0)); + if (ctx.CHARLITERAL() != null) { + if (ctx.CHARLITERAL().getText().length() != 3) { + throw new RuntimeException("Wrong format for Char literal. Good format: 'c' Bad format: " + ctx.CHARLITERAL().getText()); + } + return new CharLiteral(ctx.CHARLITERAL().getText().charAt(1)); + } throw new RuntimeException("No literal found!"); } @@ -93,7 +97,7 @@ public class ExpressionGenerator extends DecafBaseVisitor { recursiveOwnerChain = generateRecursiveOwnerChain(recipientList, null); } List args = new ArrayList<>(); - if(ctx.methCall().methName().args() != null){ + if (ctx.methCall().methName().args() != null) { for (var expr : ctx.methCall().methName().args().expr()) { Expression astExpr = expr.accept(this); args.add(astExpr); diff --git a/src/test/java/E2ETests.java b/src/test/java/E2ETests.java index 57f34f2..8ae34ec 100644 --- a/src/test/java/E2ETests.java +++ b/src/test/java/E2ETests.java @@ -1,3 +1,7 @@ public class E2ETests { + char ZZ; + public E2ETests() { + this.ZZ = 'z'; + } }