1
1
mirror of https://github.com/kanaka/mal.git synced 2024-08-17 09:40:21 +03:00
mal/impls/dart/printer.dart
Joel Martin 8a19f60386 Move implementations into impls/ dir
- Reorder README to have implementation list after "learning tool"
  bullet.

- This also moves tests/ and libs/ into impls. It would be preferrable
  to have these directories at the top level.  However, this causes
  difficulties with the wasm implementations which need pre-open
  directories and have trouble with paths starting with "../../". So
  in lieu of that, symlink those directories to the top-level.

- Move the run_argv_test.sh script into the tests directory for
  general hygiene.
2020-02-10 23:50:16 -06:00

48 lines
1.6 KiB
Dart

import 'types.dart';
String pr_str(MalType data, {bool print_readably: true}) {
if (data is MalSymbol) {
return data.value;
} else if (data is MalInt) {
return '${data.value}';
} else if (data is MalList) {
var printedElements =
data.elements.map((e) => pr_str(e, print_readably: print_readably));
return '(${printedElements.join(" ")})';
} else if (data is MalVector) {
var printedElements =
data.elements.map((e) => pr_str(e, print_readably: print_readably));
return '[${printedElements.join(" ")}]';
} else if (data is MalHashMap) {
var printedElements = <String>[];
data.value.forEach((key, value) {
printedElements.add(pr_str(key, print_readably: print_readably));
printedElements.add(pr_str(value, print_readably: print_readably));
});
return '{${printedElements.join(" ")}}';
} else if (data is MalString) {
if (print_readably) {
var readableValue = data.value
.replaceAll('\\', r'\\')
.replaceAll('\n', r'\n')
.replaceAll('\"', r'\"');
return '"$readableValue"';
} else {
return '${data.value}';
}
} else if (data is MalKeyword) {
return ':${data.value}';
} else if (data is MalBool) {
return '${data.value}';
} else if (data is MalNil) {
return 'nil';
} else if (data is MalBuiltin) {
return '#<built in function>';
} else if (data is MalClosure) {
return '#<function>';
} else if (data is MalAtom) {
return "(atom ${pr_str(data.value, print_readably: print_readably)})";
}
throw new ArgumentError("Unrecognized type: ${data.runtimeType}");
}