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