bbc-basic: Wrap BASIC and OS errors in a hash-map when they're caught.
authorBen Harris <bjh21@bjh21.me.uk>
Sat, 18 May 2019 05:18:02 +0000 (06:18 +0100)
committerBen Harris <bjh21@bjh21.me.uk>
Sat, 18 May 2019 15:34:01 +0000 (16:34 +0100)
This allows mal code to get at the error number (ERR), which is what it
should look at to determine the meaning of the error.

bbc-basic/step9_try.bbc
bbc-basic/stepA_mal.bbc

index eed78bf..00adf67 100644 (file)
@@ -119,8 +119,17 @@ DEF FNtry(ast%, env%, RETURN is_error%)
 
 REM  Return a mal value corresponding to the most-recently thrown exception.
 DEF FNwrap_exception
+  REM  There are three cases to handle.  When the error was generated
+  REM  by 'throw', we should return the value that 'throw' stashed in
+  REM  MAL_ERR%.  When the error was generated by mal, we should just
+  REM  return the error message.  When the error was generated by BASIC
+  REM  or the OS, we should wrap the message and the error number in
+  REM  a hash-map.
   IF ERR = &40E80900 THEN =MAL_ERR% : REM  Error generated by 'throw'
-=FNalloc_string(REPORT$)            : REM  OS error or error generated by mal
+  IF (ERR AND &FFFFFF00) = &40E80900 THEN =FNalloc_string(REPORT$)
+  LOCAL e%
+  e% = FNhashmap_set(FNempty_hashmap, "err", FNalloc_int(ERR))
+=FNhashmap_set(e%, "message", FNalloc_string(REPORT$))
 
 DEF FNcatch(ast%, env%, err%)
   LOCAL binds%, exprs%
index 2352c54..e56bcdd 100644 (file)
@@ -123,8 +123,17 @@ DEF FNtry(ast%, env%, RETURN is_error%)
 
 REM  Return a mal value corresponding to the most-recently thrown exception.
 DEF FNwrap_exception
+  REM  There are three cases to handle.  When the error was generated
+  REM  by 'throw', we should return the value that 'throw' stashed in
+  REM  MAL_ERR%.  When the error was generated by mal, we should just
+  REM  return the error message.  When the error was generated by BASIC
+  REM  or the OS, we should wrap the message and the error number in
+  REM  a hash-map.
   IF ERR = &40E80900 THEN =MAL_ERR% : REM  Error generated by 'throw'
-=FNalloc_string(REPORT$)            : REM  OS error or error generated by mal
+  IF (ERR AND &FFFFFF00) = &40E80900 THEN =FNalloc_string(REPORT$)
+  LOCAL e%
+  e% = FNhashmap_set(FNempty_hashmap, "err", FNalloc_int(ERR))
+=FNhashmap_set(e%, "message", FNalloc_string(REPORT$))
 
 DEF FNcatch(ast%, env%, err%)
   LOCAL binds%, exprs%