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 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) {
List<Class> 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"));
// }
}

View File

@ -57,8 +57,12 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
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<Expression> {
recursiveOwnerChain = generateRecursiveOwnerChain(recipientList, null);
}
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()) {
Expression astExpr = expr.accept(this);
args.add(astExpr);

View File

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