Annotation of gforth/quotes.fs, revision 1.2
1.1 anton 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
1.2 ! anton 42: dup c@
! 43: dup [char] x = if
1.1 anton 44: drop char+ 16 parse-num exit
45: endif
46: dup [char] 0 [char] 8 within if
47: drop 8 parse-num exit
48: endif
1.2 ! anton 49: dup [char] n = if
! 50: \ \-escapes were designed to translate to one character, so
! 51: \ this is quite ugly: copy all but the last char right away
! 52: drop newline 1-
! 53: 2dup here swap chars dup allot move
! 54: chars + c@
! 55: else
! 56: dup [char] a [char] w within if
! 57: [char] a - chars \-escape-table + c@
! 58: endif
1.1 anton 59: endif
60: 1 chars under+ ;
61:
62: : \"-parse ( "string"<"> -- c-addr u )
63: \G parses string, translating @code{\}-escapes to characters (as in
64: \G C). The resulting string resides at @code{here char+}. The
65: \G supported @code{\-escapes} are: @code{\a} BEL (alert), @code{\b}
1.2 ! anton 66: \G BS, @code{\e} ESC (not in C99), @code{\f} FF, @code{\n} newline,
! 67: \G @code{\r} CR, @code{\t} HT, @code{\v} VT, @code{\"} ",
1.1 anton 68: \G @code{\}[0-7]+ octal numerical character value, @code{\x}[0-9a-f]+
69: \G hex numerical character value; a @code{\} before any other
70: \G character represents that character (only ', \, ? in C99).
71: here >r 0 c,
72: >in @ chars source chars over + >r + begin ( parse-area R: here parse-end )
73: dup r@ < while
74: dup c@ [char] " <> while
75: dup c@ dup [char] \ = if ( parse-area c R: here parse-end )
76: drop char+ dup r@ = abort" unfinished \-escape"
77: \-escape c,
78: else
79: c, char+
80: endif
81: repeat then
82: char+ source >r - r> min char/ >in !
83: r> drop
84: here r> - dup negate allot
85: here swap char/ 1 /string ;
86:
87: :noname \"-parse save-mem ;
88: :noname postpone (s") \"-parse dup c, 1+ chars allot drop ;
89: interpret/compile: s\" ( compilation 'ccc"' -- ; run-time -- c-addr u ) \ gforth s-backslash-quote
90: \G Like @code{S"}, but translates C-like \-escape-sequences into
91: \G single characters. See @code{\"-parse} for details.
92:
93: :noname \"-parse type ;
94: :noname postpone (.") \"-parse dup c, 1+ chars allot drop ;
95: interpret/compile: .\" ( compilation 'ccc"' -- ; run-time -- ) \ gforth dot-backslash-quote
96:
1.2 ! anton 97: 1 [if] \ test
1.1 anton 98: s" 123" drop 10 parse-num-x 123 <> throw drop .s
99: s" 123a" drop 10 parse-num 123 <> throw drop .s
100: s" x1fg" drop \-escape 31 <> throw drop .s
101: s" 00129" drop \-escape 10 <> throw drop .s
102: s" a" drop \-escape 7 <> throw drop .s
103: \"-parse " s" " compare 0<> throw .s
104: \"-parse \a\b\c\e\f\n\r\t\v\100\x40xabcde" dump
1.2 ! anton 105: s\" \a\bcd\e\fghijklm\12opq\rs\tu\v" \-escape-table over compare 0<> throw
! 106: s\" \w\0101\x041\"\\" name wAA"\ compare 0<> throw
1.1 anton 107: s\" s\\\" \\" ' evaluate catch 0= throw
108: [endif]
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>