File:  [gforth] / gforth / quotes.fs
Revision 1.15: download - view: text, annotated - select for diffs
Tue Feb 12 16:44:58 2008 UTC (16 years, 1 month ago) by anton
Branches: MAIN
CVS tags: HEAD
Support kernel building with 0.6.2 engine, 0.6.2 kernel, and
   current startup.fs and friends.

    1: \ quote: S\" and .\" words
    2: 
    3: \ Copyright (C) 2002,2003,2005,2007 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 3
   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, see http://www.gnu.org/licenses/.
   19: 
   20: [ifundef] umin
   21: : umin ( u1 u2 -- u )
   22:     2dup u>
   23:     if
   24: 	swap
   25:     then
   26:     drop ;
   27: [then]
   28: 
   29: : char/ ; immediate
   30: 
   31: : parse-num-x  ( c-addr1 umax -- c-addr2 c )
   32:     >r 0. rot source chars + over - char/ r> umin >number
   33:     drop rot rot drop ;
   34: 
   35: : parse-num ( c-addr1 umax base -- c-addr2 c )
   36:     ['] parse-num-x swap base-execute ;
   37: 
   38: create \-escape-table
   39:  7 c, #bs c,  'c c,   'd c, #esc c,   #ff c,   'g c,
   40: 'h c,  'i c,  'j c,   'k c,  #lf c,   #lf c,  #lf c,
   41: 'o c,  'p c,  '" c,  #cr c,   's c,  #tab c,   'u c,
   42: 11 c,  'w c,  'x c,   'y c,    0 c,
   43: 
   44: : \-escape ( c-addr1 -- c-addr2 c )
   45:     \ c-addr1 points at a char right after a '\', c-addr2 points right
   46:     \ after the whole sequence, c is the translated char
   47:     dup c@
   48:     dup 'x = if
   49: 	drop char+ 2 16 parse-num exit
   50:     endif
   51:     dup '0 '8 within if
   52: 	drop 3 8 parse-num exit
   53:     endif
   54:     dup 'n = if
   55: 	\ \-escapes were designed to translate to one character, so
   56: 	\ this is quite ugly: copy all but the last char right away
   57: 	drop newline 1-
   58: 	2dup here swap chars dup allot move
   59: 	chars + c@
   60:     else
   61:         dup 'm = if \ crlf; ugly, because it's two characters
   62:             #cr c, \ first half, the rest follows below
   63:         endif
   64: 	dup 'a '{ within if
   65: 	    'a - chars \-escape-table + c@
   66: 	endif
   67:     endif
   68:     1 chars under+ ;
   69: 
   70: : \"-parse ( "string"<"> -- c-addr u ) \ gforth-internal  backslash-quote-parse
   71: \G parses string, translating @code{\}-escapes to characters (as in
   72: \G C).  The resulting string resides at @code{here}.  See @code{S\"}
   73: \G for the supported @code{\-escapes}.
   74:     here >r
   75:     >in @ chars source chars over + >r + begin ( parse-area R: here parse-end )
   76: 	dup r@ < while
   77: 	    dup c@ '" <> while
   78: 		dup c@ dup '\ = if ( parse-area c R: here parse-end )
   79: 		    drop char+ dup r@ = abort" unfinished \-escape"
   80: 		    \-escape c,
   81: 		else
   82: 		    c, char+
   83: 		endif
   84: 	repeat then
   85:     char+ source >r - r> min char/ >in !
   86:     r> drop
   87:     here r> - dup negate allot
   88:     here swap char/ ;
   89: 
   90: :noname \"-parse save-mem ;
   91: :noname \"-parse save-mem 2dup postpone sliteral drop free throw ;
   92: interpret/compile: s\" ( compilation 'ccc"' -- ; run-time -- c-addr u )	\ gforth	s-backslash-quote
   93: \G Like @code{S"}, but translates C-like \-escape-sequences, as
   94: \G follows: @code{\a} BEL (alert), @code{\b} BS, @code{\e} ESC (not in
   95: \G C99), @code{\f} FF, @code{\n} newline, @code{\r} CR, @code{\t} HT,
   96: \G @code{\v} VT, @code{\"} ", @code{\\} \, @code{\}[0-7]@{1,3@} octal
   97: \G numerical character value (non-standard), @code{\x}[0-9a-f]@{0,2@}
   98: \G hex numerical character value (standard only with two digits); a
   99: \G @code{\} before any other character is reserved.
  100: 
  101: :noname \"-parse type ;
  102: :noname postpone s\" postpone type ;
  103: interpret/compile: .\" ( compilation 'ccc"' -- ; run-time -- )	\ gforth	dot-backslash-quote
  104: \G Like @code{."}, but translates C-like \-escape-sequences (see
  105: \G @code{S\"}).
  106: 
  107: 0 [if] \ test
  108:     s" 123" drop 10 parse-num-x 123 <> throw drop .s
  109:     s" 123a" drop 10 parse-num   123 <> throw drop .s
  110:     s" x1fg" drop \-escape 31 <> throw drop .s
  111:     s" 00129" drop \-escape 10 <> throw drop .s
  112:     s" a" drop \-escape 7 <> throw drop .s
  113:     \"-parse " s" " str= 0= throw .s
  114:     \"-parse \a\b\c\e\f\n\r\t\v\100\x40xabcde" dump
  115:     s\" \a\bcd\e\fghijklm\12op\"\rs\tu\v" \-escape-table over str= 0= throw
  116:     s\" \w\0101\x041\"\\" name wAA"\ str= 0= throw
  117:     s\" s\\\" \\" ' evaluate catch 0= throw
  118: [endif]

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>