source: tests/tuple/tupleVariadic.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: 2.7 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// tuplePolymorphism.c --
8//
9// Author           : Rob Schluntz
10// Created On       : Fri Dec 16 10:25:35 2016
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Thu Aug  2 09:24:04 2018
13// Update Count     : 6
14//
15
16#include <stdlib.hfa>
17
18void func(void) {
19        printf("called func(void)\n");
20}
21forall(T, Params... | { void process(T); void func(Params); })
22void func(T arg1, Params p) {
23        process(arg1);
24        func(p);
25}
26void process(int x) {
27        printf("called process(int) %d\n", x);
28}
29void process(double x) {
30        printf("called process(double) %g\n", x);
31}
32
33struct array {
34        int * data;
35        int size;
36};
37
38// xxx - eventually this will be collapsed...x
39void ?{}(array & a) {
40        a.size = 0;
41        a.data = 0;
42        printf("called ?{} with no a\n");
43}
44
45void ?{}(array & a, int a0) {
46        a.size = 1;
47        a.data = (int*)malloc(sizeof(int)*a.size);
48        a.data[0] = a0;
49        printf("called ?{} with a: %d\n", a0);
50}
51
52void ?{}(array & a, int a0, int a1) {
53        a.size = 2;
54        a.data = (int*)malloc(sizeof(int)*a.size);
55        a.data[0] = a0;
56        a.data[1] = a1;
57        printf("called ?{} with a: %d %d\n", a0, a1);
58}
59
60void ?{}(array & a, int a0, int a1, int a2) {
61        a.size = 3;
62        a.data = (int*)malloc(sizeof(int)*a.size);
63        a.data[0] = a0;
64        a.data[1] = a1;
65        a.data[2] = a2;
66        printf("called ?{} with a: %d %d %d\n", a0, a1, a2);
67}
68
69void ^?{}(array & a) {
70        free(a.data);
71}
72
73// test use of a tuple argument
74[void] ?{}(array & a, [int, int, int, int] args) {
75        int a0, a1, a2, a3;
76        [a0, a1, a2, a3] = args;
77        a.size = 4;
78        a.data = (int *)malloc(sizeof(int)*a.size);
79        a.data[0] = a0;
80        a.data[1] = a1;
81        a.data[2] = a2;
82        a.data[3] = a3;
83        printf("called ?{} with a: %d %d %d %d\n", a0, a1, a2, a3);
84}
85
86void print(array * x) {
87        printf("array = { ");
88        for (int i = 0; i < x->size; ++i) {
89                printf("%d, ", x->data[i]);
90        }
91        printf("}\n");
92}
93
94forall(T)
95T * copy(T x) {
96        // test calling new inside a polymorphic function
97        return new(x);
98}
99
100forall(T... | { void foo(T); }) void bar(T x) {}
101void foo(int) {}
102
103int main() {
104        array * x0 = new();
105        print(x0);
106
107        array * x1 = new(999);
108        print(x1);
109
110        array * x2 = new(123, 456);
111        print(x2);
112
113        array * x3 = new(100, 200, 300);
114        print(x3);
115
116        array * x4 = new(10, 2, 3, 4);
117        print(x4);
118
119        int * ptr = copy(111111);
120        printf("copy=%d\n", *ptr);
121
122        printf("calling func\n");
123        func(3, 2.0, 111, 4.145);
124        printf("finished func\n");
125
126        {
127                // T = [const int] -- this ensures that void(*)(int) satisfies void(*)(const int)
128                const int x;
129                bar(x);
130        }
131
132        delete(ptr);
133        delete(x4);
134        delete(x3);
135        delete(x2);
136        delete(x1);
137        delete(x0);
138}
139
140// Local Variables: //
141// tab-width: 4 //
142// End: //
Note: See TracBrowser for help on using the repository browser.