PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/fth/smart_if.fth

https://github.com/cataska/pforth
Forth | 57 lines | 52 code | 5 blank | 0 comment | 4 complexity | 34b79e2d8b503229440f12b1bcca3c8f MD5 | raw file
  1. \ @(#) smart_if.fth 98/01/26 1.2
  2. \ Smart Conditionals
  3. \ Allow use of if, do, begin, etc.outside of colon definitions.
  4. \
  5. \ Thanks to Mitch Bradley for the idea.
  6. \
  7. \ Author: Phil Burk
  8. \ Copyright 1994 3DO, Phil Burk, Larry Polansky, Devid Rosenboom
  9. \
  10. \ The pForth software code is dedicated to the public domain,
  11. \ and any third party may reproduce, distribute and modify
  12. \ the pForth software code or any derivative works thereof
  13. \ without any compensation or license. The pForth software
  14. \ code is provided on an "as is" basis without any warranty
  15. \ of any kind, including, without limitation, the implied
  16. \ warranties of merchantability and fitness for a particular
  17. \ purpose and their equivalents under the laws of any jurisdiction.
  18. anew task-smart_if.fth
  19. variable SMIF-XT \ execution token for conditional code
  20. variable SMIF-DEPTH \ depth of nested conditionals
  21. : SMIF{ ( -- , if executing, start compiling, setup depth )
  22. state @ 0=
  23. IF
  24. :noname smif-xt !
  25. 1 smif-depth !
  26. ELSE
  27. 1 smif-depth +!
  28. THEN
  29. ;
  30. : }SMIF ( -- , unnest, stop compiling, execute code and forget )
  31. smif-xt @
  32. IF
  33. -1 smif-depth +!
  34. smif-depth @ 0 <=
  35. IF
  36. postpone ; \ stop compiling
  37. smif-xt @ execute \ execute conditional code
  38. smif-xt @ >code dp ! \ forget conditional code
  39. 0 smif-xt ! \ clear so we don't mess up later
  40. THEN
  41. THEN
  42. ;
  43. \ redefine conditionals to use smart mode
  44. : IF smif{ postpone if ; immediate
  45. : DO smif{ postpone do ; immediate
  46. : ?DO smif{ postpone ?do ; immediate
  47. : BEGIN smif{ postpone begin ; immediate
  48. : THEN postpone then }smif ; immediate
  49. : REPEAT postpone repeat }smif ; immediate
  50. : UNTIL postpone until }smif ; immediate
  51. : LOOP postpone loop }smif ; immediate
  52. : +LOOP postpone +loop }smif ; immediate