poke-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] pkl: Add exception_new to register user-defined exceptions


From: Jose E. Marchesi
Subject: Re: [PATCH] pkl: Add exception_new to register user-defined exceptions
Date: Mon, 25 Jan 2021 11:14:04 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

Hi Mohammad.

> Hi, Jose!
>
> This commit introduces a new function `exception_new` to give an
> `Exception` value with a unique `code` number.
> (Based on your suggestion a while ago in IRC).

I like the approach.

However, this couples the contents of exceptions with the
`exception_new' generator.

What is really generated is the exception code, so what about having an
`exception_code' generator.  User-defined exceptions would then be
defined like:

var E_my_exception = Exception { code = exception_new, ... };

WDYT?

>
>
> Regards,
> Mohammad-Reza
>
>
>  ChangeLog         |  5 +++++
>  doc/poke.texi     | 13 +++++++++++++
>  libpoke/pkl-rt.pk | 16 +++++++++++++++-
>  3 files changed, 33 insertions(+), 1 deletion(-)
>
> diff --git a/ChangeLog b/ChangeLog
> index d0602201..7e883630 100644
> --- a/ChangeLog
> +++ b/ChangeLog
> @@ -1,3 +1,8 @@
> +2021-01-22  Mohammad-Reza Nabipoor  <m.nabipoor@yahoo.com>
> +
> +     * libpoke/pkl-rt.pk (exception_new): New function.
> +     * doc/poke.texi (Exceptions): Add documentation for `exception_new`.
> +
>  2021-01-03  Jose E. Marchesi  <jemarch@gnu.org>
>  
>       * etc/poke.rec (Support nested integral structs): Do not schedule
> diff --git a/doc/poke.texi b/doc/poke.texi
> index 82ff3369..f50d0ec3 100644
> --- a/doc/poke.texi
> +++ b/doc/poke.texi
> @@ -10176,6 +10176,7 @@ type Exception =
>    @{
>      int<32> code;
>      string msg;
> +    int<32> exit_status;
>    @};
>  @end example
>  
> @@ -10190,6 +10191,18 @@ example:
>  raise Exception @{ code = 255; msg = "double upset event" @};
>  @end example
>  
> +User-defined exceptions should be resgistered using @code{exception_new}
> +function.  For example:
> +
> +@example
> +var E_my_exception =
> +  exception_new ("bad things happened", /*exit_status*/ 1);
> +@end example
> +
> +@noindent
> +where the @code{E_my_exception.code} is a unique number greater than or
> +equal to @code{255}.
> +
>  Exception codes in the range @code{0..254} are reserved for poke.
>  These are used in predefined exceptions which are standard, and have
>  specific meanings:
> diff --git a/libpoke/pkl-rt.pk b/libpoke/pkl-rt.pk
> index 9947d4d3..0b39513f 100644
> --- a/libpoke/pkl-rt.pk
> +++ b/libpoke/pkl-rt.pk
> @@ -77,7 +77,8 @@ type Exception =
>  /* Standard exception codes.
>     These codes should be in sync with PVM_E_* macros in pvm.h.
>     Note that user-defined exceptions must have codes starting with
> -   255.
> +   255 (and should be registered using exception_new function defined
> +   below).
>     Note also that EC_generic _must_ be zero.  */
>  
>  var EC_generic       = 0;
> @@ -135,6 +136,19 @@ var E_exit
>  var E_assert
>    = Exception {code = EC_assert, msg = "assertion failure", exit_status = 1};
>  
> +/* Registration of user-defined exceptions */
> +
> +type _ExceptionGenerator = (string, int<32>) Exception;
> +var exception_new = lambda _ExceptionGenerator:
> +  {
> +    var n = 255; /* First available code for user-defined exceptions */
> +
> +    return lambda (string msg, int<32> exit_status) Exception:
> +      {
> +        return Exception { code = n++, msg = msg, exit_status = exit_status 
> };
> +      };
> +  }();
> +
>  /* Default exception handler.
>  
>     Note that the code in this function should NOT raise any exception,



reply via email to

[Prev in Thread] Current Thread [Next in Thread]