1
1
mirror of https://github.com/kanaka/mal.git synced 2024-10-26 22:28:26 +03:00
mal/impls/logo/printer.lg
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

55 lines
1.4 KiB
Plaintext

load "../logo/types.lg
to pr_str :exp :readable
if emptyp :exp [output []]
output case obj_type :exp [
[[nil] "nil]
[[true] "true]
[[false] "false]
[[number] obj_val :exp]
[[symbol] obj_val :exp]
[[keyword] word ": obj_val :exp]
[[string] print_string :exp :readable]
[[list] pr_seq obj_val :exp :readable "\( "\) :space_char]
[[vector] pr_seq obj_val :exp :readable "\[ "\] :space_char]
[[hashmap] pr_seq obj_val :exp :readable "\{ "\} :space_char]
[[atom] (word "\(atom :space_char pr_str obj_val :exp :readable "\) ) ]
[[nativefn] (word "#<NativeFunction: obj_val :exp ">) ]
[[fn] (word "#<Function:args= pr_str fn_args :exp :readable ", :space_char "macro= fn_is_macro :exp ">) ]
[else (throw "error (sentence [unknown type] obj_type :exp))]
]
end
to escape_string :s
localmake "i 1
localmake "res "
while [:i <= count :s] [
localmake "c item :i :s
make "res word :res cond [
[[ :c = "\\ ] "\\\\ ]
[[ :c = char 10 ] "\\n ]
[[ :c = "\" ] "\\\" ]
[else :c ]
]
make "i (:i + 1)
]
output :res
end
to print_string :exp :readable
ifelse :readable [
output (word "\" escape_string obj_val :exp "\" )
] [
output obj_val :exp
]
end
to pr_seq :seq :readable :start_char :end_char :delim_char
localmake "res :start_char
foreach :seq [
if # > 1 [make "res word :res :delim_char]
make "res word :res pr_str ? :readable
]
output word :res :end_char
end