4894425d02b9b3d4f2995caac76a8512d182b2a1
[ctsim.git] / include / pol.h
1 /*****************************************************************************
2 **      Name:         pol.h
3 **      Purpose:      Header file for Problem Oriented Language Class
4 **      Programmer:   Kevin Rosenberg
5 **      Date Started: 1984
6 **
7 **  This is part of the CTSim program
8 **  Copyright (c) 1983-2001 Kevin Rosenberg
9 **
10 **  $Id: pol.h,v 1.12 2001/01/28 19:10:18 kevin Exp $
11 **
12 **  This program is free software; you can redistribute it and/or modify
13 **  it under the terms of the GNU General Public License (version 2) as
14 **  published by the Free Software Foundation.
15 **
16 **  This program is distributed in the hope that it will be useful,
17 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 **  GNU General Public License for more details.
20 **
21 **  You should have received a copy of the GNU General Public License
22 **  along with this program; if not, write to the Free Software
23 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24 ******************************************************************************/
25 #ifndef __H_POL
26 #define __H_POL
27
28 #include "hashtable.h"
29 #include <stack>
30
31 class POL {
32   
33   public:
34     
35     // codes for pol_usefile 
36     enum {
37       P_USE_STR = 1,            // use string as input source 
38       P_USE_FILE,                 // use file as input source 
39     };
40
41   
42     POL();
43     ~POL();
44     
45     void init ();
46     void addSkipWord (const char* const w);
47     void addSkipChar (int c);
48     void addKeyword (const char* const str, int code);
49     bool readWord (char *search, int nlet);
50     bool readUserToken (char *str, int *code);
51     bool readString (char *str);
52     bool readInteger (int *n, int typecode, bool boundcode, int bb1, int bb2);
53     bool readFloat (double *n, double typecode, bool boundcode, double bb1, double bb2);
54     bool skipTokens ();
55     void reader ();
56     bool readText (char *str, int lim);
57     void usefile (int source, char *fn);
58     void closefile ();
59     int lookchar ();
60     int inchar ();
61     void ungetch (int c);
62     int get_inputline (FILE *fp);
63     void set_inputline (const char* const line);
64     
65
66   enum {
67     MAXTOK = 200,               // maximum length of a token 
68       MAXLINE = 1024,       // maximum line length
69       MAXIDENT = 20,
70       MAXSKIPWORD = 20,
71       MAXSKIPCHAR = 20,
72       MIN_INT = -2000000000,
73       MAX_INT =  2000000000,
74   };
75   
76   // token types 
77   enum {
78     TT_STRING = 1,  // string token 
79       TT_INT,         // integer token 
80       TT_REAL,                  // floating point token 
81       TT_ALPHA,                 // alphabetic token 
82       TT_ALPNUM,                // alphanumeric token 
83       TT_NUMALPHA,
84       TT_SPECLCHAR,
85       TT_EOF,         // end of file reached 
86       TT_ERROR,       // error in token, caused by call to wrong type of token reader 
87       TT_BLANK,        // white space token.  pol_tok() skips these 
88       TT_USERTOK,     // user defined token 
89   };
90
91   
92 private:
93   
94   // codes for pol_int and pol_float 
95   // if in reject catagory, get new number from terminal 
96   enum {
97     P_FLTINT = 1,        // get a real or integer number 
98       P_BFLTINT,     // get a real or integer number, clip against bounds 
99       P_CBFLTINT,        // get real or int, reject if outside bounds 
100       P_FLT,               // get a real number 
101       P_BFLT,        // get a real, clip against bounds 
102       P_CBFLT,       // get a floating, reject if outside bounds 
103       P_INT,         // get a integer number 
104       P_BINT,        // get a integer, clip against bounds 
105       P_CBINT,       // get a integer, reject if outside bounds 
106   };
107   
108 #define LETTER   'a'
109 #define DIGIT    '0'
110   
111   
112 //  typedef std::map<std::string,int> KeywordCodeList;
113   
114   struct token_st {
115     int ready;                          // TRUE if token is ready 
116   //  std::string tokstr;       // token string 
117     char tokstr[MAXTOK+1];
118     int type;                             // type of token 'TT_' 
119     int code;                             // holds code for user defined tokens 
120     double fnum;                        // real value of token 
121     int inum;                       // integer value of token 
122   };
123   typedef struct token_st TOKEN;
124   struct token_st token;                                // current token 
125    
126   // Tables words stored with install() & found with lookup() 
127   KeywordCodeHashTable skiptable;               // words to ignore and skip 
128   KeywordCodeHashTable cmdtable;                // pol parameter commands 
129   KeywordCodeHashTable usertable;               // user defined symbols 
130   
131   struct metachar {
132     char eoc;           /* end of command character */
133     char str;           /* string delimiter */
134     char com;           /* comment character */
135     char cmd;           /* pol parameter command character */
136     char prg;           /* program load character */
137     char con;           /* continuation across newline character */
138     char out;           /* character that delimits output to terminal */
139     char ter;           /* character indicates insertion of input from terminal */
140     char inb;           /* input from graphics device */
141   } meta;
142   
143   
144   char m_szSkipChars [MAXSKIPCHAR]; // characters to skip
145   bool m_bTrace;
146   bool m_bNewlineIsEOC;
147   
148   struct KeywordCodeList {
149     char *keyword;
150     int  code;
151   };
152   
153   static const struct KeywordCodeList cmdlist[];
154   static const unsigned int NUMCMD;
155   
156   // Internal codes for pol commands 
157   enum {
158     PC_EOC = 1,
159       PC_STR,
160       PC_COM,
161       PC_CMD,
162       PC_PRG,
163       PC_CON,   
164       PC_OUT,
165       PC_TER,
166       PC_INB,      
167       PC_NL_EOC,
168       PC_NL_NEOC,
169       PC_TRON,
170       PC_TROFF,
171       PC_FILE,
172       PC_DUMP,
173   };
174   
175   enum {
176     INPUT_STREAM = 1,
177     INPUT_FILE,
178     INPUT_STRING,
179   };
180
181   int m_iInputType;
182   std::istream* m_pInputStream;
183   FILE* m_pInputFile;
184   char* m_pszInputString;
185
186   enum {
187     MAXFILE = 8,
188   };
189   
190   int currentf;         /* pointer to current fp */
191   FILE *filep[MAXFILE];         /* == NULL for string input */
192   char *fname[MAXFILE];         /* pointer to filename */
193   
194   char inputline[MAXLINE];              /* current input line */
195   int lineptr;                  /* current position in inputline */
196   
197   std::stack<int> m_stackPushBackInput;
198
199   bool skipSingleToken (char term[]);
200   int tok (struct token_st *token);
201   void dumptok (struct token_st *token);
202   
203   
204   int getpol_tok (struct token_st *token);
205   int getcmd ();
206   int gettok (TOKEN *tok);
207   void getblank (char *s, int toksiz);
208   int getalpha (char *s, int toksiz);
209   void getquote (char *qs, int toksiz);
210   void getescape (char *s, int delim, int toksiz);
211   int getnumber (char str[], int strsize, double *fnum, int *inum);
212   void eatline ();
213   int type (int c);
214   int getch (FILE *fp);
215   
216 };
217
218 #endif
219