/*======================================================================*/
/*  Calculator EXPRESSION Command Routine(s)                            */
/*======================================================================*/

#include stdio
#include ctype

/* calculator common definitions */

#include "calc_include.h"

/* global flags and variables */

extern int    debug_flag;
extern int    error_flag;
extern int    format_precision;
extern struct token_struct numeric_value;

/* local flags and variables */

enum datatypes standard_number_type;

char symbol[strsize];


/*----------------------------------------------------------------------*/
/*  Display The Results Of An Evaluated Expression                      */
/*                                                                      */
/*  Parameter  Description                                              */
/*                                                                      */
/*  options    processing option flags                                  */
/*  stringcnt  length of input string                                   */
/*  stringptr  address of input string                                  */
/*  tokencnt   length of current token                                  */
/*  tokenptr   address of current token                                 */
/*  character  character value of character token                       */
/*  number     binary value of numeric token                            */
/*  param      user supplied argument                                   */
/*                                                                      */
/*----------------------------------------------------------------------*/

int execute_expression_command(options,stringcnt,stringptr,tokencnt,
                               tokenptr,character,number,param)
int  options,stringcnt,tokencnt,number,param;
char character,stringptr[],tokenptr[];
{
   if (error_flag) return 1;

   if (debug_flag)
      printf("Command:    %.*s\n",tokencnt,tokenptr);

   if (numeric_value.type == INTEGER)
      printf("%d\n",numeric_value.idata);
   else if (numeric_value.type ==FLOAT)
      printf("%.*f\n",format_precision,numeric_value.fdata);
   else
      {
      printf
      ("-- Internal error, expression resulted in an unknown data type\n");
      error_flag = TRUE;
      }

   return 1;
}


/*----------------------------------------------------------------------*/
/*  An Expression Was Sucessfully Parsed                                */
/*                                                                      */
/*  Parameter  Description                                              */
/*                                                                      */
/*  options    processing option flags                                  */
/*  stringcnt  length of input string                                   */
/*  stringptr  address of input string                                  */
/*  tokencnt   length of current token                                  */
/*  tokenptr   address of current token                                 */
/*  character  character value of character token                       */
/*  number     binary value of numeric token                            */
/*  param      user supplied argument                                   */
/*                                                                      */
/*----------------------------------------------------------------------*/

int display_expression (options,stringcnt,stringptr,tokencnt,
                        tokenptr,character,number,param)
int  options,stringcnt,tokencnt,number,param;
char character,stringptr[],tokenptr[];
{
   if (debug_flag)
      printf("Expression: %.*s\n",tokencnt,tokenptr);

   return 1;
}


/*----------------------------------------------------------------------*/
/*  A Term Was Successfully Parsed                                      */
/*                                                                      */
/*  Parameter  Description                                              */
/*                                                                      */
/*  options    processing option flags                                  */
/*  stringcnt  length of input string                                   */
/*  stringptr  address of input string                                  */
/*  tokencnt   length of current token                                  */
/*  tokenptr   address of current token                                 */
/*  character  character value of character token                       */
/*  number     binary value of numeric token                            */
/*  param      user supplied argument                                   */
/*                                                                      */
/*----------------------------------------------------------------------*/

int display_term (options,stringcnt,stringptr,tokencnt,
                  tokenptr,character,number,param)
int  options,stringcnt,tokencnt,number,param;
char character,stringptr[],tokenptr[];
{
   if (debug_flag)
      printf("Term:       %.*s\n",tokencnt,tokenptr);

   return 1;
}


/*----------------------------------------------------------------------*/
/*  A Factor Was Successfully Parsed                                    */
/*                                                                      */
/*  Parameter  Description                                              */
/*                                                                      */
/*  options    processing option flags                                  */
/*  stringcnt  length of input string                                   */
/*  stringptr  address of input string                                  */
/*  tokencnt   length of current token                                  */
/*  tokenptr   address of current token                                 */
/*  character  character value of character token                       */
/*  number     binary value of numeric token                            */
/*  param      user supplied argument                                   */
/*                                                                      */
/*----------------------------------------------------------------------*/

