Commit | Line | Data |
---|---|---|
4eb88ef2 DM |
1 | load "../logo/types.lg |
2 | ||
3 | to pr_str :exp :readable | |
4 | if emptyp :exp [output []] | |
5 | output case obj_type :exp [ | |
6 | [[nil] "nil] | |
7 | [[true] "true] | |
8 | [[false] "false] | |
9 | [[number] obj_val :exp] | |
10 | [[symbol] obj_val :exp] | |
11 | [[keyword] word ": obj_val :exp] | |
12 | [[string] print_string :exp :readable] | |
13 | [[list] pr_seq obj_val :exp :readable "\( "\) :space_char] | |
14 | [[vector] pr_seq obj_val :exp :readable "\[ "\] :space_char] | |
15 | [[hashmap] pr_seq obj_val :exp :readable "\{ "\} :space_char] | |
16 | [[atom] (word "\(atom :space_char pr_str obj_val :exp :readable "\) ) ] | |
17 | [[nativefn] (word "#<NativeFunction: obj_val :exp ">) ] | |
18 | [[fn] (word "#<Function:args= pr_str fn_args :exp :readable ", :space_char "macro= fn_is_macro :exp ">) ] | |
19 | [else (throw "error (sentence [unknown type] obj_type :exp))] | |
20 | ] | |
21 | end | |
22 | ||
23 | to escape_string :s | |
24 | localmake "i 1 | |
25 | localmake "res " | |
26 | while [:i <= count :s] [ | |
27 | localmake "c item :i :s | |
28 | make "res word :res cond [ | |
29 | [[ :c = "\\ ] "\\\\ ] | |
30 | [[ :c = char 10 ] "\\n ] | |
31 | [[ :c = "\" ] "\\\" ] | |
32 | [else :c ] | |
33 | ] | |
34 | make "i (:i + 1) | |
35 | ] | |
36 | output :res | |
37 | end | |
38 | ||
39 | to print_string :exp :readable | |
40 | ifelse :readable [ | |
41 | output (word "\" escape_string obj_val :exp "\" ) | |
42 | ] [ | |
43 | output obj_val :exp | |
44 | ] | |
45 | end | |
46 | ||
47 | to pr_seq :seq :readable :start_char :end_char :delim_char | |
48 | localmake "res :start_char | |
49 | foreach :seq [ | |
50 | if # > 1 [make "res word :res :delim_char] | |
51 | make "res word :res pr_str ? :readable | |
52 | ] | |
53 | output word :res :end_char | |
54 | end |