add safety net example

FossilOrigin-Name: a6a429a5be34c4e90abf2239a6e07fd89e6a9b3fe016dad766a463217776da54
This commit is contained in:
crc 2019-07-08 12:34:17 +00:00
parent d0b770e3c6
commit b9ceeecd63
2 changed files with 25 additions and 0 deletions

View file

@ -60,6 +60,7 @@ early July window for this release.
- mandelbrot.forth - mandelbrot.forth
- RFC865.forth - RFC865.forth
- RFC867.forth - RFC867.forth
- safety-net.retro
- shell.forth - shell.forth
- sqlite3 wrapper - sqlite3 wrapper
- unix-does-user-exist.forth - unix-does-user-exist.forth

24
example/safety-net.retro Normal file
View file

@ -0,0 +1,24 @@
This implements a sort of "safety net", adding some compile and runtime
error checking and reporting.
The first of these is to extend the `@` and `!` prefixes to check for
the existence of a word name. In a basic image, if the name isn't found,
it will resolve to address 0 silently. This will have it report an error
if the word is not found.
~~~
:err:var-not-defined
'\nERROR:_variable_%s_not_defined\n s:format s:put bye ;
:if:not-defined (sq-s)
over d:lookup n:zero? swap if ;
:prefix:@
[ err:var-not-defined ] if:not-defined
d:lookup d:xt fetch class:data |fetch ; immediate
:prefix:!
[ err:var-not-defined ] if:not-defined
d:lookup d:xt fetch class:data |store ; immediate
~~~