Add Negation operator (fixes #304)
All checks were successful
Build and Test with Maven / Build-and-test-with-Maven (push) Successful in 2m41s

This commit is contained in:
Daniel Holle 2024-03-25 17:05:20 +01:00
parent 7f3c1686ec
commit c84befae51
4 changed files with 21 additions and 9 deletions

View File

@ -1,11 +1,9 @@
public class Op1{
public Op1() {
Runnable lam = () -> {
String test = "";
String b = "b";
test = b;
System.out.println(test);};
//lam.run();
import java.lang.Boolean;
public class Op1 {
public m() {
var b = false;
var c = !b;
return c;
}
}

View File

@ -996,6 +996,9 @@ public class StatementGenerator {
ret = new UnaryExpr(UnaryExpr.Operation.PREDECREMENT, expr, TypePlaceholder.fresh(op), op);
ret.setStatement();
return ret;
} else if (op.getText().equals("!")) {
ret = new UnaryExpr(UnaryExpr.Operation.NOT, expr, TypePlaceholder.fresh(op), op);
return ret;
} else {
throw new NotImplementedException();
}

View File

@ -245,6 +245,9 @@ public class TYPEStmt implements StatementVisitor {
constraintsSet.addUndConstraint(new Pair(unaryExpr.expr.getType(), number, PairOperator.SMALLERNEQDOT, loc(unaryExpr.getOffset())));
// The type of the postfix increment expression is the type of the variable
constraintsSet.addUndConstraint(new Pair(unaryExpr.expr.getType(), unaryExpr.getType(), PairOperator.EQUALSDOT, loc(unaryExpr.getOffset())));
} else if (unaryExpr.operation == UnaryExpr.Operation.NOT) {
constraintsSet.addUndConstraint(new Pair(unaryExpr.expr.getType(), unaryExpr.getType(), PairOperator.EQUALSDOT, loc(unaryExpr.getOffset())));
constraintsSet.addUndConstraint(new Pair(unaryExpr.expr.getType(), new RefType(ASTFactory.createClass(java.lang.Boolean.class).getClassName(), new NullToken()), PairOperator.EQUALSDOT, loc(unaryExpr.getOffset())));
} else {
throw new NotImplementedException();
}

View File

@ -837,6 +837,14 @@ public class TestComplete {
assertEquals(clazz.getSuperclass().getDeclaredField("x").get(instance), 3);
}
@Test
public void testOperators() throws Exception {
var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Op1.jav");
var clazz = classFiles.get("Op1");
var instance = clazz.getDeclaredConstructor().newInstance();
assertEquals(clazz.getDeclaredMethod("m").invoke(instance), true);
}
@Ignore("Not implemented")
@Test
public void testStringConcat() throws Exception {