IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Dec 10, 2013, 2:55:11 PM (12 years ago)
Author:
eugene
Message:

merge changes from eam_branches/ipp-20130904

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/Ohana

  • trunk/Ohana/src/opihi

  • trunk/Ohana/src/opihi/lib.shell/evaluate_stack.c

    r34088 r36375  
    22# define VERBOSE 0
    33
    4 # define TWO_OP(A,B,FUNC) \
    5   if (!strncasecmp (&stack[i - 2].type, A, 1) && !strncasecmp (&stack[i - 1].type, B, 1)) \
    6     status = FUNC (&tmp_stack, &stack[i - 2], &stack[i - 1], stack[i].name);
    7 
    8 # define ONE_OP(A,FUNC) \
    9   if (!strncasecmp (&stack[i - 1].type, A, 1)) \
    10     status = FUNC (&tmp_stack, &stack[i - 1], stack[i].name);
     4// all three operands must have the same type
     5# define THREE_OP(A,FUNC)                                               \
     6  if ((stack[i - 3].type == A) && (stack[i - 2].type == A) && (stack[i - 1].type == A)) { \
     7    status = FUNC (&tmp_stack, &stack[i - 3], &stack[i - 2], &stack[i - 1], stack[i].name); \
     8    goto got_three_op; } \
     9  if ((stack[i - 3].type == A+1) && (stack[i - 2].type == A) && (stack[i - 1].type == A)) { \
     10    status = FUNC (&tmp_stack, &stack[i - 3], &stack[i - 2], &stack[i - 1], stack[i].name); \
     11    goto got_three_op; } \
     12  if ((stack[i - 3].type == A) && (stack[i - 2].type == A+1) && (stack[i - 1].type == A)) { \
     13    status = FUNC (&tmp_stack, &stack[i - 3], &stack[i - 2], &stack[i - 1], stack[i].name); \
     14    goto got_three_op; } \
     15  if ((stack[i - 3].type == A+1) && (stack[i - 2].type == A+1) && (stack[i - 1].type == A)) { \
     16    status = FUNC (&tmp_stack, &stack[i - 3], &stack[i - 2], &stack[i - 1], stack[i].name); \
     17    goto got_three_op; } \
     18  if ((stack[i - 3].type == A) && (stack[i - 2].type == A) && (stack[i - 1].type == A+1)) { \
     19    status = FUNC (&tmp_stack, &stack[i - 3], &stack[i - 2], &stack[i - 1], stack[i].name); \
     20    goto got_three_op; } \
     21  if ((stack[i - 3].type == A+1) && (stack[i - 2].type == A) && (stack[i - 1].type == A+1)) { \
     22    status = FUNC (&tmp_stack, &stack[i - 3], &stack[i - 2], &stack[i - 1], stack[i].name); \
     23    goto got_three_op; } \
     24  if ((stack[i - 3].type == A) && (stack[i - 2].type == A+1) && (stack[i - 1].type == A+1)) { \
     25    status = FUNC (&tmp_stack, &stack[i - 3], &stack[i - 2], &stack[i - 1], stack[i].name); \
     26    goto got_three_op; } \
     27  if ((stack[i - 3].type == A+1) && (stack[i - 2].type == A+1) && (stack[i - 1].type == A+1)) { \
     28    status = FUNC (&tmp_stack, &stack[i - 3], &stack[i - 2], &stack[i - 1], stack[i].name); \
     29    goto got_three_op; } \
     30
     31// A & B value types all have 2 possible values
     32# define TWO_OP(A,B,FUNC) {                                             \
     33    if ((stack[i - 2].type == A) && (stack[i - 1].type == B)) {         \
     34      status = FUNC (&tmp_stack, &stack[i - 2], &stack[i - 1], stack[i].name); \
     35      goto got_two_op; }                                                \
     36    if ((stack[i - 2].type == A+1) && (stack[i - 1].type == B)) {       \
     37      status = FUNC (&tmp_stack, &stack[i - 2], &stack[i - 1], stack[i].name); \
     38      goto got_two_op; }                                                \
     39    if ((stack[i - 2].type == A) && (stack[i - 1].type == B+1)) {       \
     40      status = FUNC (&tmp_stack, &stack[i - 2], &stack[i - 1], stack[i].name); \
     41      goto got_two_op; }                                                \
     42    if ((stack[i - 2].type == A+1) && (stack[i - 1].type == B+1)) {     \
     43      status = FUNC (&tmp_stack, &stack[i - 2], &stack[i - 1], stack[i].name); \
     44      goto got_two_op; } \
     45  }
     46
     47# define ONE_OP(A,FUNC)                                         \
     48  if (stack[i - 1].type == A) {                                 \
     49    status = FUNC (&tmp_stack, &stack[i - 1], stack[i].name);   \
     50    goto got_one_op; }
    1151
    1252int evaluate_stack (StackVar *stack, int *Nstack) {
     
    2060
    2161  if (*Nstack == 1) {
    22     if ((stack[0].type == 'S') || (stack[0].type == 's')) {
     62    if ((stack[0].type == ST_SCALAR_INT) || (stack[0].type == ST_SCALAR_FLT)) {
    2363      clear_stack (&tmp_stack);
    2464      return (TRUE);
    2565    }
    26     if (stack[0].type == 'V') {
     66    if (stack[0].type == ST_VECTOR) {
    2767      /* need to make a copy so we set output value? */
    2868      V_unary (&tmp_stack, &stack[0], "=");
     
    3070      return (TRUE);
    3171    }
    32     if (stack[0].type == 'M') {
     72    if (stack[0].type == ST_MATRIX) {
    3373      /* need to make a copy so we set output value? */
    3474      M_unary (&tmp_stack, &stack[0], "=");
     
    5696    }
    5797
    58     /***** binary operators *****/
    59     if ((stack[i].type >= 3) && (stack[i].type <= 8)) {
    60 
    61       if (i < 2) {  /* need two variables to operate on */
    62         sprintf (line, "syntax error: binary operator with one operand: %s\n", stack[i].name);
     98    /***** trinary operators *****/
     99    switch (stack[i].type) {
     100      case ST_TRINARY:
     101
     102        if (i < 3) {  /* need two variables to operate on */
     103          snprintf (line, 512, "syntax error: trinary operator without three operands: %s\n", stack[i].name);
     104          push_error (line);
     105          clear_stack (&tmp_stack);
     106          return (FALSE);
     107        }
     108
     109        status = FALSE;
     110        THREE_OP (ST_MATRIX,MMM_trinary);
     111        THREE_OP (ST_VECTOR,VVV_trinary);
     112
     113        THREE_OP (ST_SCALAR_FLT,SSS_trinary);
     114        THREE_OP (ST_SCALAR_INT,SSS_trinary);
     115
     116        /* there are no valid unary string operators */
     117        push_error ("invalid operands for trinary operator (mismatch types?)");
     118        clear_stack (&tmp_stack);
     119        return (FALSE);
     120
     121      got_three_op:
     122        if (!status) {
     123          snprintf (line, 512, "syntax error: invalid operand for binary operation: %s or %s or %s\n", stack[i-1].name, stack[i-2].name, stack[i-3].name);
     124          push_error (line);
     125          clear_stack (&tmp_stack);
     126          return (FALSE);
     127        }
     128        move_stack (&stack[i-3], &tmp_stack);
     129        delete_stack (&stack[i-2], 3);
     130        for (j = i + 1; j < *Nstack; j++) {
     131          move_stack (&stack[j-3], &stack[j]);
     132        }
     133        *Nstack -= 3;
     134        i -= 3;
     135        init_stack (&tmp_stack);
     136        continue;
     137
     138        /***** binary operators *****/
     139      case ST_OR:
     140      case ST_AND:
     141      case ST_LOGIC:
     142      case ST_BITWISE:
     143      case ST_ADD:
     144      case ST_TIMES:
     145      case ST_POWER:
     146      case ST_BINARY:
     147
     148        if (i < 2) {  /* need two variables to operate on */
     149          snprintf (line, 512, "syntax error: binary operator with one operand: %s\n", stack[i].name);
     150          push_error (line);
     151          clear_stack (&tmp_stack);
     152          return (FALSE);
     153        }
     154
     155        status = FALSE;
     156        TWO_OP (ST_MATRIX,ST_MATRIX,MM_binary);
     157        TWO_OP (ST_MATRIX,ST_VECTOR,MV_binary);
     158        TWO_OP (ST_MATRIX,ST_SCALAR_INT,MS_binary);
     159
     160        TWO_OP (ST_VECTOR,ST_MATRIX,VM_binary);
     161        TWO_OP (ST_VECTOR,ST_VECTOR,VV_binary);
     162        TWO_OP (ST_VECTOR,ST_SCALAR_INT,VS_binary);
     163
     164        TWO_OP (ST_SCALAR_INT,ST_MATRIX,SM_binary);
     165        TWO_OP (ST_SCALAR_INT,ST_VECTOR,SV_binary);
     166        TWO_OP (ST_SCALAR_INT,ST_SCALAR_INT,SS_binary);     
     167
     168        TWO_OP (ST_SCALAR_FLT,ST_MATRIX,SM_binary);
     169        TWO_OP (ST_SCALAR_FLT,ST_VECTOR,SV_binary);
     170        TWO_OP (ST_SCALAR_FLT,ST_SCALAR_INT,SS_binary);     
     171
     172        TWO_OP (ST_STRING,ST_STRING,WW_binary);     
     173        TWO_OP (ST_STRING,ST_SCALAR_INT,WW_binary);     
     174        TWO_OP (ST_SCALAR_INT,ST_STRING,WW_binary);     
     175     
     176      got_two_op:
     177        if (!status) {
     178          snprintf (line, 512, "syntax error: invalid operand for binary operation: %s or %s\n", stack[i-1].name, stack[i-2].name);
     179          push_error (line);
     180          clear_stack (&tmp_stack);
     181          return (FALSE);
     182        }
     183        move_stack (&stack[i-2], &tmp_stack);
     184        delete_stack (&stack[i-1], 2);
     185        for (j = i + 1; j < *Nstack; j++) {
     186          move_stack (&stack[j - 2], &stack[j]);
     187        }
     188        *Nstack -= 2;
     189        i -= 2;
     190        init_stack (&tmp_stack);
     191        continue;
     192
     193        /***** unary operators **/
     194      case ST_UNARY:
     195
     196        if (i < 1) {  /* need one variable to operate on */
     197          push_error ("syntax error: unary operator with no operand");
     198          clear_stack (&tmp_stack);
     199          return (FALSE);
     200        }
     201
     202        ONE_OP (ST_MATRIX, M_unary);
     203        ONE_OP (ST_MATRIX_TMP, M_unary);
     204
     205        ONE_OP (ST_VECTOR, V_unary);
     206        ONE_OP (ST_VECTOR_TMP, V_unary);
     207
     208        ONE_OP (ST_SCALAR_INT, S_unary);
     209        ONE_OP (ST_SCALAR_FLT, S_unary);
     210
     211        /* there are no valid unary string operators */
     212        push_error ("syntax error: no valid string unary ops");
     213        clear_stack (&tmp_stack);
     214        return (FALSE);
     215
     216      got_one_op:
     217        move_stack (&stack[i-1], &tmp_stack);
     218        delete_stack (&stack[i], 1);
     219        for (j = i + 1; j < *Nstack; j++) {
     220          move_stack (&stack[j - 1], &stack[j]);
     221        }
     222        init_stack (&tmp_stack);
     223        *Nstack -= 1;
     224        i -= 1;
     225        continue;
     226
     227      case ST_SCALAR_INT:
     228      case ST_SCALAR_FLT:
     229      case ST_VECTOR:
     230      case ST_VECTOR_TMP:
     231      case ST_MATRIX:
     232      case ST_MATRIX_TMP:
     233      case ST_STRING:
     234        continue;
     235
     236      default:
     237        snprintf (line, 512, "syntax error: unexpected operator type %s", stack[i].name);
    63238        push_error (line);
    64239        clear_stack (&tmp_stack);
    65240        return (FALSE);
    66       }
    67 
    68       status = FALSE;
    69       TWO_OP ("M","M",MM_binary);
    70       TWO_OP ("M","V",MV_binary);
    71       TWO_OP ("M","S",MS_binary);
    72       TWO_OP ("V","M",VM_binary);
    73       TWO_OP ("V","V",VV_binary);
    74       TWO_OP ("V","S",VS_binary);
    75       TWO_OP ("S","M",SM_binary);
    76       TWO_OP ("S","V",SV_binary);
    77       TWO_OP ("S","S",SS_binary);     
    78       TWO_OP ("W","W",WW_binary);     
    79       TWO_OP ("W","S",WW_binary);     
    80       TWO_OP ("S","W",WW_binary);     
    81      
    82       if (!status) {
    83         sprintf (line, "syntax error: invalid operand for binary operation: %s or %s\n", stack[i-1].name, stack[i-2].name);
    84         push_error (line);
    85         clear_stack (&tmp_stack);
    86         return (FALSE);
    87       }
    88       move_stack (&stack[i-2], &tmp_stack);
    89       delete_stack (&stack[i-1], 2);
    90       for (j = i + 1; j < *Nstack; j++) {
    91         move_stack (&stack[j - 2], &stack[j]);
    92       }
    93       *Nstack -= 2;
    94       i -= 2;
    95       init_stack (&tmp_stack);
    96       continue;
    97     }
    98 
    99     /***** unary operators **/
    100     if (stack[i].type == 9) {
    101 
    102       if (i < 1) {  /* need one variable to operate on */
    103         push_error ("syntax error: unary operator with no operand");
    104         clear_stack (&tmp_stack);
    105         return (FALSE);
    106       }
    107 
    108       ONE_OP ("M", M_unary);
    109       ONE_OP ("V", V_unary);
    110       ONE_OP ("S", S_unary);
    111 
    112       /* there are no valid unary string operators */
    113       if (!strncasecmp (&stack[i - 1].type, "W", 1)) {
    114         push_error ("syntax error: no valid string unary ops");
    115         clear_stack (&tmp_stack);
    116         return (FALSE);
    117       }
    118 
    119       move_stack (&stack[i-1], &tmp_stack);
    120       delete_stack (&stack[i], 1);
    121       for (j = i + 1; j < *Nstack; j++) {
    122         move_stack (&stack[j - 1], &stack[j]);
    123       }
    124       init_stack (&tmp_stack);
    125       *Nstack -= 1;
    126       i -= 1;
    127       continue;
    128     }
     241    }
    129242  }
    130243  clear_stack (&tmp_stack);
     
    160273
    161274  for (i = 0; i < Nstack; i++) {
    162     if (IsBufferPtr (stack[i].buffer) && (stack[i].type == 'm')) {
     275    if (IsBufferPtr (stack[i].buffer) && (stack[i].type == ST_MATRIX_TMP)) {
    163276      if (VERBOSE) gprint (GP_ERR, "free %s (buff) (%lx)\n", stack[i].name, (long) stack[i].buffer);
    164277      free (stack[i].buffer[0].header.buffer);
     
    167280      stack[i].buffer = NULL;
    168281    }   
    169     if (IsVectorPtr (stack[i].vector) && (stack[i].type == 'v')) {
     282    if (IsVectorPtr (stack[i].vector) && (stack[i].type == ST_VECTOR_TMP)) {
    170283      if (VERBOSE) gprint (GP_ERR, "free %s (vect) (%lx)\n", stack[i].name, (long) stack[i].vector);
    171284      free (stack[i].vector[0].elements.Ptr);
     
    193306}
    194307
    195 void assign_stack (StackVar *stack, char *name, int type) {
     308void assign_stack (StackVar *stack, char *name, StackVarType type) {
    196309  stack->name = strcreate (name);
    197310  stack->type = type;
Note: See TracChangeset for help on using the changeset viewer.