source: src/tests/except-2.c @ db67b11

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since db67b11 was 0ec9229, checked in by Andrew Beach <ajbeach@…>, 7 years ago

Missed the small test for the new type of exceptions.

  • Property mode set to 100644
File size: 2.9 KB
Line 
1// New draft of exception tests.
2
3
4#include <string.h>
5
6// Local Exception Types and manual vtable types.
7#define BASE_EXCEPT __cfaehm__base_exception_t
8#define TABLE(name) name##_vtable
9#define INSTANCE(name) _##name##_vtable_instance
10#define TRIVIAL_EXCEPTION(name) \
11struct name; \
12struct TABLE(name) { \
13        struct __cfaehm__base_exception_t_vtable const * parent; \
14        size_t size; \
15        void (*copy)(name *this, name * other); \
16        void (*free)(name *this); \
17        const char * (*msg)(name *this); \
18}; \
19extern TABLE(name) INSTANCE(name); \
20struct name { \
21        struct TABLE(name) const * virtual_table; \
22}; \
23const char * name##_msg(name * this) { \
24        return #name; \
25} \
26void name##_copy(name * this, name * other) { \
27        this->virtual_table = other->virtual_table; \
28} \
29TABLE(name) INSTANCE(name) @= { \
30        .parent : &INSTANCE(__cfaehm__base_exception_t), \
31        .size : sizeof(name), .copy : name##_copy, \
32        .free : ^?{}, .msg : name##_msg \
33}; \
34void ?{}(name * this) { \
35        this->virtual_table = &INSTANCE(name); \
36}
37TRIVIAL_EXCEPTION(yin)
38TRIVIAL_EXCEPTION(yang)
39
40struct num_error;
41struct num_error_vtable {
42        struct exception_t_vtable const * parent;
43        size_t size;
44        void (*copy)(num_error *this, num_error * other);
45        void (*free)(num_error *this);
46        const char * (*msg)(num_error *this);
47        int (*code)(num_error *this);
48};
49extern num_error_vtable INSTANCE(num_error);
50struct num_error {
51        struct num_error_vtable const * virtual_table;
52        char * msg;
53        int num;
54};
55void num_error_msg(num_error * this) {
56        if ( ! this->msg ) {
57                const char * base = "Num Error with code: X";
58                this->msg = strdup( base );
59        }
60        this->msg[21] = '0' + this->num;
61        return this->msg;
62}
63void ?{}(num_error * this, int num) {
64        this->virtual_table = &_num_error_vtable_instance;
65        this->msg = 0;
66        this->num = num;
67}
68void ?{}(num_error * this, num_error * other) {
69        this->virtual_table = other->virtual_table;
70        this->msg = 0;
71        this->num = other->num;
72}
73void ^?{}(num_error * this) {
74        if( this->msg ) free( this->msg );
75}
76int num_error_code( num_error * this ) {
77        return this->num;
78}
79num_error_vtable _num_error_vtable_instance @= {
80        &___cfaehm__base_exception_t_vtable_instance,
81        sizeof(num_error), ?{}, ^?{},
82        num_error_msg, num_error_code
83};
84
85
86// Test simple throwing, matching and catching.
87void throw_catch() {
88        try {
89                yin black;
90                throw (BASE_EXCEPT *)&black;
91        } catch( yin * error ) {
92                printf("throw yin caught.\n");
93        }
94
95        try {
96                yang white;
97                throwResume (BASE_EXCEPT *)&white;
98                printf("> throwResume returned.\n");
99        } catchResume( yang * error ) {
100                printf("throwResume yang caught <");
101        }
102
103        /* Conditional catches are still a work in progress.
104        try {
105                num_error x = { 2 };
106                throw (struct exception_t *)&x;
107        }
108        catch (num_error * error0 ; 3 == error0->virtual_table->code( error0 ) ) {
109                printf("exception at %p\n", error0 );
110                printf("Should not be printed.\n");
111        }
112        catch (num_error * error1 ; 2 == error1->virtual_table->code( error1 ) ) {
113                printf("Should be printed.\n");
114        }*/
115}
116
117int main (int argc, char * argv[]) {
118        throw_catch();
119        return 0;
120}
Note: See TracBrowser for help on using the repository browser.