add decoding for { … } literals in SEE

This commit is contained in:
Jesse D. McDonald 2020-10-15 15:03:02 -05:00
parent 29b949d583
commit 2eaa43f85f
2 changed files with 53 additions and 19 deletions

View File

@ -1340,10 +1340,11 @@ defword BAILOUT
litstring "Fatal error\n" litstring "Fatal error\n"
.int TYPE .int TYPE
0: .int LIT,254,SYS_EXIT,SYSCALL1,DROP,BRANCH,(0b - .) 0: .int LIT,254,SYS_EXIT,SYSCALL1,DROP,BRANCH,(0b - .)
.int EXIT /* just to mark the end */
defword UNEXPECTED_EOF,"UNEXPECTED-EOF" defword UNEXPECTED_EOF,"UNEXPECTED-EOF"
litstring "Unexpected end of input\n" litstring "Unexpected end of input\n"
.int TYPE,BAILOUT .int TYPE,BAILOUT,EXIT
/* During bootstrapping the source buffer is the embedded file "startup.4th". */ /* During bootstrapping the source buffer is the embedded file "startup.4th". */
/* ( -- c-addr u ) */ /* ( -- c-addr u ) */
@ -1361,7 +1362,7 @@ defword PARSE_AREA,"PARSE-AREA"
/* ( "c" -- c ) Leaves c at the start of the parse area */ /* ( "c" -- c ) Leaves c at the start of the parse area */
defword PEEK_CHAR,"PEEK-CHAR" defword PEEK_CHAR,"PEEK-CHAR"
.int PARSE_AREA,ZBRANCH,(0f - .),FETCHBYTE,EXIT .int PARSE_AREA,ZBRANCH,(0f - .),FETCHBYTE,EXIT
0: .int UNEXPECTED_EOF 0: .int UNEXPECTED_EOF,EXIT
/* ( "c" -- c ) Removes and returns the first character in the parse area */ /* ( "c" -- c ) Removes and returns the first character in the parse area */
defword NEXT_CHAR,"NEXT-CHAR" defword NEXT_CHAR,"NEXT-CHAR"
@ -1480,7 +1481,7 @@ defword FIND
defword FIND_OR_ABORT,"FIND-OR-ABORT" defword FIND_OR_ABORT,"FIND-OR-ABORT"
.int FIND,QDUP,ZBRANCH,(0f - .),EXIT .int FIND,QDUP,ZBRANCH,(0f - .),EXIT
0: litstring "Word not found: " 0: litstring "Word not found: "
.int TYPE,TYPE,EOL,BAILOUT .int TYPE,TYPE,EOL,BAILOUT,EXIT
/* ( "<spaces>" -- ) */ /* ( "<spaces>" -- ) */
defword SKIPSPACE defword SKIPSPACE
@ -1516,7 +1517,7 @@ defword ESCAPED_CHAR
0: .int LIT,39,OVER,EQU,ZBRANCH,(0f - .),DROP,LIT,39,EXIT /* single-quote */ 0: .int LIT,39,OVER,EQU,ZBRANCH,(0f - .),DROP,LIT,39,EXIT /* single-quote */
0: .int LIT,92,OVER,EQU,ZBRANCH,(0f - .),DROP,LIT,92,EXIT /* backslash */ 0: .int LIT,92,OVER,EQU,ZBRANCH,(0f - .),DROP,LIT,92,EXIT /* backslash */
0: litstring "Unknown escape sequence: \\" 0: litstring "Unknown escape sequence: \\"
.int TYPE,EMIT,EOL,BAILOUT .int TYPE,EMIT,EOL,BAILOUT,EXIT
defword READSTRING defword READSTRING
.int HERE .int HERE
@ -1570,6 +1571,7 @@ defword INTERPRET
defword QUIT defword QUIT
.int R0,RSPSTORE .int R0,RSPSTORE
0: .int INTERPRET,BRANCH,(0b - .) 0: .int INTERPRET,BRANCH,(0b - .)
.int EXIT
defword LATEST defword LATEST
.int CURRENT,FETCH,FETCH,EXIT .int CURRENT,FETCH,FETCH,EXIT

View File

