visitGetExpr method

  1. @override
Object? visitGetExpr(
  1. Get expr
)
override

Implementation

@override
Object? visitGetExpr(Expr.Get expr) {
  Object? object = evaluate(expr.object);
  if (expr.name.lexeme == "toString") {
    return object.toString();
  }
  if (object is num) {
    switch (expr.name.lexeme) {
      case 'abs':
        return object.abs;
      case 'round':
        return object.round;
      case 'ceil':
        return object.ceil;
      case 'compareTo':
        return object.compareTo;
      case 'floor':
        return object.floor;
      case 'truncate':
        return object.truncate;
      case 'roundToDouble':
        return object.roundToDouble;
      case 'ceilToDouble':
        return object.ceilToDouble;
      case 'floorToDouble':
        return object.floorToDouble;
      case 'truncateToDouble':
        return object.truncateToDouble;
      case 'toInt':
        return object.toInt;
      case 'toDouble':
        return object.toDouble;
      case 'toStringAsFixed':
        return object.toStringAsFixed;
      case 'toStringAsExponential':
        return object.toStringAsExponential;
      case 'toStringAsPrecision':
        return object.toStringAsPrecision;
    }
  }
  if (object is DateTime) {
    switch (expr.name.lexeme) {
      case 'year':
        return object.year;
      case 'month':
        return object.month;
      case 'day':
        return object.day;
      case 'hour':
        return object.hour;
      case 'minute':
        return object.minute;
      case 'second':
        return object.second;
      case 'millisecond':
        return object.millisecond;
      case 'microsecond':
        return object.microsecond;
    }
  }
  if (object is Map) {
    switch (expr.name.lexeme) {
      case 'keys':
        return object.keys;
      case 'values':
        return object.values;
      case 'entries':
        return object.entries;
      case 'length':
        return object.length;
      case 'isEmpty':
        return object.isEmpty;
      case 'isNotEmpty':
        return object.isNotEmpty;
      case 'containsKey':
        return object.containsKey;
      case 'containsValue':
        return object.containsValue;
      case 'remove':
        return object.remove;
      case 'clear':
        return object.clear;
    }
    return object[expr.name.lexeme];
  }

  if (object is String) {
    switch (expr.name.lexeme) {
      case 'substring':
        return object.substring;
      case 'indexOf':
        return object.indexOf;
      case 'lastIndexOf':
        return object.lastIndexOf;
      case 'codeUnitAt':
        return object.codeUnitAt;
      case 'length':
        return object.length;
      case 'isEmpty':
        return object.isEmpty;
      case 'isNotEmpty':
        return object.isNotEmpty;
      case 'compareTo':
        return object.compareTo;
      case 'contains':
        return object.contains;
      case 'startsWith':
        return object.startsWith;
      case 'endsWith':
        return object.endsWith;
      case 'trim':
        return object.trim;
      case 'toLowerCase':
        return object.toLowerCase;
      case 'toUpperCase':
        return object.toUpperCase;
      case 'padLeft':
        return object.padLeft;
      case 'padRight':
        return object.padRight;
      case 'replaceAll':
        return object.replaceAll;
      case 'replaceFirst':
        return object.replaceFirst;
      case 'codeUnits':
        return object.codeUnits;
      case 'runes':
        return object.runes;
      case 'split':
        return object.split;
      case 'trimLeft':
        return object.trimLeft;
      case 'trimRight':
        return object.trimRight;
      case 'replaceAllMapped':
        return object.replaceAllMapped;
    }
    throw RuntimeError(expr.name, "Unknown property ${expr.name.lexeme}.");
  }
  if (object is List) {
    switch (expr.name.lexeme) {
      case 'first':
        return object.first;
      case 'last':
        return object.last;
      case 'single':
        return object.single;
      case 'length':
        return object.length;
      case 'isEmpty':
        return object.isEmpty;
      case 'isNotEmpty':
        return object.isNotEmpty;
      case 'add':
        return object.add;
      case 'removeLast':
        return object.removeLast;
      case 'clear':
        return object.clear;
      case 'insert':
        return object.insert;
      case 'insertAll':
        return object.insertAll;
      case 'setAll':
        return object.setAll;
      case 'removeAt':
        return object.removeAt;
      case 'remove':
        return object.remove;
      case 'indexOf':
        return object.indexOf;
      case 'lastIndexOf':
        return object.lastIndexOf;
      case 'join':
        return object.join;
      case 'any':
        return object.any;
      case 'shuffle':
        return object.shuffle;
      case 'sublist':
        return object.sublist;
      case 'getRange':
        return object.getRange;
      case 'setRange':
        return object.setRange;
      case 'fillRange':
        return object.fillRange;
    }
    throw RuntimeError(expr.name, "Unknown property ${expr.name.lexeme}.");
  } else if (object is Iterable) {
    switch (expr.name.lexeme) {
      case 'first':
        return object.first;
      case 'last':
        return object.last;
      case 'length':
        return object.length;
      case 'isEmpty':
        return object.isEmpty;
      case 'isNotEmpty':
        return object.isNotEmpty;
    }
    throw RuntimeError(expr.name, "Unknown property ${expr.name.lexeme}.");
  }
  if (object is LoxInstance) {
    return object.get(expr.name);
  }
  if (object is LoxGetCallable) {
    return object.get(expr.name);
  }
  throw RuntimeError(expr.name, "Only instances have properties.");
}