int display_factor (options,stringcnt,stringptr,tokencnt,
                    tokenptr,character,number,param)
int  options,stringcnt,tokencnt,number,param;
char character,stringptr[],tokenptr[];
{
   if (debug_flag)
      printf("Factor:     %.*s\n",tokencnt,tokenptr);

   return 1;
}


/*----------------------------------------------------------------------*/
/*  Parenthesis Token Parsed                                            */
/*                                                                      */
/*  Parameter  Description                                              */
/*                                                                      */
/*  options    processing option flags                                  */
/*  stringcnt  length of input string                                   */
/*  stringptr  address of input string                                  */
/*  tokencnt   length of current token                                  */
/*  tokenptr   address of current token                                 */
/*  character  character value of character token                       */
/*  number     binary value of numeric token                            */
/*  param      user supplied argument                                   */
/*                                                                      */
/*----------------------------------------------------------------------*/

int end_expression(options,stringcnt,stringptr,tokencnt,
                   tokenptr,character,number,param)
int  options,stringcnt,tokencnt,number,param;
char character,stringptr[],tokenptr[];
{
   int idx;

   if (debug_flag)
      printf("Separator:  %.*s\n",tokencnt,tokenptr);

   end_compound_expression ();

   return 1;
}

/*----------------------------------------------------------------------*/
/*  Parenthesis Token Parsed                                            */
/*                                                                      */
/*  Parameter  Description                                              */
/*                                                                      */
/*  options    processing option flags                                  */
/*  stringcnt  length of input string                                   */
/*  stringptr  address of input string                                  */
/*  tokencnt   length of current token                                  */
/*  tokenptr   address of current token                                 */
/*  character  character value of character token                       */
/*  number     binary value of numeric token                            */
/*  param      user supplied argument                                   */
/*                                                                      */
/*----------------------------------------------------------------------*/

int parenthesis(options,stringcnt,stringptr,tokencnt,
                tokenptr,character,number,param)
int  options,stringcnt,tokencnt,number,param;
char character,stringptr[],tokenptr[];
{
   int idx;

   if (debug_flag)
      printf("Paren:      %.*s\n",tokencnt,tokenptr);

   create_paren_token(tokenptr[0]);

   return 1;
}


/*----------------------------------------------------------------------*/
/*  Unary Operator Token Parsed                                         */
/*                                                                      */
/*  Parameter  Description                                              */
/*                                                                      */
/*  options    processing option flags                                  */
/*  stringcnt  length of input string                                   */
/*  stringptr  address of input string                                  */
/*  tokencnt   length of current token                                  */
/*  tokenptr   address of current token                                 */
/*  character  character value of character token                       */
/*  number     binary value of numeric token                            */
/*  param      user supplied argument                                   */
/*                                                                      */
/*----------------------------------------------------------------------*/

int unary_operator (options,stringcnt,stringptr,tokencnt,
                    tokenptr,character,number,param)
int  options,stringcnt,tokencnt,number,param;
char character,stringptr[],tokenptr[];
{
   int idx;

   if (debug_flag)
      printf("U op:       %.*s\n",tokencnt,tokenptr);

   create_operator_token(UNARY_OP,tokenptr[0]);

   return 1;
}


/*----------------------------------------------------------------------*/
/*  Binary Operator Token Parsed                                        */
/*                                                                      */
/*  Parameter  Description                                              */
/*                                                                      */
/*  options    processing option flags                                  */
/*  stringcnt  length of input string                                   */
/*  stringptr  address of input string                                  */
/*  tokencnt   length of current token                                  */
/*  tokenptr   address of current token                                 */
/*  character  character value of character token                       */
/*  number     binary value of numeric token                            */
/*  param      user supplied argument                                   */
/*                                                                      */
/*----------------------------------------------------------------------*/

