performOperation method

dynamic performOperation(
  1. dynamic left,
  2. dynamic right,
  3. String operator,
  4. int line,
  5. String code,
)

Implementation

dynamic performOperation(
    dynamic left, dynamic right, String operator, int line, String code) {
  // Handle concatenation with strings
  if (operator == '+=' && (left is String || right is String)) {
    return left.toString() + right.toString();
  }

  // For arithmetic and bitwise operations, ensure both operands are numbers
  num leftNum, rightNum;
  try {
    leftNum = num.parse(left.toString());
    rightNum = num.parse(right.toString());
  } catch (e) {
    throw JSException(line,
        "Either '$left' or '$right' cannot be parsed ino a number and number if required for the $operator operation. Relevant Code: $code");
  }
  switch (operator) {
    case '+=':
      return leftNum + rightNum;
    case '-=':
      return leftNum - rightNum;
    case '*=':
      return leftNum * rightNum;
    case '/=':
      return leftNum / rightNum;
    case '%=':
      return leftNum % rightNum;
    case '<<=':
      // Ensure operands are integers for bitwise operations
      return leftNum.toInt() << rightNum.toInt();
    case '>>=':
      return leftNum.toInt() >> rightNum.toInt();
    case '|=':
      return leftNum.toInt() | rightNum.toInt();
    case '^=':
      return leftNum.toInt() ^ rightNum.toInt();
    case '&=':
      return leftNum.toInt() & rightNum.toInt();
    default:
      throw JSException(
          line, "$operator in Code: $code is not yet supported");
  }
}