]> gitweb.factorcode.org Git - factor.git/blob - basis/tr/tr.factor
basis: use lint.vocabs tool to trim using lists
[factor.git] / basis / tr / tr.factor
1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: ascii assocs byte-arrays fry hints kernel lexer math
4 parser sequences sequences.private strings summary words ;
5 IN: tr
6
7 ERROR: bad-tr ;
8
9 M: bad-tr summary
10     drop "TR: can only be used with ASCII characters" ;
11
12 <PRIVATE
13
14 : tr-nth ( n mapping -- ch ) nth-unsafe 127 bitand ; inline
15
16 : check-tr ( from to -- )
17     [ [ ascii? ] all? ] both? [ bad-tr ] unless ;
18
19 : compute-tr ( quot from to -- mapping )
20     [ 128 <iota> ] 3dip zip
21     '[ [ _ call( x -- y ) _ at ] keep or ] B{ } map-as ; inline
22
23 : tr-hints ( word -- )
24     { { byte-array } { string } } set-specializer ;
25
26 : create-tr ( token -- word )
27     create-word-in dup tr-hints ;
28
29 : tr-quot ( mapping -- quot )
30     '[ [ dup ascii? [ _ tr-nth ] when ] map ] ;
31
32 : define-tr ( word mapping -- )
33     tr-quot ( seq -- translated ) define-declared ;
34
35 : fast-tr-quot ( mapping -- quot )
36     '[ [ _ tr-nth ] map! drop ] ;
37
38 : define-fast-tr ( word mapping -- )
39     fast-tr-quot ( seq -- ) define-declared ;
40
41 PRIVATE>
42
43 SYNTAX: TR:
44     scan-token parse-definition
45     unclip-last [ unclip-last ] dip compute-tr
46     [ check-tr ]
47     [ [ create-tr ] dip define-tr ]
48     [ [ "-fast" append create-tr ] dip define-fast-tr ] 2tri ;