source: tests/exceptions/conditional.cfa @ 42cd451e

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 42cd451e was 046a890, checked in by Andrew Beach <ajbeach@…>, 4 years ago

That should get default operations working for throws. More tests to come.

  • Property mode set to 100644
File size: 1.7 KB
Line 
1// Conditional Catch Test
2
3// I may fold this back into terminate.cfa and resume.cfa once setting
4// up the non-trivial exception is reasonable to do.
5
6#include <exception.hfa>
7#include <stdio.h>
8
9VTABLE_DECLARATION(num_error)(
10        int (*code)(num_error *this);
11);
12
13struct num_error {
14        VTABLE_FIELD(num_error);
15    char * msg;
16    int num;
17};
18
19void num_error_msg(num_error * this) {
20    if ( ! this->msg ) {
21        static const char * base = "Num Error with code: X";
22        this->msg = (char *)malloc(22);
23        for (int i = 0 ; (this->msg[i] = base[i]) ; ++i);
24    }
25    this->msg[21] = '0' + this->num;
26    return this->msg;
27}
28void ?{}(num_error & this, int num) {
29        VTABLE_INIT(this, num_error);
30    this.msg = 0;
31    this.num = num;
32}
33void ?{}(num_error & this, num_error & other) {
34    this.virtual_table = other.virtual_table;
35    this.msg = 0;
36    this.num = other.num;
37}
38void ^?{}(num_error & this) {
39    if( this.msg ) free( this.msg );
40}
41int num_error_code( num_error * this ) {
42    return this->num;
43}
44
45VTABLE_INSTANCE(num_error)(
46        num_error_msg,
47        num_error_code,
48);
49
50void caught_num_error(int expect, num_error * actual) {
51        printf("Caught num_error: expected=%d actual=%d.\n", expect, actual->num);
52}
53
54int main(int argc, char * argv[]) {
55        num_error exc = 2;
56
57        try {
58                throw exc;
59        } catch (num_error * error ; 3 == error->virtual_table->code( error )) {
60                caught_num_error(3, error);
61        } catch (num_error * error ; 2 == error->virtual_table->code( error )) {
62                caught_num_error(2, error);
63        }
64
65        try {
66                throwResume exc;
67        } catchResume (num_error * error ; 3 == error->virtual_table->code( error )) {
68                caught_num_error(3, error);
69        } catchResume (num_error * error ; 2 == error->virtual_table->code( error )) {
70                caught_num_error(2, error);
71        }
72}
Note: See TracBrowser for help on using the repository browser.