source: tests/exceptions/polymorphic.cfa@ 4ef08f7

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 4ef08f7 was 8e2cb4a, checked in by Andrew Beach <ajbeach@…>, 5 years ago

Small typo but it was annoying me.

  • Property mode set to 100644
File size: 1.5 KB
RevLine 
[21b0a23]1// Testing polymophic exception types.
2
3#include <exception.hfa>
4
[5727c23]5FORALL_TRIVIAL_EXCEPTION(proxy, (otype T), (T));
6FORALL_TRIVIAL_INSTANCE(proxy, (otype U), (U))
[21b0a23]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
[8e2cb4a]13void proxy_test(void) {
[21b0a23]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.