Skip to content

Commit

Permalink
GH-18 Fix reset of catch_frame by QUIT.
Browse files Browse the repository at this point in the history
  • Loading branch information
SirWumpus committed Aug 16, 2024
1 parent 8b90e24 commit 71ee897
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,4 @@ post4.i : config.h post4.h post4.c
${CC} -E ${CFLAGS} -DTEST -DP4_BUILT="\"${BUILT}\"" post4.c >$*.i

test : post4$E
./post4 ../test/units.p4
cd ../test && ${MAKE} $@
5 changes: 4 additions & 1 deletion src/post4.c
Original file line number Diff line number Diff line change
Expand Up @@ -1282,7 +1282,7 @@ p4StackGuards(P4_Ctx *ctx)
int
p4Repl(P4_Ctx *ctx)
{
int rc;
int rc = 0;
P4_Char *cstr;
P4_Word *word;
P4_String str;
Expand Down Expand Up @@ -1519,6 +1519,7 @@ p4Repl(P4_Ctx *ctx)
/* Historically no message, simply return to REPL. */
break;
case P4_THROW_TERMINATE:
/* Return shell equivalent exit status. */
rc = 128 + SIGTERM;
(void) printf(crlf);
goto setjmp_cleanup;
Expand Down Expand Up @@ -1547,6 +1548,8 @@ p4Repl(P4_Ctx *ctx)
ctx->here = (P4_Char *) word->data;
p4WordFree(word);
}
/* Set exit status within 1..255 */
rc = EXIT_FAILURE;
/*@fallthrough@*/
case P4_THROW_ABORT_MSG:
/* Ensure ABORT" and other messages print newline.*/
Expand Down
9 changes: 9 additions & 0 deletions src/post4.p4
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,15 @@ DEFER _fsp!
\ ( i*x -- ) ( R: j*x -- ) (F: k*x -- )
: ABORT -1 THROW ;

\ GH-18
\ QUIT resumes the REPL and never returns to the caller.
\ Need to be sure to reset the catch_frame for future
\ THROW CATCH ABORT or QUIT.
\
\ printf "' quit catch \n abort" | ./post4 ; echo $?
\
: QUIT 0 catch_frame ! QUIT ;

\ ( xt -- )
: execute-compiling
STATE @ IF EXECUTE EXIT THEN
Expand Down
4 changes: 4 additions & 0 deletions test/exceptions.p4
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ T{ 123 :NONAME 456 ABORT" TEST ERROR" ; CATCH -> 123 -2 }T
T{ 123 :NONAME 456 1138 THROW ; CATCH -> 123 1138 }T
T{ 123 :NONAME 456 HERE THROW ; CATCH -> 123 HERE }T

\ GH-18
\ https://github.com/SirWumpus/post4/issues/18#issuecomment-2293207015
T{ -1 ' THROW CATCH NIP -> -1 }T
T{ 1 2 :NONAME ( x x -- ) 2DROP ABORT ; CATCH NIP NIP -> -1 }T
test_group_end

.( -56 THROW ) test_group
Expand Down
12 changes: 10 additions & 2 deletions test/makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ CFLAGS = @CFLAGS@ -Wno-unused-label -funsigned-char -Wno-unused-const-variable
LDFLAGS = @LDFLAGS@
LIBS = @LIBS@

POST4 = ../src/post4$E
WORDS = ../src/post4.p4

BUILT := `date +'%a, %d %b %Y %H:%M:%S %z'`

#######################################################################
Expand All @@ -57,5 +60,10 @@ title :
@echo '***************************************************************'
@echo

test : ../src/post4$E
cd ../src; ${MAKE} $@
test : ${POST4}
POST4_PATH=../src ${POST4} ./units.p4
@echo Testing QUIT...
printf "' QUIT CATCH \n ABORT" | ${POST4} -c ${WORDS}; test $$? -eq 255
printf "' QUIT CATCH \n -1 THROW" | ${POST4} -c ${WORDS}; test $$? -eq 255
printf "' QUIT CATCH \n -123 THROW" | ${POST4} -c ${WORDS}; test $$? -eq 1
printf ": tw_keep_ds [: 123 QUIT ;] CATCH 456 . THROW ;\n tw_keep_ds \n ." | ${POST4} -c ${WORDS}

0 comments on commit 71ee897

Please sign in to comment.