Commit dd9a1546 authored by Nguyen, Thien Minh's avatar Nguyen, Thien Minh
Browse files

Handle nested multiplicative expression



Check if LHS also has a binary op (* or /) then we need to re-visit the whole node as a multiplicative expression itself.

Also, fix a minus handling for integer type.

Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent dd0dd8f3
Loading
Loading
Loading
Loading
+21 −5
Original line number Diff line number Diff line
@@ -644,10 +644,22 @@ antlrcpp::Any qasm3_expression_generator::visitMultiplicativeExpression(

  if (auto mult_expr = ctx->multiplicativeExpression()) {
    auto bin_op = ctx->binary_op->getText();

    mlir::Value lhs = [&](){
      // Handle nested multiplicative expressions:
      if (mult_expr->binary_op) {
        // The nested multiplicativeExpression within this MultiplicativeExpressionContext
        // also has a binary op:
        // Need to visit this whole multiplicativeExpression:
        visitMultiplicativeExpression(mult_expr);
        return current_value;
      } else {
        // Just a terminator value:
        visitExpressionTerminator(mult_expr->expressionTerminator());
    auto lhs = current_value;
        return current_value;
      }
    }(); 
    
    // Get the RHS
    visitExpressionTerminator(ctx->expressionTerminator());
    auto rhs = current_value;

@@ -751,7 +763,11 @@ antlrcpp::Any qasm3_expression_generator::visitExpressionTerminator(
        attr = mlir::IntegerAttr::get(builder.getI64Type(), -1);
      }
      auto const_op = builder.create<mlir::ConstantOp>(location, attr);
      if (current_value.getType().isa<mlir::FloatType>()) {
        createOp<mlir::MulFOp>(location, const_op, current_value);
      } else {
        createOp<mlir::MulIOp>(location, const_op, current_value);
      }
    }
    return 0;
  }