Bugfix: charLiteral now get's the actual char, not just the quotationmark

This commit is contained in:
laurenz 2024-05-15 16:56:47 +02:00
parent fdac5aa346
commit d4cdc45b63
3 changed files with 27 additions and 8 deletions

View File

@ -18,6 +18,17 @@ import java.util.List;
*/ */
public class Compiler { 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<String> fromSources) { public static Program generateAST(List<String> fromSources) {
List<Class> classes = new ArrayList<>(); List<Class> classes = new ArrayList<>();
for (String fromSource : fromSources) { for (String fromSource : fromSources) {
@ -97,9 +108,9 @@ public class Compiler {
} }
} }
public static void main(String[] args) { // public static void main(String[] args) {
generateByteCodeFileFromFile(List.of("src/main/resources/JavaTestfiles/ClassWithConstructor.java", // generateByteCodeFileFromFile(List.of("src/main/resources/JavaTestfiles/ClassWithConstructor.java",
"src/main/resources/JavaTestfiles/ClassWithConstructorAndMethodCall.java"), // "src/main/resources/JavaTestfiles/ClassWithConstructorAndMethodCall.java"),
List.of("ClassWithConstructor","ClassWithConstructorAndMethodCall")); // List.of("ClassWithConstructor","ClassWithConstructorAndMethodCall"));
} // }
} }

View File

@ -57,8 +57,12 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
return new IntLiteral(Integer.valueOf(ctx.NUMBER().getText())); return new IntLiteral(Integer.valueOf(ctx.NUMBER().getText()));
if (ctx.BOOLEANLITERAL() != null) if (ctx.BOOLEANLITERAL() != null)
return new BoolLiteral(Boolean.valueOf(ctx.BOOLEANLITERAL().getText())); return new BoolLiteral(Boolean.valueOf(ctx.BOOLEANLITERAL().getText()));
if (ctx.CHARLITERAL() != null) if (ctx.CHARLITERAL() != null) {
return new CharLiteral(ctx.CHARLITERAL().getText().charAt(0)); 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!"); throw new RuntimeException("No literal found!");
} }
@ -93,7 +97,7 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
recursiveOwnerChain = generateRecursiveOwnerChain(recipientList, null); recursiveOwnerChain = generateRecursiveOwnerChain(recipientList, null);
} }
List<Expression> args = new ArrayList<>(); List<Expression> args = new ArrayList<>();
if(ctx.methCall().methName().args() != null){ if (ctx.methCall().methName().args() != null) {
for (var expr : ctx.methCall().methName().args().expr()) { for (var expr : ctx.methCall().methName().args().expr()) {
Expression astExpr = expr.accept(this); Expression astExpr = expr.accept(this);
args.add(astExpr); args.add(astExpr);

View File

@ -1,3 +1,7 @@
public class E2ETests { public class E2ETests {
char ZZ;
public E2ETests() {
this.ZZ = 'z';
}
} }