Advertisement
ZoriaRPG

ZBasic DoExpr() v0.1

Jul 2nd, 2018
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.31 KB | None | 0 0
  1.  
  2. int DoExpr(int word)
  3. {
  4.     int inistack = curstack;
  5.     int buf[1024]; int q; bool foundop; bool sign;
  6.     int number[14]; bool foundvalue; bool foundnum; bool foundvar;
  7.     int len = SizeOfArray(word);
  8.     int varid[4];
  9.     for ( q = 0; q < len; ++q ) buf[q] = word[q];
  10.     int place = 0;
  11.     ++q; buf[q] = 0; //terminate
  12.     q = -1;
  13.     while(reading)
  14.     {
  15.         ++q;
  16.        
  17.         //look for numbers before an operator
  18.         if ( !foundop && !foundvalue )
  19.             //first value can be signed
  20.         {
  21.             if ( buf[q] == ' '; ) //eat WS
  22.                 continue;
  23.            
  24.             if ( buf[q] == '-' ) { sign = true; ++q; }
  25.             //read the number portion of the string into a value.
  26.             //numbers only
  27.            
  28.             while ( IsNumber(buf[q]) || buf[q] == '.' )
  29.             {
  30.                 foundnum = true;
  31.                 if ( buf[q] == '.' )
  32.                 {
  33.                     if ( !dec )
  34.                     {
  35.                         dec = true;
  36.                         number[place] = buf[q]];
  37.                         ++place;
  38.                     }
  39.                     else continue;
  40.                 }
  41.                 else
  42.                 {
  43.                     number[place] = buf[q];
  44.                     ++place;
  45.                 }
  46.             }
  47.            
  48.             if ( foundnum ) //it isn't a var, but a literal
  49.             {
  50.                 if ( sign ) { sign = false; val = atof(number)*-1;  }//store the extracted value.
  51.                 else val = atof(number); //store the extracted value.
  52.                 foundvalue = true;
  53.             }
  54.             //variables
  55.             if ( !foundnum ) //is it a variable?
  56.             {
  57.                 bool seeking = true;
  58.                 while(seeking)
  59.                 {
  60.                     if ( buf[q] == 'I' ) { foundvar = true; seeking = false; break; }
  61.                     else if ( buf[q] == 'N' ) { foundvar = true; seeking = false; break; }
  62.                     else if ( buf[q] == 'L' ) { foundvar = true; seeking = false; break; }
  63.                     else if ( buf[q] == 'E' ) { foundvar = true; seeking = false; break; }
  64.                     else if ( buf[q] == 'F' ) { foundvar = true; seeking = false; break; }
  65.                     else if ( buf[q] == 'C' ) { foundvar = true; seeking = false; break; }
  66.                     else if ( buf[q] == 'W' ) { foundvar = true; seeking = false; break; }
  67.                     else if ( buf[q] == 'S' ) { foundvar = true; seeking = false; break; }
  68.                     else if ( buf[q] == '<' ) { foundvar = true; seeking = false; break; }
  69.                     else break;
  70.                 }
  71.                 if ( foundvar ) //found it, so extract it.
  72.                 {
  73.                     for ( int w = q; w < q+2; ++w )
  74.                     {
  75.                         varid[w] = buf[q+w];
  76.                     }
  77.                     int vartype = ExtractVarType(varid);
  78.                     int indx = ExtractVarID(varid);
  79.                     val = GetVarValue(vartype,indx);
  80.                     foundvalue = true;
  81.                 }
  82.                    
  83.             }
  84.                
  85.         }
  86.         if ( foundvalue )
  87.         //push value here
  88.         stack.change(OPERAND);
  89.         stack.push(val);
  90.        
  91.         // 1.2.3 A left parenthesis: push it onto the operator stack.
  92.      
  93.      
  94.         //loop for a lparen
  95.         if ( buf[q] == '(' )
  96.         {
  97.             //push the paren to operators
  98.             stack.change(OPERATORS);
  99.             stack.push('(');
  100.         }
  101.        
  102.         //  1.2.4 A right parenthesis:
  103.         if ( buf[q] == ')' )
  104.         {
  105.             /*
  106.             1 While the thing on top of the operator stack is not a
  107.            left parenthesis,
  108.              1 Pop the operator from the operator stack.
  109.              2 Pop the value stack twice, getting two operands.
  110.              3 Apply the operator to the operands, in the correct order.
  111.              4 Push the result onto the value stack.
  112.          2 Pop the left parenthesis from the operator stack, and discard it.
  113.             */
  114.         }
  115.        
  116.        
  117.         /*
  118.        
  119.            1.2.5 An operator (call it thisOp):
  120.          1 While the operator stack is not empty, and the top thing on the
  121.            operator stack has the same or greater precedence as thisOp,
  122.            1 Pop the operator from the operator stack.
  123.         //stack.change(OPERATOR);
  124.         //stack.pop(INX1);
  125.            2 Pop the value stack twice, getting two operands.
  126.         //
  127.         // stack.change(OPERAND).
  128.         // stack.pop(EXPR2);
  129.         // stack.pop(EXPR1);
  130.            3 Apply the operator to the operands, in the correct order.
  131.            4 Push the result onto the value stack.
  132.         //stack.push(EXPR2);
  133.          2 Push thisOp onto the operator stack.
  134.     2. While the operator stack is not empty,
  135.         1 Pop the operator from the operator stack.
  136.         2 Pop the value stack twice, getting two operands.
  137.         3 Apply the operator to the operands, in the correct order.
  138.         4 Push the result onto the value stack.
  139.     3. At this point the operator stack should be empty, and the value
  140.        stack should have only one value in it, which is the final result.
  141.    */
  142.        
  143.         if ( !foundop && foundvalue )
  144.             //the next thing must be an operator
  145.         {
  146.             if ( buf[q] == ' '; ) //eat WS
  147.                 continue;
  148.            
  149.             if ( IsOperator(buf[q] )
  150.             {
  151.                 //push it and its precedence
  152.                 foundop = true;
  153.             }
  154.            
  155.         }
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement