C:/Users/Dennis/src/lang/russell.orig/src/pass1/applinfer/mkappl2.c

Go to the documentation of this file.
00001 # define DEBUG
00002 # undef DEBUG
00003 # include "parm.h"
00004 
00005 # include <stdio.h>
00006 
00007 # include "stree/ststructs.mh"
00008 
00009 extern FILE * unparse_file;
00010 
00011 # ifdef BAD
00012         extern boolean BADflag;
00013 # endif
00014 
00015 NODE * cor_cond();
00016 
00017 NODE * cand_cond();
00018 
00019 NODE ** outermost_op();
00020 
00021 /*
00022  *  mkappl2(h,t)
00023  *
00024  *  input:  A denotation represented as an array of pointers to primaries.
00025  *          h points to the start of the array, t to the end. The array
00026  *          should not be empty.
00027  *
00028  *  output: a tree of applications and conditionals (which reflect the
00029  *          precedence of operations in the denotation), in the form of
00030  *          an arg list for a higher level application.
00031  *          The outermost node always has a ref count of zero.
00032  */
00033 
00034 NODE * mkappl2(h,t)
00035 NODE    ** h,
00036         ** t;
00037 {
00038     NODE ** op,        /* "outermost" operator (i.e. the operation performed
00039                           last) in the denotation                          */
00040          * result,
00041          * left_arg,   /* a tree of applications which is the left arg of op */
00042          * right_arg,
00043          ** rest = t+1; /* start of arguments for next applications  */
00044 
00045 
00046 #   ifdef DEBUG
00047         /* test for empty array */
00048             if ( h == t + 1 ) {
00049                 dbgmsg("\nmkappl2: empty array - %o, %o\n", h, t);
00050                 abort();
00051             }
00052 #   endif
00053 
00054 
00055     /* If there is just a single primary, convert it into
00056        an arg list and return.                           */
00057         if ( h == t ) {
00058             /* is it already a list? */
00059                 if ( is_list(*h) ) {
00060                     /* Copy it and return the copy. */
00061                         return ( copylist(*h) );
00062                 } else {
00063                     return ( mklist(*h, -1) );
00064                 }
00065         }
00066 
00067 
00068     /* Find the "outermost" operator and assume its args are
00069        the sequences of primaries to its right and left.
00070        Build a tree representing the application of that operator
00071        to it args.                                          */
00072 
00073         /* Find the outermost operator */
00074             op = outermost_op( h,t );
00075 
00076         if( op == NIL ) {
00077             /* Couldn't find a reasonable one */
00078             if (!is_list(*h)) {
00079                 /* Interpret it as curried application */
00080                     rest = h+2;
00081                     right_arg = mkappl2( h+1, h+1);
00082                     lock(right_arg);
00083                     left_arg = emptylist();
00084                     lock(left_arg);
00085                     op = h;
00086             } else {
00087                 /* no way to deal with this mess */
00088                 yyperror("Improperly bracketed expression");
00089                 return( mklist(emptylist(), -1) );
00090             }
00091         } else {
00092 
00093           /* Construct the left argument tree */
00094             if ( op == h )
00095                 left_arg = emptylist();
00096             else
00097                 left_arg = mkappl2( h, op-1 );
00098             lock(left_arg);
00099 
00100           /* Construct the right argument tree */
00101             if ( op == t )
00102                 right_arg = emptylist();
00103             else
00104                 right_arg = mkappl2( op+1, t );
00105             lock(right_arg);
00106         }
00107 
00108         /* Construct the application of op to args,
00109             or conditional in the case of "cand" and "cor" */
00110             switch ( (*op) -> kind ) {
00111                 case WORDCAND:
00112                     /* Check that there are only two args */
00113                         single_arg(left_arg); single_arg(right_arg);
00114                     result = cand_cond(left_arg, right_arg);
00115                     vfree(unlock(left_arg));  vfree(unlock(right_arg));
00116                     break;
00117                 case WORDCOR:
00118                     /* Check that there are only two args */
00119                         single_arg(left_arg); single_arg(right_arg);
00120                     result = cor_cond(left_arg, right_arg);
00121                     vfree(unlock(left_arg));  vfree(unlock(right_arg));
00122                     break;
00123                 default:
00124                     unlock(left_arg); unlock(right_arg);
00125                     result = mknode(APPLICATION, *op, conc(left_arg,right_arg));
00126                     /* add curried applications */
00127                         while (rest <= t) {
00128                             result = mknode(APPLICATION, result,
00129                                             mkappl2( rest, rest ));
00130                             rest++;
00131                         }
00132             }
00133 
00134 
00135 #           ifdef BAD
00136                 if (BADflag)
00137                     flcheck(0,0);
00138                 else
00139                     flcheck(0,1);
00140 #           endif
00141         /* Make it into an arg list and return it */
00142             return ( mklist(result, -1) );
00143 }

Generated on Fri Jan 25 10:39:46 2008 for russell by  doxygen 1.5.4