From d0611666840ca2c56bb477f4bd0e777e567ed8d0 Mon Sep 17 00:00:00 2001 From: Koichi Nakamura Date: Thu, 9 Dec 2021 08:47:46 +0900 Subject: [PATCH] Support escaped character in s" --- bootstrap.fs | 36 +++++++++++++++++++++++++++++++++++- test/fileio.fs | 3 +-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/bootstrap.fs b/bootstrap.fs index 6537a27..b98f331 100644 --- a/bootstrap.fs +++ b/bootstrap.fs @@ -576,7 +576,16 @@ allot-cell : &find! [ ' L , , ] ; \ ( c-addr -- nt ) Throw exception at error : 3 [ key 3 key 0 - ] literal ; : 4 [ key 4 key 0 - ] literal ; : 5 [ key 5 key 0 - ] literal ; +: 6 [ key 6 key 0 - ] literal ; +: 7 [ key 7 key 0 - ] literal ; +: 8 [ key 8 key 0 - ] literal ; +: 9 [ key 9 key 0 - ] literal ; : 10 [ key : key 0 - ] literal ; +: 11 [ key ; key 0 - ] literal ; +: 12 [ key < key 0 - ] literal ; +: 13 [ key = key 0 - ] literal ; +: 14 [ key > key 0 - ] literal ; +: 15 [ key ? key 0 - ] literal ; : 16 [ key @ key 0 - ] literal ; : -1 [ key 0 key 1 - ] literal ; @@ -1045,6 +1054,14 @@ decimal \ set default to decimal : '%' [char] % ; : '$' [char] $ ; : '\'' [char] ' ; +: '\\' [char] \ ; +: 'a' [char] a ; +: 'b' [char] b ; +: 't' [char] t ; +: 'n' [char] n ; +: 'v' [char] v ; +: 'f' [char] f ; +: 'r' [char] r ; \ Display unsigned integer u2 with number base u1. : print-uint ( u1 u2 -- ) @@ -1298,6 +1315,22 @@ 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 + 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. @@ -1307,6 +1340,7 @@ char 0 char B - constant STRING-OVERFLOW-ERROR \ -18 here 0 , \ save location of length and fill dummy 0 \ length of the string + 1 (\0) begin key! dup '"' <> while + dup '\\' = if drop key! escaped-char then c, \ store character 1+ \ increment length repeat drop @@ -1317,7 +1351,7 @@ char 0 char B - constant STRING-OVERFLOW-ERROR \ -18 else s-buffer dup \ save start address begin key! dup '"' <> while - ( buf pos c pos-buf ) + dup '\\' = if drop key! escaped-char then over 3 pick - s-buffer-size 1- >= if STRING-OVERFLOW-ERROR throw then diff --git a/test/fileio.fs b/test/fileio.fs index d6af3b1..a6f76a5 100644 --- a/test/fileio.fs +++ b/test/fileio.fs @@ -9,7 +9,6 @@ T{ }T T{ 32 allocate throw constant BUF -> }T T{ BUF 32 FILE0 read-file throw -> 27 }T -T{ s" ABCDEFGHIJKLMNOPQRSTUVWXYZ -" BUF 27 strneq -> true }T +T{ s" ABCDEFGHIJKLMNOPQRSTUVWXYZ\n" BUF 27 strneq -> true }T T{ FILE0 close-file throw -> }T T{ BUF free -> }T