source: tests/exceptions/polymophic.cfa @ 53ee27e

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

Added polymophic exception macros. It adds a number of macros to help generate polymorphic exception code and a test for it.

  • Property mode set to 100644
File size: 1.5 KB
Line 
1// Testing polymophic exception types.
2
3#include <exception.hfa>
4
5FORALL_TRIVIAL_EXCEPTION_(proxy, (otype U3), (U3));
6FORALL_TRIVIAL_INSTANCE_(proxy, (otype U4), (U4))
7
8const char * msg(proxy(int) * this) { return "proxy(int)"; }
9const char * msg(proxy(char) * this) { return "proxy(char)"; }
10POLY_VTABLE_INSTANCE(proxy, int)(msg);
11POLY_VTABLE_INSTANCE(proxy, char)(msg);
12
13void proxy_test () {
14    try {
15                throw (proxy(int)){};
16        } catch (proxy(int) *) {
17                printf("terminate catch\n");
18        }
19
20        try {
21                throwResume (proxy(char)){};
22        } catchResume (proxy(char) *) {
23                printf("resume catch\n");
24        }
25
26        try {
27                throw (proxy(char)){};
28        } catch (proxy(int) *) {
29                printf("caught proxy(int)\n");
30        } catch (proxy(char) *) {
31                printf("caught proxy(char)\n");
32        }
33}
34
35FORALL_DATA_EXCEPTION(cell, (otype T), (T))(
36        T data;
37);
38
39FORALL_DATA_INSTANCE(cell, (otype T), (T))
40
41const char * msg(cell(int) * this) { return "cell(int)"; }
42const char * msg(cell(char) * this) { return "cell(char)"; }
43const char * msg(cell(bool) * this) { return "cell(bool)"; }
44POLY_VTABLE_INSTANCE(cell, int)(msg);
45POLY_VTABLE_INSTANCE(cell, char)(msg);
46POLY_VTABLE_INSTANCE(cell, bool)(msg);
47
48void cell_test(void) {
49        try {
50                cell(int) except;
51                except.data = -7;
52                throw except;
53        } catch (cell(int) * error) {
54                printf("%d\n", error->data);
55        }
56
57        try {
58                cell(bool) ball;
59                ball.data = false;
60                throwResume ball;
61                printf("%i\n", ball.data);
62        } catchResume (cell(bool) * error) {
63                printf("%i\n", error->data);
64                error->data = true;
65        }
66}
67
68int main(int argc, char * argv[]) {
69        proxy_test();
70        printf("\n");
71        cell_test();
72}
Note: See TracBrowser for help on using the repository browser.