Merge remote-tracking branch 'origin/main'

This commit is contained in:
simon 2024-06-29 08:57:21 +02:00
commit 3b0649f5cd

View File

@ -69,9 +69,13 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
public static Binary generateBinary(DecafParser.BinaryOperationContext ctx) {
ExpressionGenerator eGen = new ExpressionGenerator();
return new Binary(eGen.visit(ctx.expr().get(0)) // left side
Binary binary = new Binary(eGen.visit(ctx.expr().get(0)) // left side
, generateOperator(ctx.binaryOp()) //operator
, eGen.visit(ctx.expr().get(1))); //right side
binary = pointBeforeLineLogic(ctx, binary);
return binary;
}
public static Operator generateOperator(DecafParser.BinaryOpContext ctx) {
@ -143,4 +147,19 @@ public class ExpressionGenerator extends DecafBaseVisitor<Expression> {
}
throw new RuntimeException();
}
private static Binary pointBeforeLineLogic(DecafParser.BinaryOperationContext ctx, Binary binary) {
if (ctx.expr().get(0).getText().charAt(ctx.expr().get(0).getText().length()-1) == ')'){
return binary;
}
if (!(binary.op() == Operator.MUL || binary.op() == Operator.DIV || binary.op() == Operator.MOD)){
return binary;
}
if (binary.left() instanceof Binary left){
if (left.op() == Operator.ADD || left.op() == Operator.SUB ){
return new Binary(left.left(), left.op(), new Binary(binary.right(), binary.op(),left.right()));
}
}
return binary;
}
}