Implement AND/OR/XOR, fix #305
All checks were successful
Build and Test with Maven / Build-and-test-with-Maven (push) Successful in 2m26s

This commit is contained in:
Daniel Holle 2024-03-26 10:54:32 +01:00
parent c84befae51
commit 606ce8b82d
6 changed files with 40 additions and 8 deletions

View File

@ -1,9 +1,28 @@
import java.lang.Boolean; import java.lang.Boolean;
import java.lang.Integer;
public class Op1 { public class Op1 {
public m() { public not() {
var b = false; var b = false;
var c = !b; var c = !b;
return c; return c;
} }
public or() {
var a = 10;
var b = 20;
return a | b;
}
public and() {
var a = 10;
var b = 20;
return a & b;
}
public xor() {
var a = 10;
var b = 20;
return a ^ b;
}
} }

View File

@ -885,15 +885,15 @@ public class StatementGenerator {
} }
private Expression convert(Java17Parser.BitwiseorexpressionContext expression) { private Expression convert(Java17Parser.BitwiseorexpressionContext expression) {
throw new NotImplementedException(); return new BinaryExpr(BinaryExpr.Operator.OR, TypePlaceholder.fresh(expression.getStart()), convert(expression.expression(0)), convert(expression.expression(1)), expression.getStart());
} }
private Expression convert(Java17Parser.BitwisexorexpressionContext expression) { private Expression convert(Java17Parser.BitwisexorexpressionContext expression) {
throw new NotImplementedException(); return new BinaryExpr(BinaryExpr.Operator.XOR, TypePlaceholder.fresh(expression.getStart()), convert(expression.expression(0)), convert(expression.expression(1)), expression.getStart());
} }
private Expression convert(Java17Parser.BitwiseandexpressionContext expression) { private Expression convert(Java17Parser.BitwiseandexpressionContext expression) {
throw new NotImplementedException(); return new BinaryExpr(BinaryExpr.Operator.AND, TypePlaceholder.fresh(expression.getStart()), convert(expression.expression(0)), convert(expression.expression(1)), expression.getStart());
} }
private Expression convert(Java17Parser.EqualityexpressionContext expression) { private Expression convert(Java17Parser.EqualityexpressionContext expression) {

View File

@ -17,6 +17,7 @@ public class BinaryExpr extends Expression {
MOD, // Modulo Operator % MOD, // Modulo Operator %
AND, // & AND, // &
OR, // | OR, // |
XOR, // ^
DIV, // / DIV, // /
LESSTHAN, // < LESSTHAN, // <
BIGGERTHAN, // > BIGGERTHAN, // >

View File

@ -103,8 +103,9 @@ public class StatementToTargetExpression implements ASTVisitor {
case SUB -> new TargetBinaryOp.Sub(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr)); case SUB -> new TargetBinaryOp.Sub(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr));
case MUL -> new TargetBinaryOp.Mul(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr)); case MUL -> new TargetBinaryOp.Mul(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr));
case MOD -> new TargetBinaryOp.Rem(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr)); case MOD -> new TargetBinaryOp.Rem(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr));
case AND -> new TargetBinaryOp.And(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr)); case AND -> new TargetBinaryOp.BAnd(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr));
case OR -> new TargetBinaryOp.Or(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr)); case OR -> new TargetBinaryOp.BOr(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr));
case XOR -> new TargetBinaryOp.XOr(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr));
case DIV -> new TargetBinaryOp.Div(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr)); case DIV -> new TargetBinaryOp.Div(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr));
case LESSTHAN -> new TargetBinaryOp.Less(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr)); case LESSTHAN -> new TargetBinaryOp.Less(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr));
case BIGGERTHAN -> new TargetBinaryOp.Greater(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr)); case BIGGERTHAN -> new TargetBinaryOp.Greater(converter.convert(binary.getType()), converter.convert(binary.lexpr), converter.convert(binary.rexpr));

View File

@ -259,7 +259,15 @@ public class TYPEStmt implements StatementVisitor {
public void visit(BinaryExpr binary) { public void visit(BinaryExpr binary) {
binary.lexpr.accept(this); binary.lexpr.accept(this);
binary.rexpr.accept(this); binary.rexpr.accept(this);
if (binary.operation.equals(BinaryExpr.Operator.DIV) || binary.operation.equals(BinaryExpr.Operator.MUL) || binary.operation.equals(BinaryExpr.Operator.MOD) || binary.operation.equals(BinaryExpr.Operator.ADD) || binary.operation.equals(BinaryExpr.Operator.SUB)) { if (binary.operation.equals(BinaryExpr.Operator.DIV)
|| binary.operation.equals(BinaryExpr.Operator.MUL)
|| binary.operation.equals(BinaryExpr.Operator.MOD)
|| binary.operation.equals(BinaryExpr.Operator.ADD)
|| binary.operation.equals(BinaryExpr.Operator.SUB)
|| binary.operation.equals(BinaryExpr.Operator.OR)
|| binary.operation.equals(BinaryExpr.Operator.AND)
|| binary.operation.equals(BinaryExpr.Operator.XOR)) {
Set<Constraint<Pair>> numericAdditionOrStringConcatenation = new HashSet<>(); Set<Constraint<Pair>> numericAdditionOrStringConcatenation = new HashSet<>();
// TODO PL 2018-11-06 // TODO PL 2018-11-06

View File

@ -842,7 +842,10 @@ public class TestComplete {
var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Op1.jav"); var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Op1.jav");
var clazz = classFiles.get("Op1"); var clazz = classFiles.get("Op1");
var instance = clazz.getDeclaredConstructor().newInstance(); var instance = clazz.getDeclaredConstructor().newInstance();
assertEquals(clazz.getDeclaredMethod("m").invoke(instance), true); assertEquals(clazz.getDeclaredMethod("not").invoke(instance), true);
assertEquals(clazz.getDeclaredMethod("or").invoke(instance), 10 | 20);
assertEquals(clazz.getDeclaredMethod("and").invoke(instance), 10 & 20);
assertEquals(clazz.getDeclaredMethod("xor").invoke(instance), 10 ^ 20);
} }
@Ignore("Not implemented") @Ignore("Not implemented")