Advertisement
Guest User

Exception handling in DX-Forth

a guest
May 17th, 2025
64
0
154 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | Source Code | 0 0
  1. \ return to OS with exit code
  2. : RETURN ( code -- ) ... ;
  3.  
  4. \ perform ANS QUIT
  5. : (quit) ( -- ) r0 @ rp! reset normal /interpret
  6. begin cr (refill) drop interpret state? 0= if (vstat)
  7. @execute then again ;
  8.  
  9. \ exit to OS or back to forth
  10. : ?return ( code -- ) turnkey? if return then drop (quit) ;
  11.  
  12. \ clear data stacks
  13. : (abort) ( i*x -- ) s0 @ sp! fs0 @ fsp ! 1 ?return ;
  14.  
  15. : error ( n -- ) \ part of THROW
  16. -1 of (abort) then
  17. -2 of boot cell+ @ 0= if .error then
  18. space errmsg 2@ type (abort) then
  19. -56 of 1 ?return then
  20. ." THROW #" @base decimal swap . !base (abort) ;
  21.  
  22. : QUIT ( -- ) 0 ?return ; \ QUIT not trapped
  23.  
  24. : QUIT -56 throw ; \ QUIT trapped
  25.  
  26. : ?ABORT ( flag c-addr u -- )
  27. rot if errmsg 2! -2 throw then 2drop ;
  28.  
  29. : (abort") ( flag -- ) r> count 2dup + >r ?abort ;
  30.  
  31. : ABORT" state @ if postpone (abort") ," end
  32. postpone s" ?abort ; immediate
  33.  
  34. : ABORT -1 throw ;
  35.  
  36. : THROW ( n -- )
  37. ?dup if catcher @ ?dup 0= if error then rp!
  38. r> catcher ! 2r> fsp ! swap >r sp! drop r> then ;
  39.  
  40. : CATCH ( xt -- n | 0 )
  41. sp@ fsp @ 2>r catcher @ >r rp@ catcher !
  42. execute 0 r> catcher ! 2r> 2drop ;
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement