getValue method

  1. @override
num getValue(
  1. DartBlockArbiter arbiter
)
override

Implementation

@override
num getValue(DartBlockArbiter arbiter) {
  if (leftChild != null && operator != null && rightChild != null) {
    switch (operator!) {
      case DartBlockAlgebraicOperator.add:
        return leftChild!.getValue(arbiter) + rightChild!.getValue(arbiter);
      case DartBlockAlgebraicOperator.subtract:
        return leftChild!.getValue(arbiter) - rightChild!.getValue(arbiter);
      case DartBlockAlgebraicOperator.multiply:
        return leftChild!.getValue(arbiter) * rightChild!.getValue(arbiter);
      case DartBlockAlgebraicOperator.divide:
        final rightValue = rightChild!.getValue(arbiter);
        if (rightValue == 0) {
          throw MalformedAlgebraicExpressionException(
            null,
            rightValue,
            operator,
            "Division by zero is not allowed. ('${toString()}')",
          );
        }
        final leftValue = leftChild!.getValue(arbiter);
        // CRITICAL: mimic truncating integer division of Java.
        // Note that 9 / 2 results in 4.5 in Dart, but 4 in Java!
        if (leftValue is int && rightValue is int) {
          return leftValue ~/ rightValue;
        } else {
          return leftValue / rightValue;
        }
      case DartBlockAlgebraicOperator.modulo:
        final leftValue = leftChild!.getValue(arbiter);
        final rightValue = rightChild!.getValue(arbiter);
        if (leftValue < 0 || rightValue < 0) {
          throw MalformedAlgebraicExpressionException(
            leftValue,
            rightValue,
            operator,
            "The modulo '%' operator can only be applied to positive operands. ('${toString()}')",
          );
        } else if (rightValue == 0) {
          throw MalformedAlgebraicExpressionException(
            leftValue,
            rightValue,
            operator,
            "The right operand of the modulo '%' operator must be greater than zero. ('${toString()}')",
          );
        }
        return leftValue % rightValue;
    }
  } else if (leftChild != null && operator == null && rightChild != null) {
    throw MalformedAlgebraicExpressionException(
      null,
      null,
      operator,
      "Missing operator between the two operands. ('${toString()}')",
    );
  } else if (leftChild != null && operator != null && rightChild == null) {
    return leftChild!.getValue(arbiter);
  } else if (leftChild == null && operator != null && rightChild != null) {
    switch (operator!) {
      case DartBlockAlgebraicOperator.add:
        return rightChild!.getValue(arbiter);
      case DartBlockAlgebraicOperator.subtract:
        return -rightChild!.getValue(arbiter);
      case DartBlockAlgebraicOperator.multiply:
        throw MalformedAlgebraicExpressionException(
          null,
          null,
          operator,
          "Algebraic expressions cannot start with the multiplication '*' operator. ('${toString()}')",
        );
      case DartBlockAlgebraicOperator.divide:
        throw MalformedAlgebraicExpressionException(
          null,
          null,
          operator,
          "Algebraic expressions cannot start with the division '/' operator. ('${toString()}')",
        );
      case DartBlockAlgebraicOperator.modulo:
        throw MalformedAlgebraicExpressionException(
          null,
          null,
          operator,
          "Algebraic expressions cannot start with the modulo '%' operator. ('${toString()}')",
        );
    }
  } else if (leftChild == null && operator == null && rightChild != null) {
    return rightChild!.getValue(arbiter);
  } else if (leftChild != null && operator == null && rightChild == null) {
    return leftChild!.getValue(arbiter);
  } else if (leftChild == null && operator != null && rightChild == null) {
    throw MalformedAlgebraicExpressionException(
      null,
      null,
      operator,
      "Missing operand(s). ('${toString()}')",
    );
  }
  // all null
  else {
    throw MalformedAlgebraicExpressionException(
      null,
      null,
      null,
      "Missing operand(s) and operator. ('${toString()}')",
    );
  }
}