1 ! Copyright (C) 2007 Daniel Ehrenberg
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: parser generic kernel classes words slots io definitions
4 sequences sequences.private assocs prettyprint.sections arrays ;
7 : define-protocol ( wordlist protocol -- )
8 swap { } like "protocol-words" set-word-prop ;
11 CREATE dup reset-generic dup define-symbol
12 parse-definition swap define-protocol ; parsing
14 PREDICATE: word protocol "protocol-words" word-prop ;
16 GENERIC: group-words ( group -- words )
18 M: protocol group-words
19 "protocol-words" word-prop ;
21 M: generic group-words
24 M: tuple-class group-words
25 "slots" word-prop 1 tail ! The first slot is the delegate
26 ! 1 tail should be removed when the delegate slot is removed
27 dup [ slot-spec-reader ] map
28 swap [ slot-spec-writer ] map append ;
30 : spin ( x y z -- z y x )
33 : define-consult-method ( word class quot -- )
34 pick add <method> spin define-method ;
36 : define-consult ( class group quot -- )
38 swapd [ define-consult-method ] 2curry each ;
41 scan-word scan-word parse-definition swapd define-consult ; parsing
43 PROTOCOL: sequence-protocol
44 clone clone-like like new new-resizable nth nth-unsafe
45 set-nth set-nth-unsafe length set-length lengthen ;
47 PROTOCOL: assoc-protocol
48 at* assoc-size >alist assoc-find set-at
49 delete-at clear-assoc new-assoc assoc-like ;
51 PROTOCOL: stream-protocol
52 stream-close stream-read1 stream-read stream-read-until
53 stream-flush stream-write1 stream-write stream-format
54 stream-nl make-span-stream make-block-stream stream-readln
55 make-cell-stream stream-write-table set-timeout ;
57 PROTOCOL: definition-protocol
58 where set-where forget uses redefined*
59 synopsis* definer definition ;
61 PROTOCOL: prettyprint-section-protocol
62 section-fits? indent-section? unindent-first-line?
63 newline-after? short-section? short-section long-section
64 <section> delegate>block add-section ;
66 : define-mimic ( group mimicker mimicked -- )
67 >r >r group-words r> r> [
68 pick "methods" word-prop at dup
69 [ method-def <method> spin define-method ] [ 3drop ] if
73 scan-word scan-word scan-word define-mimic ; parsing