[gforth] / gforth / quotes.fs  

gforth: gforth/quotes.fs


1 : anton 1.1 \ quote: S\" and .\" words
2 :    
3 :     \ Copyright (C) 2002 Free Software Foundation, Inc.
4 :    
5 :     \ This file is part of Gforth.
6 :    
7 :     \ Gforth is free software; you can redistribute it and/or
8 :     \ modify it under the terms of the GNU General Public License
9 :     \ as published by the Free Software Foundation; either version 2
10 :     \ of the License, or (at your option) any later version.
11 :    
12 :     \ This program is distributed in the hope that it will be useful,
13 :     \ but WITHOUT ANY WARRANTY; without even the implied warranty of
14 :     \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 :     \ GNU General Public License for more details.
16 :    
17 :     \ You should have received a copy of the GNU General Public License
18 :     \ along with this program; if not, write to the Free Software
19 :     \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
20 :    
21 :     : char/ ; immediate
22 :    
23 :     : parse-num-x ( c-addr1 base -- c-addr2 c )
24 :     base !
25 :     0. rot source chars + over - char/ >number
26 :     drop rot rot drop ;
27 :    
28 :     : parse-num ( c-addr1 base -- c-addr2 c )
29 :     base @ >r
30 :     ['] parse-num-x catch
31 :     r> base ! throw ;
32 :    
33 :     create \-escape-table
34 :     7 c, 8 c, char c c, char d c, 27 c, 12 c, char g c,
35 :     char h c, char i c, char j c, char k c, char l c, char m c, 10 c,
36 :     char o c, char p c, char q c, 13 c, char s c, 8 c, char u c,
37 :     11 c,
38 :    
39 :     : \-escape ( c-addr1 -- c-addr2 c )
40 :     \ c-addr1 points at a char right after a '\', c-addr2 points right
41 :     \ after the whole sequence, c is the translated char
42 :     dup c@ dup [char] x = if
43 :     drop char+ 16 parse-num exit
44 :     endif
45 :     dup [char] 0 [char] 8 within if
46 :     drop 8 parse-num exit
47 :     endif
48 :     dup [char] a [char] w within if
49 :     [char] a - chars \-escape-table + c@
50 :     endif
51 :     1 chars under+ ;
52 :    
53 :     : \"-parse ( "string"<"> -- c-addr u )
54 :     \G parses string, translating @code{\}-escapes to characters (as in
55 :     \G C). The resulting string resides at @code{here char+}. The
56 :     \G supported @code{\-escapes} are: @code{\a} BEL (alert), @code{\b}
57 :     \G BS, @code{\e} ESC (not in C99), @code{\f} FF, @code{\n} LF (or
58 :     \G newline?), @code{\r} CR, @code{\t} HT, @code{\v} VT, @code{\"} ",
59 :     \G @code{\}[0-7]+ octal numerical character value, @code{\x}[0-9a-f]+
60 :     \G hex numerical character value; a @code{\} before any other
61 :     \G character represents that character (only ', \, ? in C99).
62 :     here >r 0 c,
63 :     >in @ chars source chars over + >r + begin ( parse-area R: here parse-end )
64 :     dup r@ < while
65 :     dup c@ [char] " <> while
66 :     dup c@ dup [char] \ = if ( parse-area c R: here parse-end )
67 :     drop char+ dup r@ = abort" unfinished \-escape"
68 :     \-escape c,
69 :     else
70 :     c, char+
71 :     endif
72 :     repeat then
73 :     char+ source >r - r> min char/ >in !
74 :     r> drop
75 :     here r> - dup negate allot
76 :     here swap char/ 1 /string ;
77 :    
78 :     :noname \"-parse save-mem ;
79 :     :noname postpone (s") \"-parse dup c, 1+ chars allot drop ;
80 :     interpret/compile: s\" ( compilation 'ccc"' -- ; run-time -- c-addr u ) \ gforth s-backslash-quote
81 :     \G Like @code{S"}, but translates C-like \-escape-sequences into
82 :     \G single characters. See @code{\"-parse} for details.
83 :    
84 :     :noname \"-parse type ;
85 :     :noname postpone (.") \"-parse dup c, 1+ chars allot drop ;
86 :     interpret/compile: .\" ( compilation 'ccc"' -- ; run-time -- ) \ gforth dot-backslash-quote
87 :    
88 :     0 [if] \ test
89 :     s" 123" drop 10 parse-num-x 123 <> throw drop .s
90 :     s" 123a" drop 10 parse-num 123 <> throw drop .s
91 :     s" x1fg" drop \-escape 31 <> throw drop .s
92 :     s" 00129" drop \-escape 10 <> throw drop .s
93 :     s" a" drop \-escape 7 <> throw drop .s
94 :     \"-parse " s" " compare 0<> throw .s
95 :     \"-parse \a\b\c\e\f\n\r\t\v\100\x40xabcde" dump
96 :     s\" \a\bcd\e\fghijklm\nopq\rs\tu\v" \-escape-table over compare 0<> throw
97 :     s\" \0101\x041\"\\" name AA"\ compare 0<> throw
98 :     s\" s\\\" \\" ' evaluate catch 0= throw
99 :     [endif]

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help