source: tests/exceptions/except-2.cfa @ a037f85

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since a037f85 was 3b9c674, checked in by Andrew Beach <ajbeach@…>, 4 years ago

Expections now supported in position independent code.

  • Property mode set to 100644
File size: 2.0 KB
RevLine 
[0ec9229]1// New draft of exception tests.
2
3
[73abe95]4#include <stdlib.hfa>
[dc8511c]5#include "except-mac.hfa"
[0ec9229]6
7TRIVIAL_EXCEPTION(yin)
8TRIVIAL_EXCEPTION(yang)
9
10struct num_error;
11struct num_error_vtable {
[406a6e6]12        struct TABLE(BASE_EXCEPT) const * parent;
[0ec9229]13        size_t size;
[3b9c674]14        void (*copy)(num_error &this, num_error & other);
15        void (*free)(num_error &this);
[0ec9229]16        const char * (*msg)(num_error *this);
17        int (*code)(num_error *this);
18};
19extern num_error_vtable INSTANCE(num_error);
[e9145a3]20
[0ec9229]21struct num_error {
22        struct num_error_vtable const * virtual_table;
23        char * msg;
24        int num;
25};
[e9145a3]26
[0ec9229]27void num_error_msg(num_error * this) {
28        if ( ! this->msg ) {
[e9145a3]29                static const char * base = "Num Error with code: X";
[3b9c674]30                this->msg = (char *)malloc(22);
[e9145a3]31                for (int i = 0 ; (this->msg[i] = base[i]) ; ++i);
[0ec9229]32        }
33        this->msg[21] = '0' + this->num;
34        return this->msg;
35}
[3b9c674]36void ?{}(num_error & this, int num) {
37        this.virtual_table = &INSTANCE(num_error);
38        this.msg = 0;
39        this.num = num;
[0ec9229]40}
[3b9c674]41void ?{}(num_error & this, num_error & other) {
42        this.virtual_table = other.virtual_table;
43        this.msg = 0;
44        this.num = other.num;
[0ec9229]45}
[3b9c674]46void ^?{}(num_error & this) {
47        if( this.msg ) free( this.msg );
[0ec9229]48}
49int num_error_code( num_error * this ) {
50        return this->num;
51}
52num_error_vtable _num_error_vtable_instance @= {
[fcc88a4]53        &INSTANCE(BASE_EXCEPT),
[0ec9229]54        sizeof(num_error), ?{}, ^?{},
55        num_error_msg, num_error_code
56};
57
58
59// Test simple throwing, matching and catching.
60void throw_catch() {
61        try {
62                yin black;
[e9145a3]63                THROW(&black);
[fcc88a4]64        } catch ( yin * error ) {
[0ec9229]65                printf("throw yin caught.\n");
66        }
67
68        try {
69                yang white;
[e9145a3]70                THROW_RESUME(&white);
[0ec9229]71                printf("> throwResume returned.\n");
[fcc88a4]72        } catchResume ( yang * error ) {
[0ec9229]73                printf("throwResume yang caught <");
74        }
75
76        try {
77                num_error x = { 2 };
[e9145a3]78                THROW(&x);
[0ec9229]79        }
[fcc88a4]80        catch (num_error * error ; 3 == error->virtual_table->code( error ) ) {
81                printf("exception at %p\n", error );
[0ec9229]82                printf("Should not be printed.\n");
83        }
[fcc88a4]84        catch (num_error * error ; 2 == error->virtual_table->code( error ) ) {
[0ec9229]85                printf("Should be printed.\n");
[fcc88a4]86        }
[0ec9229]87}
88
89int main (int argc, char * argv[]) {
90        throw_catch();
91        return 0;
92}
Note: See TracBrowser for help on using the repository browser.