11. Bigloo -- Errors and Assertions

Browsing
Home: | Bigloo A ``practical Scheme compiler'' for Bigloo version 2.5a April 2002
Previous chapter: | Lalr(1) parsing
Next chapter: | Eval and code interpretation
Chapters
    Copyright
    Acknowledgements
    1. Table of contents
    2. Overview of Bigloo
    3. Modules
    4. Core Language
    5. Standard Library
    6. Pattern Matching
    7. Object System
    8. Threads
    9. Regular parsing
    10. Lalr(1) parsing
    11. Errors and Assertions
    12. Eval and code interpretation
    13. Macro expansion
    14. Command Line Parsing
    15. Explicit typing
    16. The C interface
    17. The Java interface
    18. Bigloo Libraries
    19. SRFIs
    20. DSSSL support
    21. Compiler description
    22. User Extensions
    23. Bigloo Development Environment
    24. Global Index
    25. Library Index
    Bibliography
| Bigloo permits to signal an error via the `error` function. Errors may be trapped with the `try` form. Assertions allow the checking of predicates at certain points in programs.
error proc msg obj | bigloo procedure
This form signals an error by calling the current error handler with `proc`, `msg` and `obj` as arguments.
    (define (foo l)
       (if (not (pair? l))
           (error "foo" "argument not a pair" l)
           (car l)))

    (foo 4)
    error--> *** ERROR:bigloo:foo:
             argument not a pair -- 4
When the shell variable `BIGLOOSTACKDEPTH` is set and contains a number, an execution stack of depth `BIGLOOSTACKDEPTH` is printed when an error is raised.
error/location proc msg obj file location | bigloo procedure
This form signals an error by calling the current error handler with `proc`, `msg` and `obj` as arguments. The error is prompted in `file`, at character position `location`.
    (define (foo l)
       (if (not (pair? l))
           (error/location
             "foo" "argument not a pair" l "foo.scm" 115)
           (car l)))

    (foo 4)
    error--> File "foo.scm", line 4, character 115:
             #       (car l)))
             #       ^
             # *** ERROR:bigloo:foo
             # argument not a pair -- 4
                 0. FOO
                 1. DYNAMIC-WIND
                 2. INTERP
                 3. ENGINE
                 4. MAIN
try exp handler | bigloo syntax
The argument `exp` is evaluated. If an error is raised, the `handler` is called. The argument `handler` is a procedure of four arguments. Its first argument is the continuation of `try`. The other arguments are `proc`, `msg` and `obj`. Invoking the first argument will resume after the error.
    (let ((handler (lambda (escape proc mes obj)
                      (print "***ERROR:" proc ":" mes " -- " obj)
                      (escape #f))))
       (try (car 1) handler))
       -| ***ERROR:CAR:not a pair -- 1
       => #f
The argument `handler` is not evaluated in the dynamic scope of its `try` form. That is:
    (let ((handler (lambda (escape proc mes obj)
                      (escape (car obj)))))
       (try (car 1) handler))
       error--> *** ERROR:bigloo:CAR
                Type `PAIR' expected, `BINT' provided -- 1
Some library functions exist to help in writing handlers:
notify-error proc msg obj | bigloo procedure
Display on error port `proc`, `msg` and `obj`.
*warning* | bigloo variable
If this variable is `#t` warning messages are enabled.
warning [arg]... | bigloo procedure
This form signals a warning. That is, is `arg` are displayed on the standard error port.
    (define (foo l)
       (if (not (pair? l))
           (begin
              (warning "foo:" "argument not a pair -- " l)
              '())
           (car l)))

    (foo 4)
    -| *** WARNING:bigloo:foo:
       argument not a pair -- 4
    => '()
warning/location file location [arg]... | bigloo procedure
This form signals a warning. That is, is `arg` are displayed on the standard error port. The warning is prompted in `file` at character position `location`.
    (define (foo l)
       (if (not (pair? l))
           (begin
              (warning/location
                "foo.scm" 154 "foo:" "argument not a pair -- " l)
              '())
           (car l)))

    (foo 4)
    -| File "foo.scm", line 6, character 154:
       #       (car l)))
       #       ^
       # *** WARNING:bigloo:foo:
       argument not a pair -- 4
    => '()
assert (var...) s-expression | bigloo syntax
Assertions can be enabled or disabled using Bigloo's compilation flags `-g` flag to enable them). If the assertions are disabled they are not evaluated. If an assertion is evaluated, if the expression `exp` does not evaluate to `#t`, an error is signaled and the interpreter is launched in an environment where `var`... are bound to their current values. Assertion forms are legal expressions which always evaluate to the `unspecified` object. Here is an example of assertion usage:
    (module foo
       (eval (export foo)))

    (define (foo x y)
       [assert (x y) (< x y)]
       (labels ((gee (t)
                     [assert (t) (>= t 0)]
                     (let ((res (+ x t)))
                        [assert (res t) (> res 10)]
                        res)))
          (set! x (gee y))
          [assert (x) (> x 10)]
          x))

    (repl)
This module is compiled with the `-g` flag to enable assertions, then the produced executable is run:
    $ a.out

    1:=> (foo 1 2)

    File "foo.scm", line 9, character 158:
    #                   [assert (res t) (> res 10)]
    #                   ^
    # *** ERROR:bigloo:assert
    # assertion failed -- (BEGIN (> RES 10))
        0. GEE
        1. FOO
    -----------------------
    Variables' value are :
       RES : 3
       T : 2
    -----------------------
    *:=> ^D
    File "foo.scm", line 12, character 228:
    #      [assert (x) (> x 10)]
    #      ^
    # *** ERROR:bigloo:assert
    # assertion failed -- (BEGIN (> X 10))
        0. FOO
    -----------------------
    Variables' value are :
       X : 3
    -----------------------

    *:=> 3
    1:=> (foo 1 2)
    File "foo.scm", line 9, character 158:
    #                   [assert (res t) (> res 10)]
    #                   ^
    # *** ERROR:bigloo:assert
    # assertion failed -- (BEGIN (> RES 10))
        0. GEE
        1. FOO
    -----------------------
    Variables' value are :
       RES : 3
       T : 2
    -----------------------

    *:=>

This Scribe page has been generated by `scribeinfo`.
Last update Thu Apr 25 09:40:15 2002