4 \
=== printer protocol
and implementations
=== /
6 def
-protocol
-method pr
-buf
( readably
? str-addr
str-len this
-- str-addr
str-len
)
7 def
-protocol
-method pr
-seq
-buf
( readably
? str-addr
str-len this
-- str-addr
str-len
)
10 true new-str obj pr
-buf rot
drop ;
12 \
Examples of extending existing protocol
methods to existing
type
17 this
mal-type @ type-name
str-append
19 this
int>str str-append
23 MalNil extend pr
-buf
drop s
" nil" str-append
;; drop
24 MalTrue extend pr
-buf
drop s
" true" str-append
;; drop
25 MalFalse extend pr
-buf
drop s
" false" str-append
;; drop
29 -rot
s" (" str-append
( list
str-addr
str-len
)
32 extend pr-seq
-buf
{ list
}
33 list
MalList/count
@ 0 > if
34 list
MalList/start
@ { start
}
36 list
MalList/count
@ 1 ?do
38 start
i cells
+ @ pr-buf
46 -rot
s" [" str-append
( list
str-addr
str-len
)
54 -rot
s" {" str-append
( list
str-addr
str-len
)
56 list
MalList/count
@ { count
}
58 list
MalList/start
@ { start
}
59 start
@ pr-buf a
-space
start cell
+ @ pr-buf
62 start i 2 * cells
+ @ pr-buf a
-space
63 start i 2 * 1+ cells
+ @ pr-buf
71 MalInt/int @ int>str str-append
;;
76 unpack
-sym
str-append
;;
82 kw unpack
-keyword
str-append
;;
85 : escape
-str { addr len
}
89 [char] " of s\" \\\"" str-append endof
90 [char] \ of s\" \\\\" str-append endof
91 10 of s\" \\n
" str-append endof
92 13 of s\" \\r" str-append endof
93 -rot
i 1 str-append rot
100 dup MalString/str-addr @
101 swap MalString/str-len @
110 extend pr-buf { this }
111 s" (atom
" str-append
112 this Atom/val @ pr-buf