Click here to Skip to main content
15,897,187 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: okay. I ... really like TypeScript Pin
Sander Rossel14-Sep-19 5:08
professionalSander Rossel14-Sep-19 5:08 
GeneralRe: okay. I ... really like TypeScript Pin
honey the codewitch14-Sep-19 5:10
mvahoney the codewitch14-Sep-19 5:10 
GeneralDo you still have the ugliest function you've ever written? Pin
honey the codewitch13-Sep-19 8:20
mvahoney the codewitch13-Sep-19 8:20 
JokeRe: Do you still have the ugliest function you've ever written? Pin
W Balboos, GHB13-Sep-19 8:54
W Balboos, GHB13-Sep-19 8:54 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
honey the codewitch13-Sep-19 8:55
mvahoney the codewitch13-Sep-19 8:55 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
MarkTJohnson13-Sep-19 9:09
professionalMarkTJohnson13-Sep-19 9:09 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
honey the codewitch13-Sep-19 9:10
mvahoney the codewitch13-Sep-19 9:10 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
David O'Neil13-Sep-19 9:12
professionalDavid O'Neil13-Sep-19 9:12 
I believe this was the function that made me completely reevaluate my approach to parsing. Ultimately, I came up with something FAR simpler, that wasn't going to be a nightmare to change when adding another condition to the code.
C++
bool parser::parseExecute()
{
   //Start at the bottom of the token and precedence stack.  Place these values onto the
   //workTokenStack and workPrecStack.  Check if the precedence value is lower than the
   //previous one and take action if it is the case

   tokStruct_ *workTokStack;
   precedence_ *workPrecStack;
   int numTok=0, numPrec=0;
   tokStackPos = -1;

   if (!setupTokStackMem(&workTokStack, ansStackAlloc) ||
             !setupPrecStackMem(&workPrecStack, ansStackAlloc)) {
      MsgBox("Unable to allocate memory in ExecuteToken");
      return false;
   }
   workPrecStack[0] = PrecGet;
   workTokStack[0].token = TokGet;

   do {
      ++tokStackPos;
      //pop on the next precedece and token
      if (tokStack[tokStackPos].token == TokValue) {
         //must place it on the ansStack and go for the next one
         ansStack[++ansStackPos] = valStack[tokStack[tokStackPos].varListPos];
         continue;
      }
      if (tokStack[tokStackPos].token == Tok0DimVar) {
         ansStack[++ansStackPos] = getValFromVarList();
         continue;
      }
      if (tokStack[tokStackPos].token == TokMDimVarEnd) {
         //need to place the correct array variable onto ansStack.
         //first, check to make sure that the last token on workTokStack is TokMDimVar
         if (workTokStack[tokStackPos-1].token == TokMDimVarAssign) break;
         if (workTokStack[tokStackPos-1].token != TokMDimVar) {
            MsgBox("']' encountered without arrayed variable");
            return moveCursorToError(origStr);
         }
         //now, need to place the appropriate value on the stack
         int Y, Z, pos, dim;
         Y = varList[workTokStack[numTok-1].varListPos].Y;
         Z = varList[workTokStack[numTok-1].varListPos].Z;
         dim = varList[workTokStack[numTok-1].varListPos].varDim;
         if (dim == 1) pos = ansStack[ansStackPos];
         else if (dim == 2) pos = ansStack[ansStackPos-1] +
                  ansStack[ansStackPos]*Y;
         else if (dim == 3) pos = ansStack[ansStackPos-2] +
                  ansStack[ansStackPos-1]*Y + ansStack[ansStackPos]*Y*Z;

         //finally,
         ansStackPos = ansStackPos-dim+1;
         if (varList[workTokStack[numTok-1].varListPos].varType == Int) {
            ansStack[ansStackPos] =
            *((int*)(varList[workTokStack[numTok-1].varListPos].varAddress)+pos);
         }
         else if (varList[workTokStack[numTok-1].varListPos].varType == Double) {
            ansStack[ansStackPos] =
            *((double*)(varList[workTokStack[numTok-1].varListPos].varAddress)+pos);
         }
         else {
            MsgBox("Bad varType placed on ansStack");
            return moveCursorToError(origStr);
         }
         ansStackPos++;
         numTok--;   //take the variable off of the workTokStack
         continue;
      }

      ++numTok; ++numPrec;
      workTokStack[numTok] = tokStack[tokStackPos];
      workPrecStack[numPrec] = precStack[tokStackPos];
      if (workTokStack[numTok].token == TokLParen) continue;
      while (workPrecStack[numPrec-1] >= workPrecStack[numPrec]) {
         //pop the precedence stack down
         workPrecStack[numPrec-1] = workPrecStack[numPrec];
         numPrec--;
         switch (workTokStack[numTok-1].token) {
            case TokValue: //place the value on the answer stack
               ansStack[++ansStackPos] =
                     *(double*)varList[workTokStack[numTok-1].varListPos].varAddress;
               numTok++;
               numPrec++;
               tokStackPos++;
               break;
            case TokVarAssign: //place the value in the answer stack into the variable
               if (ansStackPos != 0) {
                  MsgBox("Malformed statement");
                  delete workTokStack;
                  delete workPrecStack;
                  return false;
               }
               *(double*)varList[workTokStack[numTok-1].varListPos].varAddress = ansStack[0];
               //pop the semiColon off the stacks
               numTok=numTok-2;
               numPrec--;
               ansStackPos--;
               goto pETBreakout;
            case TokAdd: //add the last two values in the answer stack
               ansStack[ansStackPos-1] = ansStack[ansStackPos-1] + ansStack[ansStackPos];
               ansStackPos--;
               numTok--;
               break;
            case TokSubtract:
               ansStack[ansStackPos-1] = ansStack[ansStackPos-1] - ansStack[ansStackPos];
               ansStackPos--;
               numTok--;
               break;
            case TokMultiply:
               ansStack[ansStackPos-1] = ansStack[ansStackPos-1] * ansStack[ansStackPos];
               ansStackPos--;
               numTok--;
               break;
            case TokDivide:
               ansStack[ansStackPos-1] = ansStack[ansStackPos-1] / ansStack[ansStackPos];
               ansStackPos--;
               numTok--;
               break;
            case TokPower:
               ansStack[ansStackPos-1] = pow(ansStack[ansStackPos-1], ansStack[ansStackPos]);
               ansStackPos--;
               numTok--;
               break;
            case TokSemiCol:  //it will reach here when the End token is reached
               if (ansStackPos != 0) {
                  MsgBox("Misformed Statement");
                  delete workTokStack;
                  delete workPrecStack;
                  return false;
               }
               ansStackPos--;
               break;
            case TokComma:
               //need to shrink workPrecStack and workTokStack to eliminate the comma.
               //workTokStack is popped down below.  workPrecStack has been popped down
               //up above
               numTok--;
               break;
            case TokLParen:
               //There are only two times this will be entered: 1) there are two or more
               //  left parenthesis on the stack back to back, or 2) a closing parenthesis
               //  and a opening parenthesis are back to back on the workTokStack
               if (tokStack[tokStackPos].token == TokLParen &&
                        tokStack[tokStackPos-1].token == TokLParen) {
                  numPrec++;
                  goto pETBreakout;
               }
               numTok=numTok-2;
               numPrec--;
               goto pETBreakout;
            case TokRParen:
               break;
            case TokMDimVar:
               //it only reaches here when assigning the last value on ansStack to the
               //appropriate location in memory for the variable.
                  int Y, Z, pos, dim;
                  Y = varList[workTokStack[numTok-1].varListPos].Y;
                  Z = varList[workTokStack[numTok-1].varListPos].Z;
                  dim = varList[workTokStack[numTok-1].varListPos].varDim;
                  if (dim == 1) pos = ansStack[ansStackPos-1];
                  else if (dim == 2) pos = ansStack[ansStackPos-2] +
                           ansStack[ansStackPos-1]*Y;
                  else if (dim == 3) pos = ansStack[ansStackPos-3] +
                           ansStack[ansStackPos-2]*Y + ansStack[ansStackPos-1]*Y*Z;

                  //finally,
                  if (varList[workTokStack[numTok-1].varListPos].varType == Int) {
                     *((int*)(varList[workTokStack[numTok-1].varListPos].varAddress)+pos) =
                              ansStack[ansStackPos];
                  }
                  else if (varList[workTokStack[numTok-1].varListPos].varType == Double) {
                     *((double*)(varList[workTokStack[numTok-1].varListPos].varAddress)+pos) =
                              ansStack[ansStackPos];
                  }
                  else {
                     MsgBox("Bad varType placed on ansStack");
                     return moveCursorToError(origStr);
                  }
                  ansStackPos = ansStackPos-dim;
                  numTok--;   //take the assignment off of the workTokStack
                  continue;

            default:
               MsgBox("Still Programming!");
               delete workTokStack;
               delete workPrecStack;
               return false;
         }
         //pop the workTokStack down
         workTokStack[numTok] = workTokStack[numTok+1];
      } //end of while loop for processing previous tokens.
pETBreakout:
   } //end of do loop
   while (workPrecStack[numPrec] != PrecEnd);
   delete workPrecStack;   workPrecStack = NULL;
   delete workTokStack;      workTokStack = NULL;
   return true;
}

