source: tests/zombies/withStatement.cfa @ b2ecd48

Last change on this file since b2ecd48 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.9 KB
RevLine 
[866f560]1//
2// Cforall Version 1.0.0 Copyright (C) 2017 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// tupleFunction.c --
8//
9// Author           : Rob Schluntz
10// Created On       : Mon Dec 04 17:41:45 2017
[d5b2ac8]11// Last Modified By : Peter A. Buhr
12// Last Modified On : Mon Dec 24 19:08:18 2018
13// Update Count     : 5
[866f560]14//
15
[d5b2ac8]16#include <fstream.hfa>
17
[866f560]18struct S {
[d5b2ac8]19        int i;
20        // dynamically allocated member ensures ctor/dtors are called correctly on temporaries
21        int * ptr;
[866f560]22};
23
24// with clause on reference parameter
[d5b2ac8]25void ?{}( S & this, int n ) with( this ) {
26        i = n;
27        ptr = (int *)malloc( sizeof(int) );
[866f560]28}
29
[d5b2ac8]30void ?{}( S & this ) {
31        this{ 0 };
[866f560]32}
33
[d5b2ac8]34void ?{}( S & this, S other ) {
35        this{ other.i };
[866f560]36}
37
[d5b2ac8]38S ?=?( S & this, S other ) with( this ) {
39        i = other.i;
40        *ptr = *other.ptr;
41        return this;
[866f560]42}
43
[d5b2ac8]44void ^?{}( S & this ) with( this ) {
45        free( ptr );
[866f560]46}
47
48struct S2 {
[d5b2ac8]49        S s;
[866f560]50};
51
[d5b2ac8]52void ?{}( S2 & this, int n ) {
53        (this.s){ n };
[866f560]54}
55
[fd54fef]56forall( T )
[866f560]57struct Box {
[d5b2ac8]58        T x;
[866f560]59};
60
[fd54fef]61forall( T )
[d5b2ac8]62void ?{}( Box(T) & this ) with( this ) { // with clause in polymorphic function
63        x{};
[866f560]64}
65
[d5b2ac8]66void print( int i ) { sout | i; }
[866f560]67
[fd54fef]68forall( T | { void print( T ); })
[d5b2ac8]69void foo( T t ) {
70        Box( T ) b = { t };
71        with( b ) {  // with statement in polymorphic function
72                print( x );
73        }
[866f560]74}
75
76// ensure with-statement temporary generation works correctly
77S mk() {
[d5b2ac8]78        sout | "called mk";
79        return (S){ 444 };
[866f560]80}
81
82// ensure with-statement temporary generation with reference-returning functions works correctly
83S & ref() {
[d5b2ac8]84        static S var = { 123456789 };
85        return var;
[866f560]86}
87
88int main() {
[d5b2ac8]89        S2 s2 = { 12345 };
90        with ( s2) {
91                with( s ) { // with s2.s
92                        sout | i | s.i | s2.s.i;
93                        foo( i );  // s.i
94                        with( mk()) {
95                                sout | i | i | i;
96                                with( ref()) {
97                                        sout | i | i | i;
98                                } // with ref()
99                                with( ref()) {
100                                        sout | i | i | i;
101                                } // with ref()
102                        } // with mk()
103                } // with s
104        } // with s2
[866f560]105}
106
107// Local Variables: //
108// tab-width: 4 //
109// End: //
Note: See TracBrowser for help on using the repository browser.