int binary_operator (options,stringcnt,stringptr,tokencnt,
                     tokenptr,character,number,param)
int  options,stringcnt,tokencnt,number,param;
char character,stringptr[],tokenptr[];
{
   int idx;

   if (debug_flag)
      printf("B op:       %.*s\n",tokencnt,tokenptr);

   create_operator_token(BINARY_OP,tokenptr[0]);

   return 1;
}


/*----------------------------------------------------------------------*/
/*  Number Operand Parsed                                               */
/*                                                                      */
/*  Parameter  Description                                              */
/*                                                                      */
/*  options    processing option flags                                  */
/*  stringcnt  length of input string                                   */
/*  stringptr  address of input string                                  */
/*  tokencnt   length of current token                                  */
/*  tokenptr   address of current token                                 */
/*  character  character value of character token                       */
/*  number     binary value of numeric token                            */
/*  param      user supplied argument                                   */
/*                                                                      */
/*----------------------------------------------------------------------*/

int number_operand (options,stringcnt,stringptr,tokencnt,
                    tokenptr,character,number,param)
int  options,stringcnt,tokencnt,number,param;
char character,stringptr[],tokenptr[];
{
   int idx;

   if (error_flag) return 1;

   if (debug_flag)
      printf("Number:     %.*s\n",tokencnt,tokenptr);

   create_value_token();

   return 1;
}


/*----------------------------------------------------------------------*/
/*  Symbol Operand Parsed                                               */
/*                                                                      */
/*  Parameter  Description                                              */
/*                                                                      */
/*  options    processing option flags                                  */
/*  stringcnt  length of input string                                   */
/*  stringptr  address of input string                                  */
/*  tokencnt   length of current token                                  */
/*  tokenptr   address of current token                                 */
/*  character  character value of character token                       */
/*  number     binary value of numeric token                            */
/*  param      user supplied argument                                   */
/*                                                                      */
/*----------------------------------------------------------------------*/

int symbol_operand (options,stringcnt,stringptr,tokencnt,
                    tokenptr,character,number,param)
int  options,stringcnt,tokencnt,number,param;
char character,stringptr[],tokenptr[];
{
   int  i,idx,limit,status;
   char symbol[strsize];

   if (error_flag) return 1;

   if (debug_flag)
      printf("Symbol:     %.*s\n",tokencnt,tokenptr);

   if (tokencnt > strsize - 1)
      limit = strsize - 1;
   else
      limit = tokencnt;

   for (i=0;i<limit;i++) symbol[i] = _toupper(tokenptr[i]);
   symbol[limit] = '\0';

   if (locate_symbol(&idx,symbol))
      {
      return_symbol_value(idx,&numeric_value);
      create_value_token();
      }
   else
      {
      printf("-- Symbol %s is currently not defined\n",symbol);
      error_flag = TRUE;
      }

   return 1;
}


/*----------------------------------------------------------------------*/
/*  A Standard Number Was Successfully Parsed                           */
/*                                                                      */
/*  Parameter  Description                                              */
/*                                                                      */
/*  options    processing option flags                                  */
/*  stringcnt  length of input string                                   */
/*  stringptr  address of input string                                  */
/*  tokencnt   length of current token                                  */
/*  tokenptr   address of current token                                 */
/*  character  character value of character token                       */
/*  number     binary value of numeric token                            */
/*  param      user supplied argument                                   */
/*                                                                      */
/*----------------------------------------------------------------------*/

int standard_number (options,stringcnt,stringptr,tokencnt,
                     tokenptr,character,number,param)