GeneralRe: Do you still have the ugliest function you've ever written? Pin
honey the codewitch13-Sep-19 9:14
mvahoney the codewitch13-Sep-19 9:14 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
Chris Maunder13-Sep-19 9:26
cofounderChris Maunder13-Sep-19 9:26 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
honey the codewitch13-Sep-19 9:27
mvahoney the codewitch13-Sep-19 9:27 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
Sander Rossel14-Sep-19 1:31
professionalSander Rossel14-Sep-19 1:31 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
honey the codewitch14-Sep-19 1:42
mvahoney the codewitch14-Sep-19 1:42 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
Sander Rossel14-Sep-19 2:01
professionalSander Rossel14-Sep-19 2:01 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
honey the codewitch14-Sep-19 2:10
mvahoney the codewitch14-Sep-19 2:10 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
Sander Rossel14-Sep-19 2:18
professionalSander Rossel14-Sep-19 2:18 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
honey the codewitch14-Sep-19 2:19
mvahoney the codewitch14-Sep-19 2:19 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
dandy7214-Sep-19 8:06
dandy7214-Sep-19 8:06 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
Chris Maunder16-Sep-19 5:00
cofounderChris Maunder16-Sep-19 5:00 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
Jörgen Andersson13-Sep-19 11:24
professionalJörgen Andersson13-Sep-19 11:24 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
Amarnath S13-Sep-19 14:54
professionalAmarnath S13-Sep-19 14:54 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
GKP199214-Sep-19 1:03
professionalGKP199214-Sep-19 1:03 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
honey the codewitch14-Sep-19 2:11
mvahoney the codewitch14-Sep-19 2:11 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
Sander Rossel14-Sep-19 1:44
professionalSander Rossel14-Sep-19 1:44 
GeneralRe: Do you still have the ugliest function you've ever written? Pin
honey the codewitch14-Sep-19 1:47
mvahoney the codewitch14-Sep-19 1:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Straw Poll

Were you affected by the geomagnetic storms this past weekend?
Communication disruptions, electrified pipes, random unexplained blue-screens in Windows - the list of effects is terrifying.
  Results   22 votes