1
1
mirror of https://github.com/kanaka/mal.git synced 2024-08-18 02:00:40 +03:00
mal/impls/jq/printer.jq
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

29 lines
1.3 KiB
Plaintext

# {key: string, value: {kkind: kind, value: value}} -> [{kind: value.kkind, value: key}, value.value]
def _reconstruct_hash:
map([{
kind: .value.kkind,
value: .key
},
.value.value]);
def pr_str(env; opt):
(select(.kind == "symbol") | .value) //
(select(.kind == "string") | .value | if opt.readable then tojson else . end) //
(select(.kind == "keyword") | ":\(.value)") //
(select(.kind == "number") | .value | tostring) //
(select(.kind == "list") | .value | map(pr_str(env; opt)) | join(" ") | "(\(.))") //
(select(.kind == "vector") | .value | map(pr_str(env; opt)) | join(" ") | "[\(.)]") //
(select(.kind == "hashmap") | .value | to_entries | _reconstruct_hash | add // [] | map(pr_str(env; opt)) | join(" ") | "{\(.)}") //
(select(.kind == "nil") | "nil") //
(select(.kind == "true") | "true") //
(select(.kind == "false") | "false") //
(select(.kind == "fn") | "#<fn \(.function)>") //
(select(.kind == "function")| "#<function \([":anon"] + .names | join(", "))>") //
(select(.kind == "atom") | "(atom \(env.atoms[.identity] | pr_str(env; opt)))") //
"#<Unknown \(.kind) in \(.)>";
def pr_str(env):
pr_str(env; {readable: true});
def pr_str:
pr_str(null); # for stepX where X<6