source: src/examples/sum.c @ 6ce9f7c7

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 6ce9f7c7 was dd51906, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

automake change gnu back to foreign (do not know why it changed), add := and & (reference) to lexer/parser

  • Property mode set to 100644
File size: 2.9 KB
RevLine 
[86bd7c1f]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//
[dd51906]7// sum.c -- test resolvers ability to deal with many variables with the same name and to use the minimum number of casts
8//    necessary to disambiguate overloaded variable names.
[86bd7c1f]9//
[097e2b0]10// Author           : Peter A. Buhr
[86bd7c1f]11// Created On       : Wed May 27 17:56:53 2015
12// Last Modified By : Peter A. Buhr
[dd51906]13// Last Modified On : Thu May 26 09:25:42 2016
14// Update Count     : 201
[86bd7c1f]15//
16
[d3b7937]17#include <fstream>
[a0d9f94]18
[b63e376]19trait sumable( otype T ) {
[86bd7c1f]20        const T 0;
21        T ?+?( T, T );
22        T ?+=?( T *, T );
[097e2b0]23        T ++?( T * );
24        T ?++( T * );
[d3b7937]25}; // sumable
[a0d9f94]26
[b63e376]27forall( otype T | sumable( T ) )
[097e2b0]28T sum( unsigned int n, T a[] ) {
29        T total = 0;                                                                            // instantiate T, select 0
30        for ( unsigned int i = 0; i < n; i += 1 )
31                total += a[i];                                                                  // select +
[86bd7c1f]32        return total;
[d3b7937]33} // sum
[a0d9f94]34
[bdd516a]35// Required to satisfy sumable as char does not have addition.
[52f85e0]36const char 0;
37char ?+?( char t1, char t2 ) { return (int)t1 + t2; }   // cast forces integer addition, otherwise recursion
38char ?+=?( char *t1, char t2 ) { *t1 = *t1 + t2; return *t1; }
39char ++?( char *t ) { *t += 1; return *t; }
40char ?++( char *t ) { char temp = *t; *t += 1; return temp; }
[bdd516a]41
[784deab]42int main( void ) {
[6e7e2b36]43        const int low = 5, High = 15, size = High - low;
[097e2b0]44
[52f85e0]45        char s = 0, a[size], v = low;
[6e7e2b36]46        for ( int i = 0; i < size; i += 1, v += 1 ) {
[097e2b0]47                s += v;
48                a[i] = v;
[52f85e0]49        } // for
[90c3b1c]50        sout | "sum from" | low | "to" | High | "is"
[0638c44]51                 | (int)sum( size, a ) | ", check" | (int)s | endl;
[86bd7c1f]52
[52f85e0]53        int s = 0, a[size], v = low;
[097e2b0]54        for ( int i = 0; i < size; i += 1, v += 1 ) {
55                s += (int)v;
56                a[i] = (int)v;
[52f85e0]57        } // for
[90c3b1c]58        sout | "sum from" | low | "to" | High | "is"
[0638c44]59                 | sum( size, (int *)a ) | ", check" | (int)s | endl;
[86bd7c1f]60
[52f85e0]61        float s = 0.0, a[size], v = low / 10.0;
[097e2b0]62        for ( int i = 0; i < size; i += 1, v += 0.1f ) {
63                s += (float)v;
64                a[i] = (float)v;
[52f85e0]65        } // for
[90c3b1c]66        sout | "sum from" | low / 10.0 | "to" | High / 10.0 | "is"
[0638c44]67                 | sum( size, (float *)a ) | ", check" | (float)s | endl;
[bd85400]68
[52f85e0]69        double s = 0, a[size], v = low / 10.0;
[d3b7937]70        for ( int i = 0; i < size; i += 1, v += 0.1 ) {
71                s += (double)v;
72                a[i] = (double)v;
[52f85e0]73        } // for
[90c3b1c]74        sout | "sum from" | low / 10.0 | "to" | High / 10.0 | "is"
[0638c44]75                 | sum( size, (double *)a ) | ", check" | (double)s | endl;
[bd85400]76
[52f85e0]77        struct S { int i, j; } 0 = { 0, 0 }, 1 = { 1, 1 };
[dd51906]78        S ?+?( S t1, S t2 ) { return (S){ t1.i + t2.i, t1.j + t2.j }; }
[52f85e0]79        S ?+=?( S *t1, S t2 ) { *t1 = *t1 + t2; return *t1; }
80        S ++?( S *t ) { *t += 1; return *t; }
81        S ?++( S *t ) { S temp = *t; *t += 1; return temp; }
[90c3b1c]82        ofstream * ?|?( ofstream * os, S v ) { return os | v.i | v.j; }
[52f85e0]83
84        S s = 0, a[size], v = { low, low };
85        for ( int i = 0; i < size; i += 1, v += (S)1 ) {
86                s += (S)v;
87                a[i] = (S)v;
88        } // for
[90c3b1c]89        sout | "sum from" | low | "to" | High | "is"
[0638c44]90                 | sum( size, (S *)a ) | ", check" | (S)s | endl;
[d3b7937]91} // main
[42dcae7]92
93// Local Variables: //
[86bd7c1f]94// tab-width: 4 //
[d3b7937]95// compile-command: "cfa sum.c" //
[42dcae7]96// End: //
Note: See TracBrowser for help on using the repository browser.