evaluateElem method

EvaluateOutput evaluateElem(
  1. EvaluateInput input
)

Implementation

EvaluateOutput evaluateElem(EvaluateInput input) {
  final elem = input.expr.elem;
  final getVal =
      evaluateExpr(EvaluateInput(defStack: input.defStack, expr: elem.get));
  if (getVal.status != EvaluateOutput_Status.OK) {
    return getVal;
  }
  final pos = getVal.value;
  final fromVal =
      evaluateExpr(EvaluateInput(defStack: input.defStack, expr: elem.from));
  if (fromVal.status != EvaluateOutput_Status.OK) {
    return fromVal;
  }
  final col = fromVal.value;

  switch (col.type) {
    case Value_Type.STR:
      if (pos.type != Value_Type.NUM) {
        return _errorUnexpectedType(
            elem.get.path, pos.type, [Value_Type.NUM]);
      }
      if (!_canInt(pos)) {
        return _errorIndexNotInteger(elem.get.path, pos.num);
      }
      final idx = pos.num.toInt();
      final runes = col.str.runes.toList();
      if (idx < 0 || idx >= runes.length) {
        return _errorIndexOutOfBounds(elem.get.path, pos, 0, runes.length);
      }
      return EvaluateOutput(value: strValue(String.fromCharCode(runes[idx])));
    case Value_Type.ARR:
      if (pos.type != Value_Type.NUM) {
        return _errorUnexpectedType(
            elem.get.path, pos.type, [Value_Type.NUM]);
      }
      if (!_canInt(pos)) {
        return _errorIndexNotInteger(elem.get.path, pos.num);
      }
      final idx = pos.num.toInt();
      if (idx < 0 || idx >= col.arr.length) {
        return _errorIndexOutOfBounds(elem.get.path, pos, 0, col.arr.length);
      }
      return EvaluateOutput(value: col.arr[idx]);
    case Value_Type.OBJ:
      if (pos.type != Value_Type.STR) {
        return _errorUnexpectedType(
            elem.get.path, pos.type, [Value_Type.STR]);
      }
      final key = pos.str;
      if (!col.obj.containsKey(key)) {
        return _errorInvalidKey(elem.get.path, key, _sortedKeys(col.obj));
      }
      return EvaluateOutput(value: col.obj[key]!);
    default:
      return _errorUnexpectedType(elem.from.path, col.type,
          [Value_Type.STR, Value_Type.ARR, Value_Type.OBJ]);
  }
}