@ -37,7 +37,7 @@ DEFER THROW-UNWIND ( k*x n -- i*x <noreturn> )
\ If there is no CATCH, perform the function of ABORT (clear data stack and QUIT) \ If there is no CATCH, perform the function of ABORT (clear data stack and QUIT)
\ Absent CATCH, whether a message is displayed depends on the value of n: \ Absent CATCH, whether a message is displayed depends on the value of n:
\ -1 (ABORT) no message \ -1 (ABORT) no message
\ -2 (?FAIL) the string passed to ?FAIL \ -2 (FAIL) the string passed to FAIL
\ otherwise message is implementation-dependent \ otherwise message is implementation-dependent
: THROW ( k*x n -- k*x | i*x n <noreturn> ) : THROW ( k*x n -- k*x | i*x n <noreturn> )
?DUP IF THROW-UNWIND THEN ; ?DUP IF THROW-UNWIND THEN ;
@ -46,14 +46,19 @@ DEFER THROW-UNWIND ( k*x n -- i*x <noreturn> )
\ This behavior can be overridden with CATCH \ This behavior can be overridden with CATCH
: ABORT ( i*x -- ) ( R: j*x -- ) -1 THROW ; : ABORT ( i*x -- ) ( R: j*x -- ) -1 THROW ;
\ THROWN-STRING holds the address and size of the string passed to ?FAIL \ THROWN-STRING holds the address and size of the string passed to FAIL
\ It may also be used to hold context strings for other system exception codes \ It may also be used to hold context strings for other system exception codes
CREATE THROWN-STRING 0 , 0 , CREATE THROWN-STRING 0 , 0 ,
\ Display a message and ABORT
\ This behavior can be overridden with CATCH
: FAIL ( c-addr u -- <none> | <noreturn> )
THROWN-STRING 2! -2 THROW ;
\ If flag is non-zero, display a message and ABORT \ If flag is non-zero, display a message and ABORT
\ This behavior can be overridden with CATCH \ This behavior can be overridden with CATCH
: ?FAIL ( flag c-addr u -- <none> | <noreturn> ) : ?FAIL ( flag c-addr u -- <none> | <noreturn> )
ROT IF THROWN-STRING 2! -2 THROW ELSE 2DROP THEN ; ROT IF FAIL ELSE 2DROP THEN ;
\ Names for the standard file descriptor numbers \ Names for the standard file descriptor numbers
0 CONSTANT STDIN 0 CONSTANT STDIN
@ -482,7 +487,7 @@ CREATE CURRENT-SOURCE-ID -1 ,
\ Create a deferred word; the target is stored in the DFA field \ Create a deferred word; the target is stored in the DFA field
\ The default target throws an exception — replace it using DEFER! or IS \ The default target throws an exception — replace it using DEFER! or IS
: (DEFERRED-UNINIT) TRUE "Uninitialized deferred word" ?FAIL ; : (DEFERRED-UNINIT) "Uninitialized deferred word" FAIL ;
: DEFER ( "<spaces>ccc" -- ) : DEFER ( "<spaces>ccc" -- )
CREATE ['] (DEFERRED-UNINIT) LATEST DEFER! ; CREATE ['] (DEFERRED-UNINIT) LATEST DEFER! ;
' (DEFERRED-UNINIT) (HIDE) ' (DEFERRED-UNINIT) (HIDE)
@ -828,7 +833,7 @@ HIDE TERMINAL-BUFFER
[CHAR] " OF [CHAR] " ENDOF [CHAR] " OF [CHAR] " ENDOF
[CHAR] ' OF [CHAR] ' ENDOF [CHAR] ' OF [CHAR] ' ENDOF
[CHAR] \ OF [CHAR] \ ENDOF [CHAR] \ OF [CHAR] \ ENDOF
TRUE "Unknown escape sequence" ?FAIL "Unknown escape sequence" FAIL
ENDCASE ENDCASE
THEN ; THEN ;
@ -941,7 +946,7 @@ HIDE TERMINAL-BUFFER
NR> RESTORE-INPUT DROP NR> RESTORE-INPUT DROP
R> SWAP >R SP! R> ; R> SWAP >R SP! R> ;
\ Run xt while trapping calls to THROW, ABORT, ?FAIL, etc. \ Run xt while trapping calls to THROW, ABORT, FAIL, etc.
\ On success has the effect of xt and also leaves the value 0 on top of the stack \ On success has the effect of xt and also leaves the value 0 on top of the stack
\ On failure the stacks and input source are reverted and the THROW code is pushed \ On failure the stacks and input source are reverted and the THROW code is pushed
: CATCH ( i*x xt -- j*x 0 | i*x n ) : CATCH ( i*x xt -- j*x 0 | i*x n )
@ -1050,6 +1055,7 @@ DEFINITIONS
\ Read one cell and increment \ Read one cell and increment
: @(+) ( a-addr1 -- a-addr2 x ) DUP CELL+ SWAP @ ; : @(+) ( a-addr1 -- a-addr2 x ) DUP CELL+ SWAP @ ;
\ Display a string in escaped (double-quoted) format, without the delimiters
: TYPE-ESCAPED ( c-addr u -- "<escapeseq*>" ) : TYPE-ESCAPED ( c-addr u -- "<escapeseq*>" )
0 DO DUP 1+ SWAP C@ CASE 0 DO DUP 1+ SWAP C@ CASE
0 OF "\\0" TYPE ENDOF 0 OF "\\0" TYPE ENDOF
@ -1071,7 +1077,25 @@ DEFINITIONS
THEN THEN
ENDCASE LOOP DROP ; ENDCASE LOOP DROP ;
: UNTHREAD ( a-addr -- ) \ Recognize the pattern BRANCH a:{c-a} b:{word} {code…} c:LIT d:{b}
\ This pattern is generated by the { … } inline :NONAME syntax
: NONAME-LITERAL? ( a-addr -- flag )
@(+) ['] BRANCH = AND-THEN
@(+) DUP 0> AND-THEN
( S: addr-b offset-c-a )
OVER CELL- + @(+) ['] LIT = AND-THEN
( S: addr-b addr-d )
@ OVER = AND-THEN
DUP WORD?
THEN
ELSE NIP THEN
ELSE NIP THEN
THEN NIP ;
\ Display the threaded code which starts at a-addr
\ Continues until it encounters a reference to EXIT beyond any forward branches
\ Numeric, string, and { … } literals are decoded, plus offsets for branches
: UNTHREAD ( a-addr -- ) RECURSIVE
DUP >R DUP >R
BEGIN BEGIN
@(+) @(+)
@ -1086,20 +1110,27 @@ DEFINITIONS
"\"" TYPE TYPE-ESCAPED "\"" TYPE SPACE "\"" TYPE TYPE-ESCAPED "\"" TYPE SPACE
+ ALIGNED + ALIGNED
ELSE ELSE
DUP ['] BRANCH = OR-ELSE DUP ['] 0BRANCH = THEN IF OVER CELL- NONAME-LITERAL? IF
>NAME TYPE SPACE DROP DUP @ OVER + 2 CELLS+ DUP R> MAX >R SWAP
@(+) DUP "{" TYPE . "}" TYPE SPACE "{ " TYPE CELL+ >DFA @ UNTHREAD "} " TYPE
OVER + R> MAX >R
ELSE ELSE
DUP WORD? AND-THEN DUP IMMEDIATE? THEN IF DUP ['] BRANCH = OR-ELSE DUP ['] 0BRANCH = THEN IF
"POSTPONE " TYPE >NAME TYPE SPACE
@(+) DUP "{" TYPE DUP 0> IF "+" TYPE THEN . "}" TYPE SPACE
OVER CELL- + R> MAX >R
ELSE
DUP WORD? AND-THEN DUP IMMEDIATE? THEN IF
"POSTPONE " TYPE
THEN
.W SPACE
THEN THEN
.W SPACE
THEN THEN
THEN THEN
THEN THEN
AGAIN ; AGAIN ;
HIDE NONAME-LITERAL?
: SEE ( "<spaces>name" -- ) : SEE ( "<spaces>name" -- )
' DUP >CFA @ CASE ' DUP >CFA @ CASE
DOCOL OF DOCOL OF
@ -1108,7 +1139,8 @@ DEFINITIONS
" " TYPE >DFA @ UNTHREAD ";\n" TYPE " " TYPE >DFA @ UNTHREAD ";\n" TYPE
ENDOF ENDOF
DODEFER OF DODEFER OF
"DEFER " TYPE DUP >NAME TYPE " ' " TYPE DUP >DFA @ .W " IS " >NAME TYPE EOL "DEFER " TYPE DUP >NAME TYPE " ' " TYPE DUP >DFA @ .W
" IS " TYPE >NAME TYPE EOL
ENDOF ENDOF
DODATA OF DODATA OF
DUP EXECUTE . " CONSTANT " TYPE >NAME TYPE EOL DUP EXECUTE . " CONSTANT " TYPE >NAME TYPE EOL