int  options,stringcnt,tokencnt,number,param;
char character,stringptr[],tokenptr[];
{
   int status;

   numeric_value.type = UNKNOWN;

   if (standard_number_type == INTEGER)
      {
      status = sscanf(tokenptr,"%d",&numeric_value.idata);
      if ((status&1)==1)
         {
         numeric_value.type  = INTEGER;
         if (debug_flag)
            printf("Std num:    %d\n",numeric_value.idata);
         }
      else
         {
         error_flag = TRUE;
         numeric_value.type  = UNKNOWN;
         printf("-- Illegal integer value %.*s\n",
            tokencnt,tokenptr);
         }
      }
   else
      {
      status = sscanf(tokenptr,"%f",&numeric_value.fdata);
      if ((status&1)==1)
         {
         numeric_value.type  = FLOAT;
         if (debug_flag)
            printf("Std num:    %f\n",numeric_value.idata);
         }
      else
         {
         error_flag = TRUE;
         numeric_value.type  = UNKNOWN;
         printf("-- Illegal float value %.*s\n",
            tokencnt,tokenptr);
         }
      }

   return 1;
}


/*----------------------------------------------------------------------*/
/*  The Integer Part Of A Standard Number Was Successfully Parsed       */
/*                                                                      */
/*  Parameter  Description                                              */
/*                                                                      */
/*  options    processing option flags                                  */
/*  stringcnt  length of input string                                   */
/*  stringptr  address of input string                                  */
/*  tokencnt   length of current token                                  */
/*  tokenptr   address of current token                                 */
/*  character  character value of character token                       */
/*  number     binary value of numeric token                            */
/*  param      user supplied argument                                   */
/*                                                                      */
/*----------------------------------------------------------------------*/

int integer_part(options,stringcnt,stringptr,tokencnt,
                 tokenptr,character,number,param)
int  options,stringcnt,tokencnt,number,param;
char character,stringptr[],tokenptr[];
{
   standard_number_type = INTEGER;

   if (debug_flag)
      printf("Integer:    %.*s\n",tokencnt,tokenptr);

   return 1;
}


/*----------------------------------------------------------------------*/
/*  The Fraction Part Of A Stadard Number Was Successfully Parsed       */
/*                                                                      */
/*  Parameter  Description                                              */
/*                                                                      */
/*  options    processing option flags                                  */
/*  stringcnt  length of input string                                   */
/*  stringptr  address of input string                                  */
/*  tokencnt   length of current token                                  */
/*  tokenptr   address of current token                                 */
/*  character  character value of character token                       */
/*  number     binary value of numeric token                            */
/*  param      user supplied argument                                   */
/*                                                                      */
/*----------------------------------------------------------------------*/

int fraction_part(options,stringcnt,stringptr,tokencnt,
                  tokenptr,character,number,param)
int  options,stringcnt,tokencnt,number,param;
char character,stringptr[],tokenptr[];
{
   standard_number_type = FLOAT;

   if (debug_flag)
      printf("Integer:    %.*s\n",tokencnt,tokenptr);

   return 1;
}


/*----------------------------------------------------------------------*/
/*  The Decimal Point In A Standard Number Was Successfully Parsed      */
/*                                                                      */
/*  Parameter  Description                                              */
/*                                                                      */
/*  options    processing option flags                                  */
/*  stringcnt  length of input string                                   */
/*  stringptr  address of input string                                  */
/*  tokencnt   length of current token                                  */
/*  tokenptr   address of current token                                 */
/*  character  character value of character token                       */
/*  number     binary value of numeric token                            */
/*  param      user supplied argument                                   */
/*                                                                      */
/*----------------------------------------------------------------------*/

int decimal_point(options,stringcnt,stringptr,tokencnt,
                  tokenptr,character,number,param)
int  options,stringcnt,tokencnt,number,param;
char character,stringptr[],tokenptr[];
{
   standard_number_type = FLOAT;

   if (debug_flag)
      printf("Token:      %.*s\n",tokencnt,tokenptr);

   return 1;
}


