1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-21 10:37:58 +03:00
mal/logo/printer.lg
Dov Murik 4eb88ef295 Logo implementation
Tested on UCBLogo 6.0 with some minor tweaks (for performance and adding
a `timems` function).  The tweaks are performed during Docker image
creation (see Dockerfile).

Tests of step 5 are skipped because UCBLogo is too slow.

Interop is available via `(logo-eval "logo code to run")`.

The `examples` directory contains a Mal example of drawing a tree using
turtle graphics.
2016-06-17 16:36:09 -04: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