repr method
Implementation
String repr(Object? object) {
if (object == null) return "None";
if (object is bool) return object ? "True" : "False";
if (object is String) {
// Add quotes and escape internal quotes/backslashes
String escaped = object
.replaceAll('\\', '\\\\')
.replaceAll("'", "\\'")
.replaceAll('\n', '\\n')
.replaceAll('\r', '\\r')
.replaceAll('\t', '\\t');
// Prefer single quotes unless string contains single quotes but not double
if (escaped.contains("'") && !escaped.contains('"')) {
return '"$escaped"';
}
return "'$escaped'";
}
if (object is PyList) return '[${object.list.map(repr).join(', ')}]';
if (object is PyTuple) {
String content = object.tuple.map(repr).join(', ');
if (object.tuple.length == 1) content += ',';
return '($content)';
}
if (object is Set) {
if (object.isEmpty) return "set()";
return '{${object.map(repr).join(', ')}}';
}
if (object is Map) {
return '{${object.entries.map((e) => '${repr(e.key)}: ${repr(e.value)}').join(', ')}}';
}
if (object is PyInstance &&
_isinstanceBuiltin(this, [object, globals.values["Exception"]], {})
as bool) {
return "${object.fields["message"]}";
}
if (object is PyCallable) {
return object.toString(); // <fn ...>, <native fn>, <method...>
}
if (object is PyClass) return "<class '${object.name}'>";
if (object is PyInstance) {
// TODO: call __repr__ when implemented
return object.toString(); // <MyClass object>
}
// Fallback für numbers and basic types
return object.toString();
}