From d0bc652c5df4f6524669596645e5a2cc6a61a117 Mon Sep 17 00:00:00 2001 From: Koichi Nakamura Date: Thu, 9 Dec 2021 10:56:50 +0900 Subject: [PATCH] Support escaped character in '..' --- bootstrap.fs | 40 ++++++++++++++++++++++------------------ test/core.fs | 3 +++ 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/bootstrap.fs b/bootstrap.fs index 3404a30..3fa2d9e 100644 --- a/bootstrap.fs +++ b/bootstrap.fs @@ -1165,6 +1165,23 @@ decimal \ set default to decimal 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. \ This function interprets prefixes that specifies number base. : >number ( c-addr -- n f ) @@ -1205,7 +1222,11 @@ decimal \ set default to decimal dup c@ unless drop 0 false exit then - dup c@ swap 1+ + dup c@ '\\' = if + 1+ dup c@ escaped-char swap 1+ + else + dup c@ swap 1+ + then c@ case 0 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. 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 " \ compile mode: the string is stored as operand of 'string' operator. \ immediate mode: the string is stored to temporary buffer. diff --git a/test/core.fs b/test/core.fs index 8c442bc..8d77ca5 100644 --- a/test/core.fs +++ b/test/core.fs @@ -611,6 +611,9 @@ testing char [char] [ ] bl s" T{ bl -> 20 }T T{ char X -> 58 }T T{ char HELLO -> 48 }T +T{ '\n' -> 0xa }T +T{ '\0' -> 0 }T +T{ 'a' -> 61 }T T{ : gc1 [char] X ; -> }T T{ : gc2 [char] HELLO ; -> }T T{ gc1 -> 58 }T