Compare commits

...
11 Commits
Author SHA1 Message Date
dholle 4b576da19d Fix #414 by implementing the other number formats
Build and Test with Maven / Build-and-test-with-Maven (push) Successful in 1m26s
SonarQube Scan / SonarQube Trigger (pull_request) Successful in 2m35s
2026-11-11 14:38:19 +01:00
dholle de96cda327 Fix #417 in Codegen and by changing constraint generation
Build and Test with Maven / Build-and-test-with-Maven (push) Successful in 1m23s
SonarQube Scan / SonarQube Trigger (pull_request) Successful in 2m36s
2026-11-11 14:00:22 +01:00
dholle 2710685db3 Merge pull request 'Fix #409 by moving increment to the beginning of the loop' (#427) from fix-409 into master
SonarQube Scan / SonarQube Trigger (push) Successful in 2m29s
Reviewed-on: #427
2026-11-11 11:25:41 +00:00
dholle e951a60ae6 Fix incorrect behavior
Build and Test with Maven / Build-and-test-with-Maven (push) Successful in 1m18s
SonarQube Scan / SonarQube Trigger (pull_request) Successful in 2m37s
2026-11-11 12:18:42 +01:00
dholle 825df05fac Fix #409 by moving increment to the beginning of the loop
Build and Test with Maven / Build-and-test-with-Maven (push) Failing after 1m17s
SonarQube Scan / SonarQube Trigger (pull_request) Failing after 1m33s
2026-11-11 12:00:59 +01:00
dholle fab3e73596 Merge branch 'fix-423'
SonarQube Scan / SonarQube Trigger (push) Canceled after 26m58s
2026-11-11 11:34:32 +01:00
dholle 392c5fa619 Merge branch 'fix-422'
SonarQube Scan / SonarQube Trigger (push) Failing after 1m39s
2026-11-11 11:31:48 +01:00
dholle e21d2d7ced Merge pull request 'Fix #421 by adding error listeners to the Parser & Lexer' (#426) from fix-421 into master
SonarQube Scan / SonarQube Trigger (push) Failing after 1m39s
Reviewed-on: #426
2026-11-11 10:28:50 +00:00
dholle 5a12545e8f Fix #421 by adding error listeners to the Parser & Lexer
Build and Test with Maven / Build-and-test-with-Maven (push) Failing after 7m30s
SonarQube Scan / SonarQube Trigger (pull_request) Failing after 1m35s
2026-11-10 15:31:41 +01:00
dholle d6c59f07f6 Add check for duplicate class definition in source files, fixes #422
Build and Test with Maven / Build-and-test-with-Maven (push) Failing after 1m52s
SonarQube Scan / SonarQube Trigger (pull_request) Failing after 2m24s
2026-11-10 15:04:47 +01:00
dholle eb1c5c6629 Add check for missing interface methods, fixes #423
Build and Test with Maven / Build-and-test-with-Maven (push) Failing after 1m22s
SonarQube Scan / SonarQube Trigger (pull_request) Failing after 2m11s
2026-11-10 14:46:41 +01:00
16 changed files with 190 additions and 19 deletions

No files matched your search

+13
View File
@@ -0,0 +1,13 @@
import java.lang.Integer;
public class Bug409 {
public static m() {
var n = 0;
for (var i = 0; i < 5; i++) {
n = n + 1;
if (n > 100) { return -1; }
if (i == 2) { continue; }
}
return n;
}
}
+11
View File
@@ -0,0 +1,11 @@
import java.lang.Integer;
import java.lang.String;
public class Bug417 {
public name(Integer i) {
return switch (i) {
case 1 -> "one";
default -> "many";
};
}
}
+3
View File
@@ -0,0 +1,3 @@
public class Bug421 {
public m() { return 1 } // Missing semicolon
}
+2
View File
@@ -0,0 +1,2 @@
public class Bug422 {}
public class Bug422 {}
+5
View File
@@ -0,0 +1,5 @@
import java.lang.Integer;
interface Bug423I { Integer f(); }
public class Bug423 implements Bug423I { }
@@ -15,6 +15,7 @@ interface C {
}
class ClassX implements A {
void method1() {}
}
record ClassY(Integer myInt) implements C {}
+4
View File
@@ -5,4 +5,8 @@ public class Literal {
public m2() { return 'C'; }
public m3() { return 10L; }
public m4() { return 10.5F; }
public m5() { return 10.5D; }
public m6() { return 0xFF; }
public m7() { return 0b101010; }
public m8() { return 010; }
}
@@ -1056,6 +1056,7 @@ public class Codegen {
}
Label start = new Label();
Label skip = new Label();
Label end = new Label();
mv.visitLabel(start);
if (_for.termination() != null) {
@@ -1068,13 +1069,14 @@ public class Codegen {
}
var env = new BreakEnv();
env.startLabel = start;
env.startLabel = skip;
env.endLabel = end;
state.breakStack.push(env);
generate(state, _for.body());
state.breakStack.pop();
mv.visitLabel(skip);
if (_for.increment() != null) {
_for.increment().forEach(e -> {
generate(state, e);
@@ -1435,13 +1437,16 @@ public class Codegen {
if (label instanceof TargetTypePattern || label instanceof TargetComplexPattern) {
if (label.type() instanceof TargetGenericType) types.add(Type.getType(Object.class));
else types.add(Type.getObjectType(label.type().getInternalName()));
} else if (label instanceof TargetLiteral lit) {
types.add(lit.value());
} else if (label instanceof TargetExpressionPattern expr) {
if (expr.expression() instanceof TargetLiteral literal) {
types.add(Type.getObjectType(TargetType.toWrapper(literal.type()).getInternalName()));
} else {
throw new NotImplementedException();
}
} else if (label instanceof TargetGuard guard) {
types.add(Type.getObjectType(guard.inner().type().getInternalName()));
// TODO Same here we need to evaluate constant;
} else {
logger.info(label);
throw new NotImplementedException();
}
}
@@ -1846,6 +1851,35 @@ public class Codegen {
clazz.constructors().forEach(this::generateConstructor);
clazz.methods().forEach(this::generateMethod);
// Check if non abstract class implements all interface methods
if ((access & ACC_ABSTRACT) == 0) {
for (var intf : clazz.implementingInterfaces()) {
var intfClass = compiler.getClass(new JavaClassName(intf.name()));
for (var method : intfClass.getMethods()) {
if ((method.modifier & ACC_ABSTRACT) != 0) {
// Check if the method is implemented in the current class
// TODO This is duplicate code, we should probably refactor instances of this pattern
var foundMethods = clazz.methods().stream().filter(m -> {
if (!m.name().equals(method.name)) return false;
if (m.signature().parameters().size() != method.getParameterList().getFormalparalist().size())
return false;
for (var i = 0; i < m.signature().parameters().size(); i++) {
var param1 = m.signature().parameters().get(i).pattern().type();
var param2 = converter.convert(
method.getParameterList().getParameterAt(i).getType(),
ASTToTargetAST.Generics.nullGenerics(compiler).javaGenerics());
if (!param1.equals(param2)) return false;
}
return true;
}).toList();
if (foundMethods.size() != 1) {
throw new CodeGenException("Class " + clazz.qualifiedName() + " does not implement method " + method.name + " from interface " + intfClass.getClassName());
}
}
}
}
}
if (clazz.staticConstructor() != null)
generateStaticConstructor(clazz.staticConstructor());
@@ -5,6 +5,7 @@ import de.dhbwstuttgart.bytecode.Codegen;
import de.dhbwstuttgart.bytecode.FunNGenerator;
import de.dhbwstuttgart.environment.CompilationEnvironment;
import de.dhbwstuttgart.environment.DirectoryClassLoader;
import de.dhbwstuttgart.exceptions.CompilerException;
import de.dhbwstuttgart.exceptions.CompilerWarning;
import de.dhbwstuttgart.exceptions.DebugException;
import de.dhbwstuttgart.exceptions.WarningsException;
@@ -171,6 +172,8 @@ public class JavaTXCompiler {
var numberOfGenerics = decl == null ? 0 : decl.genericTypeVar().size();
var className = pkgName + (!pkgName.isEmpty() ? "." : "") + name;
if (classRegistry.contains(className))
throw new CompilerException("Duplicate class definition: " + className);
classRegistry.addName(className, numberOfGenerics);
input.add(classRegistry.getName(className));
}
@@ -0,0 +1,7 @@
package de.dhbwstuttgart.exceptions;
public class CompilerException extends RuntimeException {
public CompilerException(String message) {
super(message);
}
}
@@ -2,9 +2,8 @@ package de.dhbwstuttgart.exceptions;
import org.antlr.v4.runtime.Token;
public class ParserError extends TypeinferenceException{
public ParserError(Token offset){
super("Fehler beim Parsen", offset);
public class ParserError extends RuntimeException{
public ParserError(String message) {
super(message);
}
}
@@ -1,6 +1,7 @@
package de.dhbwstuttgart.parser;
import de.dhbwstuttgart.environment.CompilationEnvironment;
import de.dhbwstuttgart.exceptions.ParserError;
import de.dhbwstuttgart.parser.SyntaxTreeGenerator.SyntaxTreeGenerator;
import de.dhbwstuttgart.parser.antlr.Java17Lexer;
import de.dhbwstuttgart.parser.antlr.Java17Parser;
@@ -8,9 +9,7 @@ import de.dhbwstuttgart.parser.scope.JavaClassRegistry;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.util.Logger;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
@@ -21,13 +20,25 @@ public class JavaTXParser {
public static Logger logger = new Logger("Parser");
public static class ThrowingErrorListener extends BaseErrorListener {
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol,
int line, int charPositionInLine, String msg, RecognitionException e) {
throw new ParserError("Syntax error at line " + line + ":" + charPositionInLine + " - " + msg);
}
}
public static Java17Parser.SourceFileContext parse(File source) throws IOException, java.lang.ClassNotFoundException {
InputStream stream = new FileInputStream(source);
// DEPRECATED: ANTLRInputStream input = new ANTLRInputStream(stream);
var errorListener = new ThrowingErrorListener();
CharStream input = CharStreams.fromStream(stream);
Java17Lexer lexer = new Java17Lexer(input);
lexer.addErrorListener(errorListener);
CommonTokenStream tokens = new CommonTokenStream(lexer);
Java17Parser parser = new Java17Parser(tokens);
parser.addErrorListener(errorListener);
return parser.sourceFile();
/*
* SyntaxTreeGenerator generator = new SyntaxTreeGenerator(environment.getRegistry(source)); return generator.convert(tree);
@@ -1033,10 +1033,22 @@ public class StatementGenerator {
private Expression convert(Java17Parser.LiteralContext literal) {
switch (literal) {
case IntLiteralContext intliteral:
var text = intliteral.getText();
var radix = 10;
if (text.startsWith("0x") || text.startsWith("0X")) {
radix = 16;
text = text.substring(2);
} else if (text.startsWith("0b") || text.startsWith("0B")) {
radix = 2;
text = text.substring(2);
} else if (text.startsWith("0") && text.length() > 1) {
radix = 8;
text = text.substring(1);
}
Number value;
if (intliteral.getText().endsWith("l") || intliteral.getText().endsWith("L"))
value = Long.parseLong(intliteral.getText().substring(0, intliteral.getText().length() - 1));
else value = Integer.parseInt(intliteral.getText());
if (text.endsWith("l") || text.endsWith("L"))
value = Long.parseLong(text.substring(0, text.length() - 1), radix);
else value = Integer.parseInt(text, radix);
return new Literal(TypePlaceholder.fresh(literal.getStart()), value, intliteral.getStart());
case FltLiteralContext floatliteral:
if (floatliteral.getText().endsWith("f") || floatliteral.getText().endsWith("F"))
@@ -875,8 +875,8 @@ public class TYPEStmt implements StatementVisitor {
if (caseExpressionType == null) {
for (var child : switchStmt.getBlocks()) {
for (var label : child.getLabels()) {
if (label.getPattern() == null) {
constraintsSet.addUndConstraint(new Pair(new RefType(new JavaClassName("java.lang.Object"), new NullToken()), switchStmt.getSwitch().getType(), PairOperator.SMALLERDOT, loc(label.getOffset())));
if (label.getPattern() == null && switchStmt.getSwitch().getType() instanceof TypePlaceholder) {
constraintsSet.addUndConstraint(new Pair(new RefType(new JavaClassName("java.lang.Object"), new NullToken()), switchStmt.getSwitch().getType(), PairOperator.EQUALSDOT, loc(label.getOffset())));
}
if (label.getPattern() instanceof RecordPattern) {
RecordPattern recordPattern = (RecordPattern) label.getPattern();
+56 -1
View File
@@ -1,6 +1,8 @@
import de.dhbwstuttgart.bytecode.CodeGenException;
import de.dhbwstuttgart.core.ConsoleInterface;
import de.dhbwstuttgart.exceptions.CompilerException;
import de.dhbwstuttgart.exceptions.ParserError;
import de.dhbwstuttgart.exceptions.WarningsException;
import de.dhbwstuttgart.util.Logger;
import de.dhbwstuttgart.util.Logger.LogLevel;
@@ -1081,9 +1083,9 @@ public class TestComplete {
assertEquals(40.0, m.invoke(instance, d, 0));
}
@Disabled("Waiting for #407, skipping for now")
@Test
public void testPatternMatchingInterface() throws Exception {
ConsoleInterface.logLevel = LogLevel.INFO;
var classFiles = generateClassFiles(createClassLoader(), false, "Matcher.jav", "PatternMatchingInterface.jav");
var Intf = classFiles.get("Interface");
var clazz = classFiles.get("PatternMatchingInterface");
@@ -1228,6 +1230,10 @@ public class TestComplete {
assertEquals('C', clazz.getDeclaredMethod("m2").invoke(instance));
assertEquals(10L, clazz.getDeclaredMethod("m3").invoke(instance));
assertEquals(10.5F, clazz.getDeclaredMethod("m4").invoke(instance));
assertEquals(10.5D, clazz.getDeclaredMethod("m5").invoke(instance));
assertEquals(0xFF, clazz.getDeclaredMethod("m6").invoke(instance));
assertEquals(0b101010, clazz.getDeclaredMethod("m7").invoke(instance));
assertEquals(010, clazz.getDeclaredMethod("m8").invoke(instance));
}
@Test
@@ -1704,4 +1710,53 @@ public class TestComplete {
var clazz = classFiles.get("Bug406");
clazz.getDeclaredMethod("main", List.class).invoke(null, List.of());
}
@Test
public void testBug423() throws Exception {
try {
var classFiles = generateClassFiles(createClassLoader(), "Bug423.jav");
fail("No exception thrown!");
} catch (CodeGenException e) {
assertEquals("Class Bug423 does not implement method f from interface Bug423I", e.getMessage());
}
}
@Test
public void testBug422() throws Exception {
try {
var classFiles = generateClassFiles(createClassLoader(), "Bug422.jav");
fail("Shouldn't compile!");
} catch (CompilerException e) {
assertEquals("Duplicate class definition: Bug422", e.getMessage());
}
}
@Test
public void testBug421() throws Exception {
try {
var classFiles = generateClassFiles(createClassLoader(), "Bug421.jav");
fail("Shouldn't compile!");
} catch (ParserError e) {
assertEquals("Syntax error at line 2:26 - no viable alternative at input 'm() { return 1 }'", e.getMessage());
}
}
@Test
public void testBug409() throws Exception {
var classFiles = generateClassFiles(createClassLoader(), "Bug409.jav");
var clazz = classFiles.get("Bug409");
var instance = clazz.getDeclaredConstructor().newInstance();
var m = clazz.getDeclaredMethod("m");
assertEquals(5, m.invoke(instance));
}
@Test
public void testBug417() throws Exception {
var classFiles = generateClassFiles(createClassLoader(), "Bug417.jav");
var clazz = classFiles.get("Bug417");
var instance = clazz.getDeclaredConstructor().newInstance();
var name = clazz.getDeclaredMethod("name", Integer.class);
assertEquals("one", name.invoke(instance, 1));
assertEquals("many", name.invoke(instance, 2));
}
}
+13 -2
View File
@@ -202,9 +202,20 @@ public class TestCodegen {
@Test
public void testFor() throws Exception {
var targetClass = new TargetClass(Opcodes.ACC_PUBLIC, new JavaClassName("For"));
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "forLoop", List.of(), TargetType.Integer, new TargetBlock(List.of(new TargetVarDecl(TargetType.Integer, "sum", new TargetLiteral.IntLiteral(0)), new TargetFor(List.of(new TargetVarDecl(TargetType.Integer, "i", new TargetLiteral.IntLiteral(0))), new TargetBinaryOp.Less(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "i"), new TargetLiteral.IntLiteral(10)), List.of(new TargetAssign(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "i"), new TargetBinaryOp.Add(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "i"), new TargetLiteral.IntLiteral(1)))), new TargetBlock(List.of(new TargetAssign(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "sum"), new TargetBinaryOp.Add(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "sum"), new TargetLocalVar(TargetType.Integer, "i")))))), new TargetReturn(new TargetLocalVar(TargetType.Integer, "sum")))));
targetClass.addMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "forLoop", List.of(), TargetType.Integer,
new TargetBlock(List.of(new TargetVarDecl(TargetType.Integer, "sum", new TargetLiteral.IntLiteral(0)),
new TargetFor(List.of(new TargetVarDecl(TargetType.Integer, "i", new TargetLiteral.IntLiteral(0))),
new TargetBinaryOp.Less(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "i"),
new TargetLiteral.IntLiteral(10)), List.of(new TargetAssign(TargetType.Integer,
new TargetLocalVar(TargetType.Integer, "i"),
new TargetBinaryOp.Add(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "i"), new TargetLiteral.IntLiteral(1)))),
new TargetBlock(List.of(
new TargetAssign(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "sum"),
new TargetBinaryOp.Add(TargetType.Integer, new TargetLocalVar(TargetType.Integer, "sum"),
new TargetLocalVar(TargetType.Integer, "i")))))),
new TargetReturn(new TargetLocalVar(TargetType.Integer, "sum")))));
var clazz = generateClass(targetClass, createClassLoader());
assertEquals(clazz.getDeclaredMethod("forLoop").invoke(null), 45);
assertEquals(45, clazz.getDeclaredMethod("forLoop").invoke(null));
}
@Test