Ignore:
Timestamp:
Jul 10, 2024, 3:39:37 AM (9 months ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
725f777f
Parents:
bb336a6 (diff), f3811df (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' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified doc/theses/mike_brooks_MMath/programs/hello-array.cfa

    rbb336a6 rdbff8ec  
    88
    99
    10 forall( [N] ) // array bound
    11 array(bool, N) & f( array(float, N) & a, array(float, N) & b ) {
    12         array(bool, N) & ret = *alloc(); // sizeof used by alloc
    13         for( i; N ) {
    14                 ret[i] = 0.005 > 2 * (abs(a[i] - b[i])) / (abs(a[i]) + abs(b[i]));
     10forall( [@N@] )                                                         $\C{// array dimension}$
     11array( bool, @N@) & f( array( float, @N@ ) & x, array( float, @N@ ) & y ) {
     12        array( bool, @N@ ) & ret = *@alloc@();  $\C{// sizeof ret  used by alloc}$
     13        for ( i; @N@ ) {
     14                ret[i] = 0.005 > 2 * (abs( x[i] - y[i] )) / (abs( x[i]) + abs(y[i] ));
    1515        }
    1616        return ret;
     
    2929
    3030int main( int argc, char * argv[] ) {
    31         int n = ato( argv[1] );
    32         array(float, n) a, b; // VLA
    33         for ( i; n ) {
    34                 a[i] = 3.14 / (i + 1);
    35                 b[i] = a[i] + 0.005 ;
     31        const int @n@ = ato( argv[1] );                 $\C{// deduce conversion type}$
     32        array( float, @n@ ) x, y;                               $\C{// VLAs}$
     33        for ( i; n ) {                                                  $\C{// initialize arrays}$
     34                x[i] = 3.14 / (i + 1);
     35                y[i] = x[i] + 0.005 ;
    3636        }
    37         array(bool, n) & result = f( a, b ); // call
    38         sout | "result: " | nonl;
     37        array( bool, @n@ ) & result = @f( x, y )@; $\C{// call}$
     38        sout | "result: " | nonl;                               $\C{// print result}$
    3939        for ( i; n )
    4040                sout | result[i] | nonl;
    4141        sout | nl;
    42         free( &result ); // free returned storage
     42        free( &result );                                                $\C{// free result storage}$
    4343}
    4444/*
     
    5050
    5151void fred() {
    52         array(float, 10) a;
    53         array(float, 20) b;
    54         f( a, a );
    55         f( b, b );
    56         f( a, b );
     52        array( float, @10@ ) x;
     53        array( float, @20@ ) y;
     54        f( x, x );
     55        f( y, y );
     56//      f( x, y );
    5757}
    5858
    5959#ifdef SHOWERR1
    6060forall( [M], [N] )
    61 void bad( array(float, M) &a, array(float, N) &b ) {
    62         f( a, a ); // ok
    63         f( b, b ); // ok
    64         f( a, b ); // error
     61void bad( array(float, M) &x, array(float, N) &y ) {
     62        f( x, x );              $\C[1.5in]{// ok}$
     63        f( y, y );              $\C{// ok}$
     64        f( x, y );              $\C{// error}\CRT$
    6565}
    6666#endif
     
    6969
    7070forall( [M], [N] )
    71 void bad_fixed( array(float, M) & a, array(float, N) & b ) {
    72         if ( M == N ) {
    73                 f( a, (array(float, M) &)b ); // cast b to matching type
    74         }
     71void bad_fixed( array( float, M ) & x, array( float, N ) & y ) {
     72        if ( M == N )
     73                f( x, @(array( float, M ) &)@y ); $\C{// cast y to matching type}$
    7574}
     75
     76// Local Variables: //
     77// compile-command: "sed -f sedcmd hello-array.cfa > ../build/tmp.cfa; cfa ../build/tmp.cfa -Wall -Wextra" //
     78// End: //
Note: See TracChangeset for help on using the changeset viewer.