IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 42080


Ignore:
Timestamp:
Feb 28, 2022, 12:14:19 PM (4 years ago)
Author:
eugene
Message:

add string unary functions (isword, isnum, isint, isflt, length)

Location:
trunk/Ohana/src/opihi/lib.shell
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/opihi/lib.shell/VariableOps.c

    r39558 r42080  
    11# include "opihi.h"
     2# define USE_DEPTH 1
    23
    34Variable *variables;   /* variable to store the list of all variables */
     
    2728
    2829  int i;
    29   char *local, *MacroName;
    30 
    31   /* a local variable has the form (macroname).(varname) */
    32   MacroName = GetMacroName ();
     30  char *local;
     31
     32  /* a local variable has the form (macroname).(depth).(varname) */
     33  char *MacroName = GetMacroName ();
     34  int   MacroDepth = GetMacroDepth ();
    3335
    3436  /* need to use a mutex to prevent two threads from changing variable list simultaneously */
     
    3638
    3739  /* look for existing local variable */
    38   ALLOCATE (local, char, strlen(name) + strlen(MacroName) + 2);
    39   sprintf (local, "%s.%s", MacroName, name);
     40  int maxLen = strlen(name) + strlen(MacroName) + 16;
     41  ALLOCATE (local, char, maxLen);
     42  if (USE_DEPTH) {
     43    snprintf (local, maxLen, "%s.%d.%s", MacroName, MacroDepth, name);
     44  } else {
     45    snprintf (local, maxLen, "%s.%s", MacroName, name);
     46  }
    4047  for (i = 0; i < Nvariables; i++) {
    4148    if (!strcmp (local, variables[i].name)) {
     
    5966
    6067  int i;
    61   char *local, *MacroName;
    62 
    63   MacroName = GetMacroName ();
     68  char *local;
     69
     70  char *MacroName = GetMacroName ();
     71  int   MacroDepth = GetMacroDepth ();
    6472
    6573  /* need to use a mutex to prevent two threads from changing variable list simultaneously */
     
    6775
    6876  /* look for local variable first */
    69   ALLOCATE (local, char, strlen(name) + strlen(MacroName) + 2);
    70   sprintf (local, "%s.%s", MacroName, name);
     77  int maxLen = strlen(name) + strlen(MacroName) + 16;
     78  ALLOCATE (local, char, maxLen);
     79  if (USE_DEPTH) {
     80    snprintf (local, maxLen, "%s.%d.%s", MacroName, MacroDepth, name);
     81  } else {
     82    snprintf (local, maxLen, "%s.%s", MacroName, name);
     83  }
    7184  for (i = 0; i < Nvariables; i++) {
    7285    if (!strcmp (local, variables[i].name)) {
     
    122135 
    123136  int i;
    124   char *local, *MacroName;
    125 
    126   MacroName = GetMacroName ();
     137  char *local;
     138
     139  char *MacroName = GetMacroName ();
     140  int   MacroDepth = GetMacroDepth ();
    127141
    128142  /* look for local variable first */
    129   ALLOCATE (local, char, strlen(name) + strlen(MacroName) + 2);
    130   sprintf (local, "%s.%s", MacroName, name);
     143  int maxLen = strlen(name) + strlen(MacroName) + 16;
     144  ALLOCATE (local, char, maxLen);
     145  if (USE_DEPTH) {
     146    snprintf (local, maxLen, "%s.%d.%s", MacroName, MacroDepth, name);
     147  } else {
     148    snprintf (local, maxLen, "%s.%s", MacroName, name);
     149  }
    131150
    132151  /* need to use a mutex to prevent another thread from misleading on size of Nvariables */
  • trunk/Ohana/src/opihi/lib.shell/VectorOps.c

    r41959 r42080  
    103103    if (mode == OLDVECTOR) goto error;
    104104    // validate vector name syntax
    105 //    vector with ":" will be failed so I comment it out CCL
    106 //    if (!IsVectorNameValid(name)) { gprint (GP_ERR, "invalid vector name %s\n", name); return NULL; }
     105    if (!IsVectorNameValid(name)) { gprint (GP_ERR, "invalid vector name %s\n", name); return NULL; }
    107106    pthread_mutex_lock (&mutex);
    108107    Nvectors ++;
  • trunk/Ohana/src/opihi/lib.shell/convert_to_RPN.c

    r41341 r42080  
    7171    if (!strcmp (argv[i], "isnan"))  { type = ST_UNARY; goto gotit; }
    7272
     73    /* string-valid unary operations */
     74    if (!strcmp (argv[i], "isword")) { type = ST_UNARY; goto gotit; }
     75    if (!strcmp (argv[i], "isnum"))  { type = ST_UNARY; goto gotit; }
     76    if (!strcmp (argv[i], "isint"))  { type = ST_UNARY; goto gotit; }
     77    if (!strcmp (argv[i], "isflt"))  { type = ST_UNARY; goto gotit; }
     78    if (!strcmp (argv[i], "length")) { type = ST_UNARY; goto gotit; }
     79
    7380    /* binary operations */
     81    // NOTE: I archically use the first character of the function name in a switch to identify the operation
     82    // I should re-work this with an enum which defines the operatino
    7483    if (!strcmp (argv[i], "^"))      { type = ST_POWER; goto gotit; }
    7584
  • trunk/Ohana/src/opihi/lib.shell/evaluate_stack.c

    r41363 r42080  
    124124        THREE_OP (ST_SCALAR_INT,SSS_trinary);
    125125
    126         /* there are no valid unary string operators */
     126        /* there are no valid trinary string operators */
    127127        snprintf (line, 512, "invalid operands for trinary operator %s (mismatch types?)\n(Note that the : in a trinary operation must be protected by spaces)", stack[i].name);
    128128        push_error (line);
     
    260260        ONE_OP (ST_SCALAR_FLT, S_unary);
    261261
    262         /* there are no valid unary string operators */
     262        ONE_OP (ST_STRING, W_unary);
     263
    263264        snprintf (line, 512, "invalid operand for unary operator %s (undefined value?)", stack[i].name);
    264265        push_error (line);
  • trunk/Ohana/src/opihi/lib.shell/expand_vectors.c

    r41959 r42080  
    135135    // include/shell.h.  A vector name cannot include a colon, while a variable name
    136136    // cannot include a dot.
    137     // temporary remove ISVEC check until ":" issue fixed CCL
    138     //for (w = p - 1; (w >= line) && !OHANA_WHITESPACE(*w) && ISVEC(*w); w--);
    139     for (w = p - 1; (w >= line) && !OHANA_WHITESPACE(*w); w--);
     137
     138    for (w = p - 1; (w >= line) && !OHANA_WHITESPACE(*w) && ISVEC(*w); w--);
    140139    w ++;
    141140    n = (int)(p - w);
  • trunk/Ohana/src/opihi/lib.shell/stack_math.c

    r41363 r42080  
    15001500    if ((FTYPE == ST_SCALAR_INT) && (V1->type == ST_SCALAR_INT)) {      \
    15011501      opihi_int M1  = V1[0].IntValue;                                   \
     1502      OUT[0].type = ST_SCALAR_INT;                                      \
     1503      OUT[0].IntValue = OP;                                             \
     1504      clear_stack (V1);                                                 \
     1505      return (TRUE);                                                    \
     1506    }                                                                   \
     1507  }
     1508
     1509# define W_FUNC(OP,FTYPE) {                                             \
     1510    if (V1->type == ST_SCALAR_FLT) {                                    \
     1511      OUT[0].type = ST_SCALAR_FLT;                                      \
     1512      OUT[0].FltValue = OP;                                             \
     1513      clear_stack (V1);                                                 \
     1514      return (TRUE);                                                    \
     1515    }                                                                   \
     1516    if ((FTYPE == ST_SCALAR_FLT) && (V1->type == ST_SCALAR_INT)) {      \
     1517      OUT[0].type = ST_SCALAR_FLT;                                      \
     1518      OUT[0].FltValue = OP;                                             \
     1519      clear_stack (V1);                                                 \
     1520      return (TRUE);                                                    \
     1521    }                                                                   \
     1522    if ((FTYPE == ST_SCALAR_INT) && (V1->type == ST_SCALAR_INT)) {      \
    15021523      OUT[0].type = ST_SCALAR_INT;                                      \
    15031524      OUT[0].IntValue = OP;                                             \
     
    15361557  if (!strcmp (op, "datan"))  S_FUNC(atan (M1)*DEG_RAD, ST_SCALAR_FLT);
    15371558  if (!strcmp (op, "lgamma")) S_FUNC(lgamma (M1), ST_SCALAR_FLT);
    1538   if (!strcmp (op, "rnd"))    S_FUNC(0.0*M1 + drand48(), ST_SCALAR_FLT);
    1539   if (!strcmp (op, "drnd"))   S_FUNC(0.0*M1 + drand48(), ST_SCALAR_FLT);
    1540   if (!strcmp (op, "lrnd"))   S_FUNC(0*M1 + lrand48(), ST_SCALAR_INT);
    1541   if (!strcmp (op, "mrnd"))   S_FUNC(0*M1 + mrand48(), ST_SCALAR_INT);
    15421559  if (!strcmp (op, "not"))    S_FUNC(!(M1), ST_SCALAR_INT);
    15431560  if (!strcmp (op, "--"))     S_FUNC(-1*M1, ST_SCALAR_INT); // NOTE: opihi_int is signed,
     
    15451562  if (!strcmp (op, "isnan"))  S_FUNC(isnan((opihi_flt)(M1)), ST_SCALAR_FLT); // XXX modify in future   
    15461563
     1564  // these ops do not use the V1 value (W_FUNC does define a temp variable M1)
     1565  if (!strcmp (op, "rnd"))    W_FUNC(drand48(), ST_SCALAR_FLT);
     1566  if (!strcmp (op, "drnd"))   W_FUNC(drand48(), ST_SCALAR_FLT);
     1567  if (!strcmp (op, "lrnd"))   W_FUNC(lrand48(), ST_SCALAR_INT);
     1568  if (!strcmp (op, "mrnd"))   W_FUNC(mrand48(), ST_SCALAR_INT);
     1569
     1570  // these are also valid for string values
     1571  if (!strcmp (op, "isword")) W_FUNC(FALSE, ST_SCALAR_INT);
     1572  if (!strcmp (op, "isnum"))  W_FUNC(TRUE,  ST_SCALAR_INT);
     1573  if (!strcmp (op, "isint"))  W_FUNC((V1->type == ST_SCALAR_INT), ST_SCALAR_INT);
     1574  if (!strcmp (op, "isflt"))  W_FUNC((V1->type == ST_SCALAR_FLT), ST_SCALAR_INT);
     1575
    15471576# undef S_FUNC
    1548 
    1549   clear_stack (V1);
    1550 
    1551   char line[512]; // this is only used to report an error
    1552   snprintf (line, 512, "error: op %s not defined as unary scalar op!", op);
     1577# undef W_FUNC
     1578
     1579  clear_stack (V1);
     1580
     1581  char line[512]; // this is only used to report an error
     1582  snprintf (line, 512, "error: op %s not defined as unary scalar op for a numerical value!", op);
     1583  push_error (line);
     1584  return (FALSE);
     1585}
     1586
     1587int W_unary (StackVar *OUT, StackVar *V1, char *op) {
     1588
     1589# define W_FUNC(VALUE) {                                                \
     1590    OUT[0].type = ST_SCALAR_INT;                                        \
     1591    OUT[0].IntValue = VALUE;                                            \
     1592    clear_stack (V1);                                                   \
     1593    return (TRUE);                                                      \
     1594  }
     1595
     1596  // string unary functions (all result in int output values)
     1597  if (!strcmp (op, "isword")) W_FUNC(TRUE);
     1598  if (!strcmp (op, "isnum"))  W_FUNC(FALSE);
     1599  if (!strcmp (op, "isint"))  W_FUNC(FALSE);
     1600  if (!strcmp (op, "isflt"))  W_FUNC(FALSE);
     1601  if (!strcmp (op, "length")) W_FUNC(strlen(V1[0].name));
     1602
     1603# undef W_FUNC
     1604
     1605  clear_stack (V1);
     1606
     1607  char line[512]; // this is only used to report an error
     1608  snprintf (line, 512, "error: op %s not defined as unary scalar op for a string value!", op);
    15531609  push_error (line);
    15541610  return (FALSE);
Note: See TracChangeset for help on using the changeset viewer.