/*----------------------------------------------------------------------*/
/*  The Exponent Part Of A Standard Number Was Succesfully Parsed       */
/*                                                                      */
/*  Parameter  Description                                              */
/*                                                                      */
/*  options    processing option flags                                  */
/*  stringcnt  length of input string                                   */
/*  stringptr  address of input string                                  */
/*  tokencnt   length of current token                                  */
/*  tokenptr   address of current token                                 */
/*  character  character value of character token                       */
/*  number     binary value of numeric token                            */
/*  param      user supplied argument                                   */
/*                                                                      */
/*----------------------------------------------------------------------*/

int exponent_part(options,stringcnt,stringptr,tokencnt,
                  tokenptr,character,number,param)
int  options,stringcnt,tokencnt,number,param;
char character,stringptr[],tokenptr[];
{
   standard_number_type = FLOAT;

   if (debug_flag)
      printf("Integer:    %.*s\n",tokencnt,tokenptr);

   return 1;
}


/*----------------------------------------------------------------------*/
/*  An Octal Number Was Successfully Parsed                             */
/*                                                                      */
/*  Parameter  Description                                              */
/*                                                                      */
/*  options    processing option flags                                  */
/*  stringcnt  length of input string                                   */
/*  stringptr  address of input string                                  */
/*  tokencnt   length of current token                                  */
/*  tokenptr   address of current token                                 */
/*  character  character value of character token                       */
/*  number     binary value of numeric token                            */
/*  parameter  user supplied argument                                   */
/*                                                                      */
/*----------------------------------------------------------------------*/

int octal_number (options,stringcnt,stringptr,tokencnt,
                  tokenptr,character,number,parameter)
int  options,stringcnt,tokencnt,number,parameter;
char character,stringptr[],tokenptr[];
{
   numeric_value.type  = INTEGER;
   numeric_value.idata = number;

   if (debug_flag)
      {
      printf ("Oct Num:    %.*s\n",tokencnt,tokenptr);
      printf ("Value is    %d\n",numeric_value.idata);
      }

   return 1;
}


/*----------------------------------------------------------------------*/
/*  A Decimal Number Was Successfully Parsed                            */
/*                                                                      */
/*  Parameter  Description                                              */
/*                                                                      */
/*  options    processing option flags                                  */
/*  stringcnt  length of input string                                   */
/*  stringptr  address of input string                                  */
/*  tokencnt   length of current token                                  */
/*  tokenptr   address of current token                                 */
/*  character  character value of character token                       */
/*  number     binary value of numeric token                            */
/*  parameter  user supplied argument                                   */
/*                                                                      */
/*----------------------------------------------------------------------*/

int decimal_number (options,stringcnt,stringptr,tokencnt,
                    tokenptr,character,number,parameter)
int  options,stringcnt,tokencnt,number,parameter;
char character,stringptr[],tokenptr[];
{
   numeric_value.type  = INTEGER;
   numeric_value.idata = number;

   if (debug_flag)
      {
      printf ("Dec Num:    %.*s\n",tokencnt,tokenptr);
      printf ("Value is    %d\n",numeric_value.idata);
      }

   return 1;
}


/*----------------------------------------------------------------------*/
/*  A Hexadecimal Number Was Successfully Parsed                        */
/*                                                                      */
/*  Parameter  Description                                              */
/*                                                                      */
/*  options    processing option flags                                  */
/*  stringcnt  length of input string                                   */
/*  stringptr  address of input string                                  */
/*  tokencnt   length of current token                                  */
/*  tokenptr   address of current token                                 */
/*  character  character value of character token                       */
/*  number     binary value of numeric token                            */
/*  parameter  user supplied argument                                   */
/*                                                                      */
/*----------------------------------------------------------------------*/

int hexadecimal_number (options,stringcnt,stringptr,tokencnt,
                        tokenptr,character,number,parameter)
int  options,stringcnt,tokencnt,number,parameter;
char character,stringptr[],tokenptr[];
{
   numeric_value.type  = INTEGER;
   numeric_value.idata = number;

   if (debug_flag)
      {
      printf ("Hex Num:    %.*s\n",tokencnt,tokenptr);
      printf ("Value is    %d\n",numeric_value.idata);
      }

   return 1;
}
