calc.h

00001 /* Small Calculator example program
00002    Copyright (C) 1999, 2000, 2002 Free Software Foundation, Inc.
00003    Written by Stephane Carrez (stcarrez@nerim.fr)       
00004 
00005 This file is free software; you can redistribute it and/or modify it
00006 under the terms of the GNU General Public License as published by the
00007 Free Software Foundation; either version 2, or (at your option) any
00008 later version.
00009 
00010 In addition to the permissions in the GNU General Public License, the
00011 Free Software Foundation gives you unlimited permission to link the
00012 compiled version of this file with other programs, and to distribute
00013 those programs without any restriction coming from the use of this
00014 file.  (The General Public License restrictions do apply in other
00015 respects; for example, they cover modification of the file, and
00016 distribution when not linked into another program.)
00017 
00018 This file is distributed in the hope that it will be useful, but
00019 WITHOUT ANY WARRANTY; without even the implied warranty of
00020 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00021 General Public License for more details.
00022 
00023 You should have received a copy of the GNU General Public License
00024 along with this program; see the file COPYING.  If not, write to
00025 the Free Software Foundation, 59 Temple Place - Suite 330,
00026 Boston, MA 02111-1307, USA.  */
00027 
00028 #ifndef _CALC_H
00029 #define _CALC_H
00030 
00031 #include <sys/param.h>
00032 #include <imath.h>
00033 
00034 #define MAX_VALUES 32
00035 
00036 #if !defined(VALUE_16) && !defined(VALUE_32) && !defined(VALUE_64)
00037 # if TEXT_SIZE > 16384
00038 #  define VALUE_64
00039 # elif TEXT_SIZE > 6144
00040 #  define VALUE_32
00041 # else
00042 #  define VALUE_16
00043 # endif
00044 #endif
00045 
00046 /* Select the size of integers used by the calculator.  */
00047 #ifdef VALUE_16
00048 typedef short value_t;
00049 typedef int vavalue_t;
00050 #define HEX_CVT_MASK 0x0fff
00051 #define calc_sqrt lsqrt
00052 #elif defined(VALUE_32)
00053 typedef long value_t;
00054 typedef value_t vavalue_t;
00055 #define HEX_CVT_MASK 0x0fffffffL
00056 #define calc_sqrt lsqrt
00057 #elif defined(VALUE_64)
00058 typedef long long value_t;
00059 typedef value_t vavalue_t;
00060 #define HEX_CVT_MASK 0x0fffffffffffffffLL
00061 #define calc_sqrt lsqrt64
00062 #endif
00063 
00064 /* List of operations/commands supported by the tool.  */
00065 typedef enum op_type 
00066 {
00067   OP_ADD,
00068   OP_SUB,
00069   OP_MUL,
00070   OP_DIV,
00071   OP_MOD,
00072   OP_AND,
00073   OP_OR,
00074   OP_XOR,
00075   OP_NOT,
00076   OP_NEG,
00077   OP_SQRT,
00078   OP_QUIT,
00079   OP_LIST,
00080   OP_NUMBER,
00081   OP_DEC,
00082   OP_HEX,
00083   OP_HELP
00084 } op_type;
00085 
00086 typedef struct command
00087 {
00088   const char* name;
00089   op_type type;
00090   const char* help;
00091 } command;
00092 
00093 /* Definition of the format that we must use to print the value.  */
00094 typedef enum print_mode
00095 {
00096   PRINT_DEC,
00097   PRINT_HEX,
00098   PRINT_OCT,
00099   PRINT_BIN
00100 } print_mode;
00101 
00102 /* The stack of values.  The operations operate on the stack.  They pop
00103    one or two values and push the result on the stack (like some HP
00104    calculators).  */
00105 typedef struct value_stack 
00106 {
00107   value_t* values;
00108   int top;
00109   int max;
00110   print_mode mode;
00111 } value_stack_t;
00112 
00113 /* Stack methods.  */
00114 extern int     push_value  (value_stack_t* stack, value_t value);
00115 extern value_t pop_value   (value_stack_t* stack);
00116 extern int     operation   (value_stack_t* stack, enum op_type op);
00117 extern void    print_value (value_stack_t* stack, print_mode mode, int which);
00118 extern int     calc_loop   (value_stack_t* stack);
00119 
00120 
00121 #ifdef __linux__
00122 inline void
00123 serial_print (char* p)
00124 {
00125   write(1, p, strlen(p));
00126 }
00127 inline int
00128 serial_recv ()
00129 {
00130   char c;
00131   read (0, &c, 1);
00132   return c;
00133 }
00134 inline int
00135 serial_init ()
00136 {
00137   return 0;
00138 }
00139 #else
00140 
00141 #include <sys/sio.h>
00142 
00143 extern void print (const char* msg);
00144 
00145 inline void
00146 print (const char* msg)
00147 {
00148   serial_print (msg);
00149 }
00150 
00151 #endif
00152 
00153 #endif