• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
  • entries
    625
  • comments
    1446
  • views
    1006491

Error handling in Epoch

Sign in to follow this  
Followers 0
ApochPiQ

562 views

As I gear up towards rewriting the Epoch compiler in Epoch, instead of in C++, I've found myself running up against a handful of peripheral issues that affect the ability to write real code in Epoch in the first place.

One of these issues relates to error handling. In my opinion, most languages conflate several issues which surround error handling, all of which should ideally be orthogonal:

  • Propagating error information as failures occur
  • Adjusting control flow in the presence of error circumstances
  • Maintaining certain contractual semantics (this file will get closed, that memory will get freed, etc.)
  • Responding to and possibly recovering from error conditions

    Consider C++ exceptions. The combination of stack unwinding and object destructors, when used with the RAII idiom, means that exceptions/RAII typically conflate all four issues! Error information is propagated by the exception payload itself; control flow is modified by stack unwinding and catch() blocks; contractual semantics are handled by RAII; and recovery is again handled by catch(). Two mechanisms, four distinct jobs... blech!

    C# or Java exceptions are similar, except they rely on finally() blocks to handle contractual obligations that are not easily enforced in purely garbage-collected languages.

    Continuation-passing style is a nice way to avoid conflating control flow with error propagation, but IMHO continuations are even harder to reason about than gotos, and are not really desirable in most code.

    So how will Epoch address these four issues? I intend to keep each of the four aspects as independent as I can. Here's how it will work:

    • Error information is propagated solely by passing parameters or returning values to/from functions
    • Control flow can be abstracted using guard {} entities and the evacuate() function
    • Contractual semantics are handled by one of two mechanisms: transactional entities, or deterministically destructed objects
    • Error recovery/response is accomplished using local functions

      Consider the following Epoch code, which sadly will not compile yet:

      entrypoint : () -> ()
      {
      sqrt(-1)
      }


      This minimal program will (theoretically) terminate due to sqrt() throwing an error. Specifically, sqrt() calls evacuate(), which performs stack unwinding up to the most recently-encountered guard {} entity, or abnormally kills the program if no guard {} is present.

      So if we want to avoid vomiting due to sqrt() failures, we simply wrap the possibly-failing call in a guard entity:

      entrypoint : () -> ()
      {
      guard
      {
      sqrt(-1)
      debugwritestring("This line will never be printed!")
      }
      }


      The debugwritestring() call will never execute, because once sqrt() evacuates, the guard will abandon the rest of the code in its block and return control flow to the next statement in the program.

      Now, suppose we want something a little richer: foo() is a function which can throw an error code, or return a value. However, it will not do both at the same time - i.e. it does not return a magic value to indicate an error. If something does go wrong, it does a little more than sqrt() does: it calls evacuate() with a parameter.

      This parameter is stored in a special register by the VM, and can be handed off to a function call via guard:

      entrypoint : () -> ()
      {
      guard(recover)
      {
      foo()
      }
      }

      recover : (integer(error)) -> ()
      {
      debugwritestring("Error code: " ; cast(string, error))
      }


      When foo() evacuates, it attaches an integer into the special register. The guard() entity in this case has a parameter as well, which is the name of a function to call if the guard catches an evacuation in progress.

      So what happens if foo() evacuates with a string instead of an integer? Well, turns out that guard {} treats its function as if it had pattern matching enabled. If guard cannot find an overload that succeeds in the pattern match, it silently re-evacuates with the same parameter it was given. This allows evacuations to cascade up the call stack until they find an appropriate handler, similar to exception handling in many other languages.

      What's cool about this is that we can use the full extent of Epoch's pattern matching facilities to handle errors!


      entrypoint : () -> ()
      {
      guard(recover)
      {
      foo()
      }
      }

      recover : (SPECIFIC_INTEGER_ERROR_CODE) -> ()
      {
      debugwritestring("A very specific error occurred!")
      }

      recover : (integer(error)) -> ()
      {
      debugwritestring("Error code: " ; cast(string, error))
      }



      Now, obviously this thing is kind of ugly if we want to contain error recovery to a specific area. What if error recovery needs to handle local state from the calling function? Clearly, this business of calling external functions isn't going to fly!

      Thankfully, Epoch allows nested function definitions. We can also use a function before it appears in code, i.e. we don't have to write foo() { ... } before calling foo. This lets us write the following code:

      entrypoint : () -> ()
      {
      guard(recovery)
      {
      foo()
      }

      recovery : (SPECIFIC_ERROR_CODE) -> () { debugwritestring("Specific error") }
      recovery : (integer(error)) -> () { debugwritestring("General error: " ; cast(string, error)) }
      }


      We could even define local variables like "bletch" inside of entrypoint(), and refer to them from within the recovery() function overloads. We can also have any number of functions that have internal recovery() functions.


      To wrap up, here's some real pie-in-the-sky speculation. This involves lambda syntax and generics, neither of which exist in Epoch yet, but are high on the priority list.

      entrypoint : () -> ()
      {
      guard( [ ] : ((error)) -> () { debugwritestring("Error!") } )
      {
      foo()
      }
      }


      In this snippet, we pass guard an anonymous lambda (I'm stealing C++11's syntax for this temporarily, just for lack of a better notation offhand). The lambda accepts one generic parameter, whose type is denoted by T (its genericity is denoted by the angle brackets). The parameter is named error but not used directly (since we can't do anything useful without knowing its concrete type).

      This enables the guard to handle any evacuation payload with the same response. It's not very practical, but it does show the direction I'm heading with the language a bit, and illuminates some of the (speculative) ideas I've had on syntax and notation.


      Keeping in mind that all this is set in wet concrete, let me know what you think!

1
Sign in to follow this  
Followers 0


4 Comments


Nice. :)

Is it possible to pass no parameters / more than one parameter to evacuate() (and - maybe opening a different can of worms - would it be possible to catch them generically)? I'm not saying this would actually be a good idea or not, it's just something that sticks out.

Is there any kind of "exception specification" so a user can know what functions "throw" what (or fulfil what "exception safety guarantees")? (or maybe that's just something that could be relegated to an IDE tool).
0

Share this comment


Link to comment
I was looking at a similar pattern matching sort of implementation for exception handling, though I also don't see how this is too much different from y'know... exception handling. It certainly a bit cleaner syntax/implementation, but I'm not sure how this gets you too far down your orthogonal road in practice.
0

Share this comment


Link to comment
evacuate() can accept 0 parameters, sure. Handling them via generics is definitely something I'd like to see, as sketched out in the final code snippet.


As for the orthogonality - I'll admit it does still entangle control flow and out-of-band error information, although I tend to think of that as the least harmful pairing. I certainly am having trouble thinking up a clean way to do out-of-band error tracking that doesn't amount to global state, which is just icky.

The real bonus comes when you start thinking about deterministic destruction and contract enforcement. I'll write up some more details on my thoughts in those areas later on.
0

Share this comment


Link to comment
Oh sure, I don't disagree that simply being able to supply/reuse exception handlers is a fairly significant win. Just pointing out that nothing here seems too distant from exceptions like you suggested (though I think that we maybe shouldn't strive too far from them without good reason).
0

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now