Ignore:
Timestamp:
Aug 17, 2017, 4:13:42 PM (8 years ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
8f6dfe7
Parents:
21f0aa8
Message:

Updated exception tests to exception structures. Should be re-orginized still.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/tests/except-2.c

    r21f0aa8 re9145a3  
    22
    33
    4 #include <string.h>
     4#include <stdlib>
     5#include "except-mac.h"
    56
    6 // Local Exception Types and manual vtable types.
    7 #define GLUE2(left, right) left##right
    8 #define GLUE3(left, middle, right) left##middle##right
    9 #define BASE_EXCEPT __cfaehm__base_exception_t
    10 #define TABLE(name) GLUE2(name,_vtable)
    11 #define INSTANCE(name) GLUE3(_,name,_vtable_instance)
    12 #define TRIVIAL_EXCEPTION(name) \
    13 struct name; \
    14 struct TABLE(name) { \
    15         struct TABLE(BASE_EXCEPT) const * parent; \
    16         size_t size; \
    17         void (*copy)(name *this, name * other); \
    18         void (*free)(name *this); \
    19         const char * (*msg)(name *this); \
    20 }; \
    21 extern TABLE(name) INSTANCE(name); \
    22 struct name { \
    23         struct TABLE(name) const * virtual_table; \
    24 }; \
    25 const char * name##_msg(name * this) { \
    26         return #name; \
    27 } \
    28 void name##_copy(name * this, name * other) { \
    29         this->virtual_table = other->virtual_table; \
    30 } \
    31 TABLE(name) INSTANCE(name) @= { \
    32         .parent : &INSTANCE(BASE_EXCEPT), \
    33         .size : sizeof(name), .copy : name##_copy, \
    34         .free : ^?{}, .msg : name##_msg \
    35 }; \
    36 void ?{}(name * this) { \
    37         this->virtual_table = &INSTANCE(name); \
    38 }
    397TRIVIAL_EXCEPTION(yin)
    408TRIVIAL_EXCEPTION(yang)
     
    5018};
    5119extern num_error_vtable INSTANCE(num_error);
     20
    5221struct num_error {
    5322        struct num_error_vtable const * virtual_table;
     
    5524        int num;
    5625};
     26
    5727void num_error_msg(num_error * this) {
    5828        if ( ! this->msg ) {
    59                 const char * base = "Num Error with code: X";
    60                 this->msg = strdup( base );
     29                static const char * base = "Num Error with code: X";
     30                this->msg = malloc(22);
     31                for (int i = 0 ; (this->msg[i] = base[i]) ; ++i);
    6132        }
    6233        this->msg[21] = '0' + this->num;
     
    9061        try {
    9162                yin black;
    92                 throw (BASE_EXCEPT *)&black;
     63                THROW(&black);
    9364        } catch ( yin * error ) {
    9465                printf("throw yin caught.\n");
     
    9768        try {
    9869                yang white;
    99                 throwResume (BASE_EXCEPT *)&white;
     70                THROW_RESUME(&white);
    10071                printf("> throwResume returned.\n");
    10172        } catchResume ( yang * error ) {
     
    10576        try {
    10677                num_error x = { 2 };
    107                 throw (BASE_EXCEPT *)&x;
     78                THROW(&x);
    10879        }
    10980        catch (num_error * error ; 3 == error->virtual_table->code( error ) ) {
Note: See TracChangeset for help on using the changeset viewer.