source: tests/exceptions/polymorphic.cfa @ 474d610

arm-ehjacob/cs343-translationnew-ast-unique-expr
Last change on this file since 474d610 was fd54fef, checked in by Michael Brooks <mlbrooks@…>, 11 months ago

Converting the project to use the new syntax for otype, dtype and ttytpe.

Changed prelude (gen), libcfa and test suite to use it. Added a simple deprecation rule of the old syntax to the parser; we might wish to support both syntaxes "officially," like with an extra CLI switch, but this measure should serve as a simple reminder for our team to try the new syntax.

  • 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, (T), (T));
6FORALL_TRIVIAL_INSTANCE(proxy, (U), (U))
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(void) {
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, (T), (T))(
36        T data;
37);
38
39FORALL_DATA_INSTANCE(cell, (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.