Changeset 24d6572 for tests/forall.cfa


Ignore:
Timestamp:
Jun 12, 2023, 2:45:32 PM (2 years ago)
Author:
Fangren Yu <f37yu@…>
Branches:
ast-experimental, master
Children:
62d62db
Parents:
34b4268 (diff), 251ce80 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into ast-experimental

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tests/forall.cfa

    r34b4268 r24d6572  
    1010// Created On       : Wed May  9 08:48:15 2018
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Jun  5 10:06:08 2021
    13 // Update Count     : 36
    14 //
     12// Last Modified On : Thu Feb 23 20:29:59 2023
     13// Update Count     : 91
     14//
     15
     16#include <fstream.hfa>
    1517
    1618void g1() {
    17         forall( T ) T f( T ) {};
    18         void f( int ) {};
    19         void h( void (*p)(void) ) {};
    20 
    21         int x;
    22         void (*y)(void);
    23         char z;
    24         float w;
     19        forall( T ) T f( T p ) { sout | 'f'; return p;  };
     20        void f( int p ) { sout | p; };
     21        void g( void ) { sout | 'g'; };
     22        void h( void (*p)(void) ) { p(); };
     23
     24        int x = 1;
     25        void (*y)(void) = g;
     26        char z = 'a';
     27        float w = 3.5;
    2528
    2629        f( x );
     
    2831        f( z );
    2932        f( w );
     33        h( y );
     34        f( y );
    3035        h( f( y ) );
    3136}
    3237
    3338void g2() {
    34         forall( T ) void f( T, T ) {}
    35         forall( T, U ) void f( T, U ) {}
     39        forall( T ) void f( T, T ) { sout | "fT"; }
     40        forall( T, U ) void f( T, U ) { sout | "fTU"; }
    3641
    3742        int x;
    3843        float y;
    39         int *z;
    40         float *w;
    41 
     44        int * z;
     45        float * w;
     46
     47        f( x, x );
     48        f( y, y );
     49        f( w, w );
    4250        f( x, y );
    4351        f( z, w );
     
    5058
    5159forall( T )
    52 void swap( T left, T right ) {
    53         T temp = left;
    54         left = right;
    55         right = temp;
    56 }
    57 
    58 trait sumable( T ) {
     60void swap( T & left, T & right ) {                                              // by reference
     61    T temp = left;
     62    left = right;
     63    right = temp;
     64}
     65
     66forall( T )
     67[ T, T ] swap( T i, T j ) {                                                             // by value
     68    return [ j, i ];
     69}
     70
     71forall( T ) trait sumable {
    5972        void ?{}( T &, zero_t );                                                        // 0 literal constructor
    6073        T ?+?( T, T );                                                                          // assortment of additions
     
    6477}; // sumable
    6578
    66 forall( T | sumable( T ) )                                              // use trait
     79forall( T | sumable( T ) )                                                              // use trait
    6780T sum( size_t size, T a[] ) {
    6881        T total = 0;                                                                            // initialize by 0 constructor
     
    7285} // sum
    7386
    74 forall( T | { T ?+?( T, T ); T ?++( T & ); [T] ?+=?( T &,T ); } )
     87forall( T | { T ?+?( T, T ); T ?++( T & ); [T] ?+=?( T &, T ); } )
    7588T twice( T t ) {
    7689        return t + t;
     
    8295}
    8396
    84 int fred() {
    85         int x = 1, y = 2, a[10];
     97void fred() {
     98        int x = 1, y = 2, a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    8699        float f;
    87100
     101        sout | x | y;
    88102        swap( x, y );
    89         twice( x );
     103        sout | x | y | nl | swap( x, y );
     104        // [ x, y ] = swap( y, x );
     105        sout | twice( ' ' ) | ' ' | twice( 0hh ) | twice( 1h ) | twice( 0n ) | twice( 2 )
     106                 | twice( 3.2f ) | twice( 3.2 ) | twice( 3.2d ) | twice( 3.2+1.5i ) | twice( x );
    90107        f = min( 4.0, 3.0 );
    91         sum( 10, a );
     108        sout | f | min( 4.0, 3.0 );
     109        sout | sum( 10, a );
    92110}
    93111
     
    177195
    178196forall( T ) struct S { T t; } (int) x, y, z;
    179 forall( T ) struct { T t; } (int) a, b, c;
     197static forall( T ) struct { T t; } (int) a, b, c;
    180198
    181199forall( T ) static forall( S ) {
     
    186204
    187205forall( T ) {
    188         extern "C" {
     206//      extern "C" {
    189207                struct SS { T t; };
    190                 T foo( T ) {}
    191         }
     208                T foo( T p ) { return p; }
     209//      }
    192210}
    193211
     
    195213W(int,int) w;
    196214
    197 int jane() {
     215void jane() {
    198216//      int j = bar( 3, 4 );
    199217        int k = baz( 3, 4, 5 );
    200218        int i = foo( 3 );
     219        sout | k | i;
    201220}
    202221
     
    211230        T t;
    212231        T t2 = t;
     232        sout | &tr | tp;
    213233}
    214234
     
    242262
    243263int main( void ) {
    244     #pragma GCC warning "Compiled"                      // force non-empty .expect file, NO TABS!!!
     264        g1();
     265        g2();
     266        fred();
     267        jane();
    245268}
    246269
Note: See TracChangeset for help on using the changeset viewer.