source: src/tests/tupleVariadic.c @ 1e3d5b6

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 1e3d5b6 was 1e3d5b6, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

updated tupleVariadic test to include recursive example

  • Property mode set to 100644
File size: 2.4 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 : Rob Schluntz
12// Last Modified On : Fri Dec 21 14:42:48 2016
13// Update Count     : 2
14//
15
16void func(void) {
17        printf("called func(void)\n");
18}
19forall(otype T, ttype Params | { void process(T); void func(Params); })
20void func(T arg1, Params p) {
21        process(arg1);
22        func(p);
23}
24void process(int x) {
25        printf("called process(int) %d\n", x);
26}
27void process(double x) {
28        printf("called process(double) %g\n", x);
29}
30
31forall(otype T) T * malloc();
32
33forall(otype T, ttype Params | sized(T) | { void ?{}(T *, Params); })
34T * new(Params p) {
35        return ((T*)malloc()){ p };
36}
37
38struct array {
39        int * data;
40        int size;
41};
42
43// xxx - eventually this will be collapsed...x
44void ?{}(array * a) {
45        a->size = 0;
46        a->data = 0;
47        printf("called ?{} with no a\n");
48}
49
50void ?{}(array * a, int a0) {
51        a->size = 1;
52        a->data = (int*)malloc(sizeof(int)*a->size);
53        a->data[0] = a0;
54        printf("called ?{} with a: %d\n", a0);
55}
56
57void ?{}(array * a, int a0, int a1) {
58        a->size = 2;
59        a->data = (int*)malloc(sizeof(int)*a->size);
60        a->data[0] = a0;
61        a->data[1] = a1;
62        printf("called ?{} with a: %d %d\n", a0, a1);
63}
64
65void ?{}(array * a, int a0, int a1, int a2) {
66        a->size = 3;
67        a->data = (int*)malloc(sizeof(int)*a->size);
68        a->data[0] = a0;
69        a->data[1] = a1;
70        a->data[2] = a2;
71        printf("called ?{} with a: %d %d %d\n", a0, a1, a2);
72}
73
74// test use of a tuple argument
75[void] ?{}(array * a, [int, int, int, int] args) {
76        int a0, a1, a2, a3;
77        [a0, a1, a2, a3] = args;
78        a->size = 4;
79        a->data = malloc(sizeof(int)*a->size);
80        a->data[0] = a0;
81        a->data[1] = a1;
82        a->data[2] = a2;
83        a->data[3] = a3;
84        printf("called ?{} with a: %d %d %d %d\n", a0, a1, a2, a3);
85}
86
87void print(array * x) {
88        printf("array = { ");
89        for (int i = 0; i < x->size; ++i) {
90                printf("%d, ", x->data[i]);
91        }
92        printf("}\n");
93}
94
95int main() {
96        array * x0 = new();
97        print(x0);
98
99        array * x1 = new(999);
100        print(x1);
101
102        array * x2 = new(123, 456);
103        print(x2);
104
105        array * x3 = new(100, 200, 300);
106        print(x3);
107
108        array * x4 = new(10, 2, 3, 4);
109        print(x4);
110        printf("calling func\n");
111        func(3, 2.0, 111, 4.145);
112        printf("finished func\n");
113}
114
115// Local Variables: //
116// tab-width: 4 //
117// End: //
118
Note: See TracBrowser for help on using the repository browser.