kiselgra / c-mera

Next-level syntax for C-like languages :)
Other
403 stars 17 forks source link

Try/Catch/Throw #67

Closed kiselgra closed 6 years ago

kiselgra commented 7 years ago

We are missing those :)

(function foo () -> void
  (throw (#:std:logic-error "missing")))

(function bar () -> void
  (try
    (progn ...)
    ((tag...) forms...)
    ((tag...) forms...)))

or like this?

(function bar () -> void
  (with-handlers (((tag...) ...)
                  ((tag...) ...))
    body...))

or with a different name?

(function bar () -> void
  (catching (((tag...) ...)
             ((tag...) ...))
    body...))

We could also have the first one built-in and the second one as user-space macro...

@lispbub Any preferences?

kiselgra commented 7 years ago

Currently I do this:

(comment "throw" :prefix "") (runtime-error ...)

I'm not sure about the handlers, but I think adding throw would be easy and (even alone) has the benefit of GCC's runtime catching it and printing the exception's text before aborting the program.

kiselgra commented 6 years ago

I think I would prefer the latter, i.e., catchting, oder with-exception-handler or something like that.

kiselgra commented 6 years ago

We chose the syntax

(catching (((T1 name) handler-code1)
           ((T2 name) handler-code2))
   body)

that generates

try {
    body
}
catch (T1 name) {
    handler-code1
}
catch (T2 name) {
    handler-code2
}
kiselgra commented 6 years ago

Addendum: catch-all works via (t ...)

(catching (((T1 name) handler-code1)
           ((T2 name) handler-code2))
           (t         handler-code3))
   body)

that generates

try {
    body
}
catch (T1 name) {
    handler-code1
}
catch (T2 name) {
    handler-code2
}
catch (...) {
    handler-code3
}