From b9ceeecd637b8c23df6e36f736d18b20696d8d04 Mon Sep 17 00:00:00 2001 From: crc Date: Mon, 8 Jul 2019 12:34:17 +0000 Subject: [PATCH] add safety net example FossilOrigin-Name: a6a429a5be34c4e90abf2239a6e07fd89e6a9b3fe016dad766a463217776da54 --- RELEASE-NOTES | 1 + example/safety-net.retro | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 example/safety-net.retro diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 9afc9d8..c338b81 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -60,6 +60,7 @@ early July window for this release. - mandelbrot.forth - RFC865.forth - RFC867.forth + - safety-net.retro - shell.forth - sqlite3 wrapper - unix-does-user-exist.forth diff --git a/example/safety-net.retro b/example/safety-net.retro new file mode 100644 index 0000000..00663a9 --- /dev/null +++ b/example/safety-net.retro @@ -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 +~~~