source: tests/tuple/tuplePolymorphism.cfa @ 2980ccb8

Last change on this file since 2980ccb8 was fd54fef, checked in by Michael Brooks <mlbrooks@…>, 4 years 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.7 KB
RevLine 
[f7e749f]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       : Tue Nov 16 10:38:00 2016
[17e0dc9]11// Last Modified By : Peter A. Buhr
12// Last Modified On : Thu May 18 18:05:12 2017
13// Update Count     : 4
[f7e749f]14//
15
[ebb5ed9]16// packed is needed so that structs are not passed with the same alignment as function arguments
17__attribute__((packed)) struct A {
[17e0dc9]18        double x;
19        char y;
20        double z;
[ebb5ed9]21};
22
23__attribute__((packed)) struct B {
[17e0dc9]24        long long x;
25        char y;
26        long long z;
[ebb5ed9]27};
28
29// ensure that f is a viable candidate for g, even though its parameter structure does not exactly match
30[A] f([A, B] x, B y) { printf("%g %c %g %lld %c %lld %lld %c %lld\n", x.0.[x,y,z], x.1.[x,y,z], y.[x,y,z]); return x.0; }
[fd54fef]31forall(T, U | { T f(T, U, U); })
[ebb5ed9]32void g(T x, U y) { f(x, y, y); }
33
34// add two triples
[fd54fef]35forall(T | { T ?+?(T, T); })
[f7e749f]36[T, T, T] ?+?([T, T, T] x, [T, T, T] y) {
37        return [x.0+y.0, x.1+y.1, x.2+y.2];
38}
39
40int main() {
[17e0dc9]41        int x1 = 123, x3 = 456;
42        double x2 = 999.123;
[f7e749f]43
[17e0dc9]44        int i1 = 111, i3 = 222;
45        double i2 = 333;
[f7e749f]46
[17e0dc9]47        int d1 = 555, d3 = 444;
48        double d2 = 666;
[f7e749f]49
50
[17e0dc9]51        [i1, i2, i3] = ([x1, (int)x2, x3]) + ([9, 2, 3]);
52        [d1, d2, d3] = ([x1, x2, x3]) + ([9, 2, 3]);
53        printf("%d %g %d\n", i1, i2, i3);
54        printf("%d %g %d\n", d1, d2, d3);
[f7e749f]55
[17e0dc9]56        [double, double, double] zzz;
57        zzz = [x1, x2, x3];
58        printf("%g %g %g\n", zzz);
59        [x1, x2, x3] = zzz+zzz;
60        printf("%d %g %d\n", x1, x2, x3);
[ebb5ed9]61
[17e0dc9]62        // ensure non-matching assertions are specialized correctly
63        g((A){ 1.21, 'x', 10.21}, (B){ 1111LL, 'v', 54385938LL });
[f7e749f]64}
65
[fd54fef]66forall(T)
[f7e749f]67[T, T] foo([T, T] y) {
68        [T, T] x;
69        return x;
70}
71
72// Local Variables: //
73// tab-width: 4 //
74// End: //
Note: See TracBrowser for help on using the repository browser.