From 3f01c8d7ed24642126758a432edbdb09dbcda289 Mon Sep 17 00:00:00 2001 From: laurenz Date: Sat, 29 Jun 2024 01:17:27 +0200 Subject: [PATCH] Point before line logic in Expression Gnerator sorry --- .../java/de/maishai/ExpressionGenerator.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/maishai/ExpressionGenerator.java b/src/main/java/de/maishai/ExpressionGenerator.java index ff3a6e0..89421d8 100644 --- a/src/main/java/de/maishai/ExpressionGenerator.java +++ b/src/main/java/de/maishai/ExpressionGenerator.java @@ -69,9 +69,13 @@ public class ExpressionGenerator extends DecafBaseVisitor { 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 { } 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; + } }