home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sherlock.zip / EXPR.C < prev    next >
C/C++ Source or Header  |  1994-06-29  |  25KB  |  1,074 lines

  1. /*
  2. **  Sherlock - Copyright 1992, 1993, 1994
  3. **    Harfmann Software
  4. **    Compuserve: 73147,213
  5. **    All rights reserved
  6. */
  7. /*
  8. **  Analyize an expression to find a variable in memory.
  9. */
  10. #include    <stdio.h>
  11. #include    <stdlib.h>
  12. #include    <limits.h>
  13. #include    <string.h>
  14. #include    <malloc.h>
  15. #include    <ctype.h>
  16. #include    <math.h>
  17. #include    <sys\stat.h>
  18.  
  19. #define     INCL_DOSPROCESS
  20. #include    <os2.h>
  21. #include    "debug.h"
  22.  
  23. #include    "Debugger.h"
  24. #include    "SrcInter.h"
  25. #include    "Source.h"
  26.  
  27. /*
  28. ** Manifest constants.
  29. */
  30. #define TRUE    1
  31. #define FALSE    0
  32.  
  33. /*
  34. ** Macros for common operations.
  35. */
  36. #define freeData(state)             \
  37.     {    if(state.value.typeValue == STR_VAL)    \
  38.         free(state.value.val.sVal);         \
  39.     if(state.value.typeValue == NAME_VAL)    \
  40.         free(state.value.val.sVal);         \
  41.     state.value.val.sVal = NULL;            \
  42.     if(state.typeData != NULL)        \
  43.         free(state.typeData);        \
  44.     state.typeData = NULL;            \
  45.     }
  46.  
  47. #define copyState(state, state2)         \
  48.     {    memcpy(&state2, state, sizeof(State));     \
  49.     memset(&state2.value, 0, sizeof(Value)); \
  50.     state2.isStruct = FALSE;         \
  51.     state2.typeData = NULL;          \
  52.     }
  53.  
  54.  
  55.  
  56. static char *curPtr;
  57.  
  58. static int match(char *matchString);
  59. static char *isName(void);
  60. static int isNumber(State *state);
  61. static int isLiteral(State *state);
  62.  
  63. static int exp1(State *state);
  64. static int exp2(State *state);
  65. static int exp3(State *state);
  66. static int exp4(State *state);
  67. static int exp5(State *state);
  68. static int exp6(State *state);
  69. static int primary(State *state);
  70. static int member(State *state);
  71.  
  72. /*
  73. ** Concatinate the rest of the strings since they will become
  74. ** the expression.
  75. */
  76. int evaluate(char *expr, DebugModule *module, ULONG eip, ULONG ebp, Value *value)
  77. {
  78. int    rVal;
  79. State    state;
  80.  
  81.     /*
  82.     ** Initialize the state block.
  83.     */
  84.     memset(&state, 0, sizeof(state));
  85.     state.baseEIP = eip;
  86.     state.baseEBP = ebp;
  87.     debugBuffer.Addr = eip;
  88.     if(module == NULL) {
  89.     DispatchCommand(DBG_C_AddrToObject);
  90.     state.module = FindModule(debugBuffer.MTE, NULL);
  91.     } else {
  92.     state.module = module;
  93.     }
  94.     curPtr = expr;
  95.  
  96.     /*
  97.     ** Evaluate the expression.
  98.     */
  99.     if((rVal = exp1(&state)) != SUCCESS) {
  100.     freeData(state);
  101.     return rVal;
  102.     }
  103.  
  104.     /*
  105.     ** If this is a structure, build a structure value array.
  106.     */
  107.     if((state.value.typeValue == PTR_VAL) && state.isStruct) {
  108.     int         i, numMembers;
  109.     State         state2, state3;
  110.     StructValue *valueData;
  111.     void        *newTD;
  112.  
  113.     /*
  114.     ** Make a copy of the state and the type data for later
  115.     ** use.  GetName destroys the type data, so we need to
  116.     ** make a copy of it for reuse later.
  117.     */
  118.     value->typeValue = STRUCT_VAL;
  119.     value->val.strVal = valueData = calloc(sizeof(StructValue), 1);
  120.     valueData->str = malloc(strlen(expr) + 20);
  121.     sprintf(valueData->str, "%s: (0x%08x)", expr, state.value.val.lVal);
  122.     numMembers = GetNumMembers(state.module, &state);
  123.     newTD = malloc(state.typeDataSize);
  124.  
  125.     /*
  126.     ** Iterate through each member of the array.
  127.     */
  128.     for(i=0; i<numMembers; i++) {
  129.         int     size;
  130.         char    name[MAX_FUNCNAME];
  131.  
  132.         /*
  133.         ** Copy the state so we can traverse the list of members.
  134.         */
  135.         memcpy(&state2, &state, sizeof(State));
  136.         state2.typeData = newTD;
  137.         memcpy(state2.typeData, state.typeData, state.typeDataSize);
  138.         memcpy(&state3, &state, sizeof(State));
  139.         state3.value.typeValue = NAME_VAL;
  140.         state3.value.val.sVal  = name;
  141.         state3.typeData       = NULL;
  142.  
  143.         /*
  144.         ** Find out what variable is at the current index.
  145.         */
  146.         if((rVal = GetMemberIndex(state.module, &state2, i, name)) != SUCCESS) {
  147.         valueData->next = calloc(sizeof(StructValue), 1);
  148.         valueData = valueData->next;
  149.         valueData->str = malloc(strlen(name) + 40);
  150.         sprintf(valueData->str, "%s: Member error! %d", name, rVal);
  151.         continue;
  152.         }
  153.  
  154.         /*
  155.         ** Get the data for the name specified
  156.         */
  157.         if((rVal = GetName(state.module, &state2, &state3)) != SUCCESS) {
  158.         valueData->next = calloc(sizeof(StructValue), 1);
  159.         valueData = valueData->next;
  160.         valueData->str    = malloc(strlen(name) + 40);
  161.         sprintf(valueData->str, "%s: ERROR! %d 0x%08x",
  162.             name, state2.value.typeValue, state2.value.val.lVal);
  163.         continue;
  164.         }
  165.  
  166.         /*
  167.         ** Put the data into the return linked list.
  168.         */
  169.         size = strlen(name) +
  170.            ((state2.value.typeValue == STR_VAL) ?
  171.             (strlen(state2.value.val.sVal) + 20): 20);
  172.         valueData->next = calloc(sizeof(StructValue), 1);
  173.         valueData = valueData->next;
  174.         valueData->str  = malloc(size);
  175.         switch(state2.value.typeValue) {
  176.         case UNKNOWN_VAL:
  177.             sprintf(valueData->str, "%s: UNKNOWN!", name);
  178.             break;
  179.         case LONG_VAL:
  180.             sprintf(valueData->str, "%s: %d (0x%08x)", name,
  181.                 state2.value.val.lVal, state2.value.val.lVal);
  182.             break;
  183.         case DOUBLE_VAL:
  184.             sprintf(valueData->str, "%s: %lf", name,
  185.                 state2.value.val.dVal);
  186.             break;
  187.         case CHAR_VAL:
  188.             sprintf(valueData->str, "%s: '%c' (0x%02x)", name,
  189.                 state2.value.val.cVal, state2.value.val.cVal);
  190.             break;
  191.         case STR_VAL:
  192.             sprintf(valueData->str, "%s: (0x%08x) - \"%s\"", name,
  193.                 state2.value.val.sVal, state2.value.val.sVal);
  194.             break;
  195.         case NAME_VAL:
  196.             sprintf(valueData->str, "%s: NAME: \"%s\"", name,
  197.                 state2.value.val.sVal);
  198.             break;
  199.         case PTR_VAL:
  200.             sprintf(valueData->str, "%s: PTR 0x%08x", name,
  201.                 state2.value.val.lVal);
  202.             break;
  203.         default:
  204.             sprintf(valueData->str, "%s: INTERNAL ERROR!  Type: %d", name,
  205.                 state2.value.typeValue);
  206.             break;
  207.         }
  208.     }
  209.     return SUCCESS;
  210.     }
  211.  
  212.     /*
  213.     ** Copy the value to whoever wanted the evaluation.
  214.     */
  215.     memcpy(value, &state.value, sizeof(Value));
  216.     return SUCCESS;
  217. }
  218.  
  219. /*
  220. ** Is the current token a literal?
  221. ** If so, return true, else return false;
  222. */
  223. static int isLiteral(State *state)
  224. {
  225.     /*
  226.     ** Skip over white space.
  227.     */
  228.     while(isspace(*curPtr))
  229.     curPtr++;
  230.  
  231.     /*
  232.     ** Initialize the structure.
  233.     */
  234.     memset(&state->value, 0, sizeof(Value));
  235.  
  236.     if(isNumber(state))
  237.     return TRUE;
  238.  
  239.     /*
  240.     ** Is this a quote delimited string?
  241.     */
  242.     if(match("\"")) {
  243.         int i;
  244.         char *buff;
  245.     buff = malloc(strlen(curPtr) + 1);
  246.         for(i=0; *curPtr != '"' && *curPtr; i++, curPtr++) {
  247.             buff[i] = *curPtr;
  248.     }
  249.     curPtr++;
  250.         buff[i] = 0;
  251.     state->value.typeValue = STR_VAL;
  252.     state->value.val.sVal = strdup(buff);
  253.     return TRUE;
  254.     }
  255.  
  256.     /*
  257.     ** No escape sequences yet!
  258.     */
  259.     if(match("'")) {
  260.     state->value.val.cVal       = *curPtr++;
  261.     state->value.typeValue = CHAR_VAL;
  262.     if(state->value.val.cVal == '\\') {
  263.             switch(*curPtr) {
  264.         case '\\':  state->value.val.cVal = '\\'; ++curPtr; break;
  265.         case 'n':   state->value.val.cVal = '\n'; ++curPtr; break;
  266.         case 't':   state->value.val.cVal = '\t'; ++curPtr; break;
  267.         case 'v':   state->value.val.cVal = '\v'; ++curPtr; break;
  268.         case 'b':   state->value.val.cVal = '\b'; ++curPtr; break;
  269.         case 'r':   state->value.val.cVal = '\r'; ++curPtr; break;
  270.         case 'f':   state->value.val.cVal = '\f'; ++curPtr; break;
  271.                 case '0':
  272.                 case '1':
  273.                 case '2':
  274.                 case '3':
  275.                 case '4':
  276.                 case '5':
  277.                 case '6':
  278.         case '7':   state->value.val.cVal = (char) strtol(curPtr, &curPtr, 8);
  279.                             break;
  280.                 case 'x':
  281.         case 'X':   state->value.val.cVal = (char) strtol(curPtr + 1, &curPtr, 16);
  282.                             break;
  283.         default:    return FALSE;
  284.             }
  285.         }
  286.     curPtr++;
  287.     return TRUE;
  288.     }
  289.     return FALSE;
  290. }
  291.  
  292. /*
  293. ** Is the current token a number?
  294. ** Return true if it is, false if not.
  295. */
  296. static int isNumber(State *state)
  297. {
  298. int    isFloat = 0;
  299. int    i;
  300. char   *dummy;
  301. char    buffer[30];
  302.  
  303.     if(!isdigit(*curPtr))
  304.     return FALSE;
  305.  
  306.     buffer[0] = 0;
  307.     for(i=0; i<sizeof(buffer); i++, curPtr++) {
  308.         if(isdigit(*curPtr)) {
  309.         buffer[i] = *curPtr;
  310.         continue;
  311.     }
  312.     if((*curPtr == '.') || (*curPtr == 'e') || (*curPtr == 'E')) {
  313.         buffer[i] = *curPtr;
  314.         isFloat = 1;
  315.         continue;
  316.     }
  317.     buffer[i] = 0;
  318.     break;
  319.     }
  320.     if(isFloat) {
  321.     state->value.val.dVal       = strtod(buffer, &dummy);
  322.     state->value.typeValue = DOUBLE_VAL;
  323.     } else {
  324.     state->value.val.lVal = strtol(buffer, &dummy, 0);
  325.     if(state->value.val.lVal == LONG_MAX || state->value.val.lVal == LONG_MIN)
  326.         state->value.val.lVal = strtoul(buffer, &dummy, 0);
  327.     state->value.typeValue = LONG_VAL;
  328.     }
  329.     return TRUE;
  330. }
  331.  
  332. /*
  333. ** Is this a register?
  334. */
  335. static int isRegister(State *state)
  336. {
  337. ULONG  *offset = NULL;
  338. int    shift  = 0;
  339. int    length;
  340.  
  341.     /*
  342.     ** 32 Bit registers.
  343.     */
  344.     if(stricmp(curPtr, "EAX") == 0) {
  345.     offset = &debugBuffer.EAX;
  346.     length = 4;
  347.     } else if(stricmp(curPtr, "EBX") == 0) {
  348.     offset = &debugBuffer.EBX;
  349.     length = 4;
  350.     } else if(stricmp(curPtr, "ECX") == 0) {
  351.     offset = &debugBuffer.ECX;
  352.     length = 4;
  353.     } else if(stricmp(curPtr, "EDX") == 0) {
  354.     offset = &debugBuffer.EDX;
  355.     length = 4;
  356.     } else if(stricmp(curPtr, "ESP") == 0) {
  357.     offset = &debugBuffer.ESP;
  358.     length = 4;
  359.     } else if(stricmp(curPtr, "EBP") == 0) {
  360.     offset = &debugBuffer.EBP;
  361.     length = 4;
  362.     } else if(stricmp(curPtr, "ESI") == 0) {
  363.     offset = &debugBuffer.ESI;
  364.     length = 4;
  365.     } else if(stricmp(curPtr, "EDI") == 0) {
  366.     offset = &debugBuffer.EDI;
  367.     length = 4;
  368.     } else if(stricmp(curPtr, "EIP") == 0) {
  369.     offset = &debugBuffer.EIP;
  370.     length = 4;
  371.     } else
  372.  
  373.     /*
  374.     ** 16 Bit registers.
  375.     */
  376.     if(stricmp(curPtr, "AX") == 0) {
  377.     offset = &debugBuffer.EAX;
  378.     length = 2;
  379.     } else if(stricmp(curPtr, "BX") == 0) {
  380.     offset = &debugBuffer.EBX;
  381.     length = 2;
  382.     } else if(stricmp(curPtr, "CX") == 0) {
  383.     offset = &debugBuffer.ECX;
  384.     length = 2;
  385.     } else if(stricmp(curPtr, "DX") == 0) {
  386.     offset = &debugBuffer.EDX;
  387.     length = 2;
  388.     } else if(stricmp(curPtr, "SP") == 0) {
  389.     offset = &debugBuffer.ESP;
  390.     length = 2;
  391.     } else if(stricmp(curPtr, "BP") == 0) {
  392.     offset = &debugBuffer.EBP;
  393.     length = 2;
  394.     } else if(stricmp(curPtr, "SI") == 0) {
  395.     offset = &debugBuffer.ESI;
  396.     length = 2;
  397.     } else if(stricmp(curPtr, "DI") == 0) {
  398.     offset = &debugBuffer.EDI;
  399.     length = 2;
  400.     } else if(stricmp(curPtr, "IP") == 0) {
  401.     offset = &debugBuffer.EIP;
  402.     length = 2;
  403.     } else
  404.  
  405.     /*
  406.     ** 8 Bit registers.
  407.     */
  408.     if(stricmp(curPtr, "AL") == 0) {
  409.     offset = &debugBuffer.EAX;
  410.     length = 1;
  411.     } else if(stricmp(curPtr, "AH") == 0) {
  412.     offset = &debugBuffer.EAX;
  413.     length = 1;
  414.     shift  = 8;
  415.     } else if(stricmp(curPtr, "BL") == 0) {
  416.     offset = &debugBuffer.EBX;
  417.     length = 1;
  418.     } else if(stricmp(curPtr, "BH") == 0) {
  419.     offset = &debugBuffer.EBX;
  420.     length = 1;
  421.     shift  = 8;
  422.     } else if(stricmp(curPtr, "CL") == 0) {
  423.     offset = &debugBuffer.ECX;
  424.     length = 1;
  425.     } else if(stricmp(curPtr, "CH") == 0) {
  426.     offset = &debugBuffer.ECX;
  427.     length = 1;
  428.     shift  = 8;
  429.     } else if(stricmp(curPtr, "DL") == 0) {
  430.     offset = &debugBuffer.EDX;
  431.     length = 1;
  432.     } else if(stricmp(curPtr, "DH") == 0) {
  433.     offset = &debugBuffer.EDX;
  434.     length = 1;
  435.     shift  = 8;
  436.     } else
  437.  
  438.     /*
  439.     ** Segment registers.
  440.     */
  441.     if(stricmp(curPtr, "CS") == 0) {
  442.     offset = (ULONG *) &debugBuffer.CS;
  443.     length = 2;
  444.     } else if(stricmp(curPtr, "DS") == 0) {
  445.     offset = (ULONG *) &debugBuffer.DS;
  446.     length = 2;
  447.     } else if(stricmp(curPtr, "ES") == 0) {
  448.     offset = (ULONG *) &debugBuffer.ES;
  449.     length = 2;
  450.     } else if(stricmp(curPtr, "SS") == 0) {
  451.     offset = (ULONG *) &debugBuffer.SS;
  452.     length = 2;
  453.     } else if(stricmp(curPtr, "FS") == 0) {
  454.     offset = (ULONG *) &debugBuffer.FS;
  455.     length = 2;
  456.     } else if(stricmp(curPtr, "GS") == 0) {
  457.     offset = (ULONG *) &debugBuffer.GS;
  458.     length = 2;
  459.     }
  460.  
  461.     /*
  462.     ** See if we matched a register.
  463.     */
  464.     if(offset == NULL)
  465.     return FALSE;
  466.  
  467.     /*
  468.     ** Get the registers
  469.     */
  470.     DispatchCommand(DBG_C_ReadReg);
  471.     if(length == 1) {
  472.     state->value.val.lVal = (*offset >> shift) & 0xff;
  473.     } else if(length == 2) {
  474.     state->value.val.lVal = *offset & 0xffff;
  475.     } else {
  476.     state->value.val.lVal = *offset;
  477.     }
  478.     state->value.typeValue = LONG_VAL;
  479.     return TRUE;
  480. }
  481.  
  482. /*
  483. ** Is the current token a name?
  484. ** Return NULL if not, else a duplicate if so.
  485. */
  486. static char *isName(void)
  487. {
  488. int    i;
  489. char    buffer[MAX_FUNCNAME];
  490.  
  491.     if((*curPtr != '_') && !isalpha(*curPtr))
  492.     return NULL;
  493.  
  494.     for(i=0; i<sizeof(buffer); i++, curPtr++) {
  495.     if((*curPtr == '_') || isalnum(*curPtr)) {
  496.         buffer[i] = *curPtr;
  497.         continue;
  498.     }
  499.     buffer[i] = 0;
  500.     break;
  501.     }
  502.     return strdup(buffer);
  503. }
  504.  
  505. /*
  506. ** Answer whether the specified string matches the next token.
  507. ** If it does, return true.  If not, return false.
  508. */
  509. static int match(char *matchString)
  510. {
  511.     /*
  512.     ** Skip over white space.
  513.     */
  514.     while(isspace(*curPtr))
  515.     curPtr++;
  516.  
  517.     /*
  518.     ** Compare the match string to the current text.
  519.     */
  520.     if(strncmp(curPtr, matchString, strlen(matchString)) == 0) {
  521.     curPtr += strlen(matchString);
  522.     return TRUE;
  523.     }
  524.     return FALSE;
  525. }
  526.  
  527. /*
  528. ** Handle logical operations.
  529. */
  530. static int exp1(State *state)
  531. {
  532. int    rVal;
  533. State    state2;
  534.  
  535.     if((rVal = exp2(state)) != SUCCESS)
  536.     return rVal;
  537.  
  538.     /*
  539.     ** Initialize the state variable.
  540.     */
  541.     copyState(state, state2);
  542.  
  543.     while(TRUE) {
  544.  
  545.     /*
  546.     ** Logical or.
  547.     */
  548.     if(match("|")) {
  549.         if(state->value.typeValue != LONG_VAL)
  550.         return INVALID_VALUE;
  551.  
  552.         if((rVal = exp2(&state2)) != SUCCESS) {
  553.         freeData(state2);
  554.         return rVal;
  555.         }
  556.         if(state2.value.typeValue != LONG_VAL) {
  557.         freeData(state2);
  558.         return INVALID_VALUE;
  559.         }
  560.  
  561.         state->value.val.lVal = state->value.val.lVal | state2.value.val.lVal;
  562.         freeData(state2);
  563.         continue;
  564.     }
  565.  
  566.     /*
  567.     ** Logical xor.
  568.     */
  569.     if(match("^")) {
  570.         if(state->value.typeValue != LONG_VAL)
  571.         return INVALID_VALUE;
  572.  
  573.         if((rVal = exp2(&state2)) != SUCCESS) {
  574.         freeData(state2);
  575.         return rVal;
  576.         }
  577.         if(state2.value.typeValue != LONG_VAL) {
  578.         freeData(state2);
  579.         return INVALID_VALUE;
  580.         }
  581.  
  582.         state->value.val.lVal = state->value.val.lVal ^ state2.value.val.lVal;
  583.         freeData(state2);
  584.         continue;
  585.     }
  586.  
  587.     /*
  588.     ** Logical and.
  589.     */
  590.     if(match("&")) {
  591.         if(state->value.typeValue != LONG_VAL)
  592.         return INVALID_VALUE;
  593.  
  594.         if((rVal = exp2(&state2)) != SUCCESS) {
  595.         freeData(state2);
  596.         return rVal;
  597.         }
  598.         if(state2.value.typeValue != LONG_VAL) {
  599.         freeData(state2);
  600.         return INVALID_VALUE;
  601.         }
  602.  
  603.         state->value.val.lVal = state->value.val.lVal & state2.value.val.lVal;
  604.         freeData(state2);
  605.         continue;
  606.     }
  607.     return SUCCESS;
  608.     }
  609. }
  610.  
  611. /*
  612. ** Handle the shift operations.
  613. */
  614. static int exp2(State *state)
  615. {
  616. int    rVal;
  617. State    state2;
  618.  
  619.     if((rVal = exp3(state)) != SUCCESS)
  620.     return rVal;
  621.  
  622.     /*
  623.     ** Initialize the state variable.
  624.     */
  625.     copyState(state, state2);
  626.  
  627.     while(TRUE) {
  628.     /*
  629.     ** Shift left.
  630.     */
  631.     if(match("<<")) {
  632.         if(state->value.typeValue != LONG_VAL)
  633.         return INVALID_VALUE;
  634.  
  635.         if((rVal = exp3(&state2)) != SUCCESS) {
  636.         freeData(state2);
  637.         return rVal;
  638.         }
  639.         if(state2.value.typeValue != LONG_VAL) {
  640.         freeData(state2);
  641.         return INVALID_VALUE;
  642.         }
  643.  
  644.         state->value.val.lVal = state->value.val.lVal << state2.value.val.lVal;
  645.         freeData(state2);
  646.         continue;
  647.     }
  648.  
  649.     /*
  650.     ** Shift right.
  651.     */
  652.     if(match(">>")) {
  653.         if(state->value.typeValue != LONG_VAL)
  654.         return INVALID_VALUE;
  655.  
  656.         if((rVal = exp3(&state2)) != SUCCESS) {
  657.         freeData(state2);
  658.         return rVal;
  659.         }
  660.         if(state2.value.typeValue != LONG_VAL) {
  661.         freeData(state2);
  662.         return INVALID_VALUE;
  663.         }
  664.  
  665.         state->value.val.lVal = state->value.val.lVal >> state2.value.val.lVal;
  666.         continue;
  667.     }
  668.     return SUCCESS;
  669.     }
  670. }
  671.  
  672. /*
  673. ** Add/Subtract
  674. */
  675. static int exp3(State *state)
  676. {
  677. int    rVal;
  678. State    state2;
  679.  
  680.     if((rVal = exp4(state)) != SUCCESS)
  681.     return rVal;
  682.  
  683.     /*
  684.     ** Initialize the state variable.
  685.     */
  686.     copyState(state, state2);
  687.  
  688.     while(TRUE) {
  689.     /*
  690.     ** Addition.
  691.     */
  692.     if(match("+")) {
  693.         if((rVal = exp4(&state2)) != SUCCESS) {
  694.         freeData(state2);
  695.         return rVal;
  696.         }
  697.  
  698.         if(state->value.typeValue == LONG_VAL) {
  699.         if(state2.value.typeValue == LONG_VAL) {
  700.             state->value.val.lVal = state->value.val.lVal + state2.value.val.lVal;
  701.             state->value.typeValue = LONG_VAL;
  702.         } else {
  703.             state->value.val.dVal = state->value.val.lVal + state2.value.val.dVal;
  704.             state->value.typeValue = DOUBLE_VAL;
  705.         }
  706.         } else {
  707.         if(state2.value.typeValue == LONG_VAL) {
  708.             state->value.val.dVal = state->value.val.dVal + state2.value.val.lVal;
  709.             state->value.typeValue = DOUBLE_VAL;
  710.         } else {
  711.             state->value.val.dVal = state->value.val.dVal + state2.value.val.dVal;
  712.             state->value.typeValue = DOUBLE_VAL;
  713.         }
  714.         }
  715.         freeData(state2);
  716.         continue;
  717.     }
  718.  
  719.     /*
  720.     ** Subtraction.
  721.     */
  722.     if(match("-")) {
  723.         if((rVal = exp4(&state2)) != SUCCESS) {
  724.         freeData(state2);
  725.         return rVal;
  726.         }
  727.  
  728.         if(state->value.typeValue == LONG_VAL) {
  729.         if(state2.value.typeValue == LONG_VAL) {
  730.             state->value.val.lVal = state->value.val.lVal - state2.value.val.lVal;
  731.             state->value.typeValue = LONG_VAL;
  732.         } else {
  733.             state->value.val.dVal = state->value.val.lVal - state2.value.val.dVal;
  734.             state->value.typeValue = DOUBLE_VAL;
  735.         }
  736.         } else {
  737.         if(state2.value.typeValue == LONG_VAL) {
  738.             state->value.val.dVal = state->value.val.dVal - state2.value.val.lVal;
  739.             state->value.typeValue = DOUBLE_VAL;
  740.         } else {
  741.             state->value.val.dVal = state->value.val.dVal - state2.value.val.dVal;
  742.             state->value.typeValue = DOUBLE_VAL;
  743.         }
  744.         }
  745.         freeData(state2);
  746.         continue;
  747.     }
  748.     return SUCCESS;
  749.     }
  750. }
  751.  
  752. /*
  753. ** Multiply/Divide
  754. */
  755. static int exp4(State *state)
  756. {
  757. int    rVal;
  758. State    state2;
  759.  
  760.     if((rVal = exp5(state)) != SUCCESS)
  761.     return rVal;
  762.  
  763.     /*
  764.     ** Initialize the state variable.
  765.     */
  766.     copyState(state, state2);
  767.  
  768.     while(TRUE) {
  769.     /*
  770.     ** Multiplication.
  771.     */
  772.     if(match("*")) {
  773.         if((rVal = exp5(&state2)) != SUCCESS) {
  774.         freeData(state2);
  775.         return rVal;
  776.         }
  777.  
  778.         if(state->value.typeValue == LONG_VAL) {
  779.         if(state2.value.typeValue == LONG_VAL) {
  780.             state->value.val.lVal = state->value.val.lVal * state2.value.val.lVal;
  781.             state->value.typeValue = LONG_VAL;
  782.         } else {
  783.             state->value.val.dVal = state->value.val.lVal * state2.value.val.dVal;
  784.             state->value.typeValue = DOUBLE_VAL;
  785.         }
  786.         } else {
  787.         if(state2.value.typeValue == LONG_VAL) {
  788.             state->value.val.dVal = state->value.val.dVal * state2.value.val.lVal;
  789.             state->value.typeValue = DOUBLE_VAL;
  790.         } else {
  791.             state->value.val.dVal = state->value.val.dVal * state2.value.val.dVal;
  792.             state->value.typeValue = DOUBLE_VAL;
  793.         }
  794.         }
  795.         freeData(state2);
  796.         continue;
  797.     }
  798.  
  799.     /*
  800.     ** Division
  801.     */
  802.     if(match("/")) {
  803.         if((rVal = exp5(&state2)) != SUCCESS) {
  804.         freeData(state2);
  805.         return rVal;
  806.         }
  807.  
  808.         if(state->value.typeValue == LONG_VAL) {
  809.         if(state2.value.typeValue == LONG_VAL) {
  810.             state->value.val.lVal = state->value.val.lVal / state2.value.val.lVal;
  811.             state->value.typeValue = LONG_VAL;
  812.         } else {
  813.             state->value.val.dVal = state->value.val.lVal / state2.value.val.dVal;
  814.             state->value.typeValue = DOUBLE_VAL;
  815.         }
  816.         } else {
  817.         if(state2.value.typeValue == LONG_VAL) {
  818.             state->value.val.dVal = state->value.val.dVal / state2.value.val.lVal;
  819.             state->value.typeValue = DOUBLE_VAL;
  820.         } else {
  821.             state->value.val.dVal = state->value.val.dVal / state2.value.val.dVal;
  822.             state->value.typeValue = DOUBLE_VAL;
  823.         }
  824.         }
  825.         freeData(state2);
  826.         continue;
  827.     }
  828.  
  829.     /*
  830.     ** Modulues
  831.     */
  832.     if(match("%")) {
  833.         if((rVal = exp5(&state2)) != SUCCESS) {
  834.         freeData(state2);
  835.         return rVal;
  836.         }
  837.  
  838.         if(state->value.typeValue == LONG_VAL) {
  839.         if(state2.value.typeValue == LONG_VAL) {
  840.             state->value.val.lVal = state->value.val.lVal % state2.value.val.lVal;
  841.             state->value.typeValue = LONG_VAL;
  842.         } else {
  843.             state->value.typeValue = DOUBLE_VAL;
  844.             state->value.val.dVal = fmod(state->value.val.dVal, state2.value.val.dVal);
  845.         }
  846.         } else {
  847.         if(state2.value.typeValue == LONG_VAL) {
  848.             state->value.typeValue = DOUBLE_VAL;
  849.             state->value.val.dVal = fmod(state->value.val.dVal, state2.value.val.lVal);
  850.         } else {
  851.             state->value.typeValue = DOUBLE_VAL;
  852.             state->value.val.dVal = fmod(state->value.val.dVal, state2.value.val.dVal);
  853.         }
  854.         }
  855.         freeData(state2);
  856.         continue;
  857.     }
  858.  
  859.     /*
  860.     ** No more matches at this level, then must be done.
  861.     */
  862.     return SUCCESS;
  863.     }
  864. }
  865.  
  866. /*
  867. ** Unary ops
  868. */
  869. static int exp5(State *state)
  870. {
  871. int rVal;
  872.  
  873.     /*
  874.     ** Complement
  875.     */
  876.     if(match("~")) {
  877.     if((rVal = exp6(state)) != SUCCESS)
  878.         return rVal;
  879.  
  880.     if(state->value.typeValue != LONG_VAL)
  881.         return INVALID_VALUE;
  882.  
  883.     state->value.val.lVal = ~state->value.val.lVal;
  884.     return SUCCESS;
  885.     }
  886.  
  887.     /*
  888.     ** Not
  889.     */
  890.     if(match("!")) {
  891.     if((rVal = exp6(state)) != SUCCESS)
  892.         return rVal;
  893.  
  894.     if(state->value.typeValue != LONG_VAL)
  895.         return INVALID_VALUE;
  896.  
  897.     state->value.val.lVal = !state->value.val.lVal;
  898.     return SUCCESS;
  899.     }
  900.  
  901.     /*
  902.     ** Negate
  903.     */
  904.     if(match("-")) {
  905.     if((rVal = exp6(state)) != SUCCESS)
  906.         return rVal;
  907.  
  908.     if(state->value.typeValue == LONG_VAL)
  909.         state->value.val.lVal = -state->value.val.lVal;
  910.     else
  911.         state->value.val.dVal = -state->value.val.dVal;
  912.  
  913.     return SUCCESS;
  914.     }
  915.  
  916.     /*
  917.     ** Address of expression.
  918.     */
  919.     if(match("&")) {
  920.     if((rVal = exp6(state)) != SUCCESS)
  921.         return rVal;
  922.  
  923.     state->value.val.lVal = state->addr;
  924.     state->value.typeValue = LONG_VAL;
  925.     }
  926.  
  927.     /*
  928.     ** Dereference the expresssion.
  929.     */
  930.     if(match("*")) {
  931.     if((rVal = exp5(state)) != SUCCESS)
  932.         return rVal;
  933.  
  934. fprintf(logFile, "Address Dereference Not yet implemented\n");
  935.     }
  936.  
  937.     /*
  938.     ** Allow nested parens.
  939.     */
  940.     if(match("(")) {
  941.     if((rVal = exp1(state)) != SUCCESS)
  942.         return rVal;
  943.     if(!match(")"))
  944.         return SYNTAX_ERROR;
  945.     return SUCCESS;
  946.     }
  947.  
  948.     return exp6(state);
  949. }
  950.  
  951. /*
  952. ** Nesting
  953. */
  954. static int exp6(State *state)
  955. {
  956. int    rVal;
  957. State    state2;
  958.  
  959.     /*
  960.     ** Initialize the state variable.
  961.     */
  962.     copyState(state, state2);
  963.  
  964.     /*
  965.     ** Is the next token a literal?
  966.     */
  967.     if(isLiteral(state)) {
  968.     rVal = SUCCESS;
  969.     } else {
  970.     rVal = primary(state);
  971.     }
  972.  
  973.     while(TRUE) {
  974.  
  975.     /*
  976.     ** See if we have an array.
  977.     */
  978.     if(match("[")) {
  979.         if((rVal = exp1(&state2)) != SUCCESS) {
  980.         freeData(state2);
  981.         return rVal;
  982.         }
  983.  
  984.         if(state2.value.typeValue != LONG_VAL) {
  985.         freeData(state2);
  986.         return INVALID_VALUE;
  987.         }
  988.         if(!match("]")) {
  989.         freeData(state2);
  990.         return SYNTAX_ERROR;
  991.         }
  992.  
  993.         /*
  994.         ** See if this is a string.
  995.         */
  996.         if(state->value.typeValue != PTR_VAL) {
  997.         if(state->value.typeValue == STR_VAL) {
  998.             char   *data;
  999.  
  1000.             data = state->value.val.sVal;
  1001.             state->value.typeValue = CHAR_VAL;
  1002.             if(((int) state2.value.val.lVal < 0) ||
  1003.               (state2.value.val.lVal >= strlen(data)))
  1004.             return INVALID_INDEX;
  1005.  
  1006.             state->value.val.cVal = data[state2.value.val.lVal];
  1007.             freeData(state2);
  1008.             free(data);
  1009.             return SUCCESS;
  1010.         }
  1011.         return SYNTAX_ERROR;
  1012.         }
  1013.  
  1014.         /*
  1015.         ** Must be for a more complex array.
  1016.         */
  1017.         if((rVal = GetArray(state->module, state, &state2)) != SUCCESS) {
  1018.         freeData(state2);
  1019.         return rVal;
  1020.         }
  1021.         freeData(state2);
  1022.         continue;
  1023.     }
  1024.  
  1025.     /*
  1026.     ** While we find members of a name, keep getting them.
  1027.     */
  1028.     if(match("->") || match(".")) {
  1029.         char   *ptr;
  1030.         if((ptr = isName()) == NULL) {
  1031.         return INVALID_NAME;
  1032.         }
  1033.         state2.value.typeValue = NAME_VAL;
  1034.         state2.value.val.sVal = ptr;
  1035.         if((rVal = GetName(state->module, state, &state2)) != SUCCESS) {
  1036.         freeData(state2);
  1037.         return rVal;
  1038.         }
  1039.         freeData(state2);
  1040.         continue;
  1041.     }
  1042.     return SUCCESS;
  1043.     }
  1044. }
  1045.  
  1046. /*
  1047. ** Primary name.
  1048. */
  1049. static int primary(State *state)
  1050. {
  1051. int    rVal;
  1052. char   *ptr;
  1053.  
  1054.     /*
  1055.     ** If we do not match a name, we are in trouble.
  1056.     */
  1057.     if((ptr = isName()) == NULL) {
  1058.     if(isRegister(state))
  1059.         return SUCCESS;
  1060.     return INVALID_NAME;
  1061.     }
  1062.  
  1063.     /*
  1064.     ** Find the primary name.
  1065.     */
  1066.     state->value.val.sVal = ptr;
  1067.     state->value.typeValue = NAME_VAL;
  1068.     if((rVal = GetName(state->module, state, NULL)) != SUCCESS) {
  1069.     return rVal;
  1070.     }
  1071.  
  1072.     return rVal;
  1073. }
  1074.