Support escaped character in '..'

This commit is contained in:
Koichi Nakamura 2021-12-09 10:56:50 +09:00
parent f0cc87b375
commit d0bc652c5d
2 changed files with 25 additions and 18 deletions

View file

@ -1165,6 +1165,23 @@ decimal \ set default to decimal
then then
; ;
\ Return ascii-code of corresponding escaped char
\ e.g '\n' escaped-char -> 10
: escaped-char ( n -- n )
case
'0' of 0 endof
'a' of 7 endof
'b' of 8 endof
't' of 9 endof
'n' of 10 endof
'v' of 11 endof
'f' of 12 endof
'r' of 13 endof
'\\' of '\\' endof
drop -1
endcase
;
\ Parse string as number. \ Parse string as number.
\ This function interprets prefixes that specifies number base. \ This function interprets prefixes that specifies number base.
: >number ( c-addr -- n f ) : >number ( c-addr -- n f )
@ -1205,7 +1222,11 @@ decimal \ set default to decimal
dup c@ unless dup c@ unless
drop 0 false exit drop 0 false exit
then then
dup c@ swap 1+ dup c@ '\\' = if
1+ dup c@ escaped-char swap 1+
else
dup c@ swap 1+
then
c@ case c@ case
0 of true exit endof 0 of true exit endof
'\'' of true exit endof '\'' of true exit endof
@ -1315,23 +1336,6 @@ create s-buffer s-buffer-size allot
\ because we can't write string literal yet. \ because we can't write string literal yet.
char 0 char B - constant STRING-OVERFLOW-ERROR \ -18 char 0 char B - constant STRING-OVERFLOW-ERROR \ -18
\ Return ascii-code of corresponding escaped char
\ e.g '\n' escaped-char -> 10
: escaped-char ( n -- n )
case
'0' of 0 endof
'a' of 7 endof
'b' of 8 endof
't' of 9 endof
'n' of 10 endof
'v' of 11 endof
'f' of 12 endof
'r' of 13 endof
'\\' of '\\' endof
drop -1
endcase
;
\ Parse string delimited by " \ Parse string delimited by "
\ compile mode: the string is stored as operand of 'string' operator. \ compile mode: the string is stored as operand of 'string' operator.
\ immediate mode: the string is stored to temporary buffer. \ immediate mode: the string is stored to temporary buffer.

View file

@ -611,6 +611,9 @@ testing char [char] [ ] bl s"
T{ bl -> 20 }T T{ bl -> 20 }T
T{ char X -> 58 }T T{ char X -> 58 }T
T{ char HELLO -> 48 }T T{ char HELLO -> 48 }T
T{ '\n' -> 0xa }T
T{ '\0' -> 0 }T
T{ 'a' -> 61 }T
T{ : gc1 [char] X ; -> }T T{ : gc1 [char] X ; -> }T
T{ : gc2 [char] HELLO ; -> }T T{ : gc2 [char] HELLO ; -> }T
T{ gc1 -> 58 }T T{ gc1 -> 58 }T