+/*****************************************************************************
+** This is part of the CTSim program
+** Copyright (C) 1983-2000 Kevin Rosenberg
+**
+** $Id: pol.h,v 1.7 2000/12/27 03:16:02 kevin Exp $
+**
+** This program is free software; you can redistribute it and/or modify
+** it under the terms of the GNU General Public License (version 2) as
+** published by the Free Software Foundation.
+**
+** This program is distributed in the hope that it will be useful,
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+** GNU General Public License for more details.
+**
+** You should have received a copy of the GNU General Public License
+** along with this program; if not, write to the Free Software
+** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+******************************************************************************/
#ifndef __H_POL
#define __H_POL
-/* codes for pol_usefile */
-#define P_USE_STR 1 /* use string as input source */
-#define P_USE_FILE 2 /* use file as input source */
-
-/* codes for pol_int and pol_float */
-/* if in reject catagory, get new number from terminal */
-
-#define P_FLTINT 1 /* get a real or integer number */
-#define P_BFLTINT 2 /* get a real or integer number, clip against bounds */
-#define P_CBFLTINT 3 /* get real or int, reject if outside bounds */
-
-#define P_FLT 4 /* get a real number */
-#define P_BFLT 5 /* get a real, clip against bounds */
-#define P_CBFLT 6 /* get a floating, reject if outside bounds */
-
-#define P_INT 7 /* get a integer number */
-#define P_BINT 8 /* get a integer, clip against bounds */
-#define P_CBINT 9 /* get a integer, reject if outside bounds */
-
-#define LETTER 'a'
-#define DIGIT '0'
-
-#define MAXTOK 200 /* maximum length of a token */
-#define MAXLINE 255
-#define MAXIDENT 20
-#define MAXSKIPWORD 20
-#define MAXSKIPCHAR 20
-
-#define MIN_INT -32768
-#define MAX_INT 32767
-
-/* token types */
-
-#define TT_STRING 1 /* string token */
-#define TT_INT 2 /* integer token */
-#define TT_REAL 3 /* floating point token */
-#define TT_ALPHA 4 /* alphabetic token */
-#define TT_ALPNUM 5 /* alphanumeric token */
-#define TT_NUMALPHA 6
-#define TT_SPECLCHAR 7
-
-#define TT_EOF 8 /* end of file reached */
-#define TT_ERROR 9 /* error in token, caused by call to wrong */
- /* type of token reader */
-#define TT_BLANK 10 /* white space token. pol_tok() skips these */
-#define TT_USERTOK 11 /* user defined token */
-
-struct symlist {
- char *name;
- int code;
- struct symlist *next;
-};
-
-typedef struct symlist SYMBOL;
-
-struct token_st {
- int ready; /* TRUE if token is ready */
- char tokstr[MAXTOK+1]; /* token string */
- int type; /* type of token 'TT_' */
- int code; /* holds code for user defined tokens */
- double fnum; /* real value of token */
- int inum; /* integer value of token */
+class POL {
+
+ public:
+
+ // codes for pol_usefile \r
+ enum {\r
+ P_USE_STR = 1, // use string as input source \r
+ P_USE_FILE, // use file as input source \r
+ };\r
+\r
+ \r
+ POL();
+ ~POL();
+
+ void init ();
+ void skpword (char *w);
+ void skpchar (char *s);
+ int installKeyword (char *str, int code);
+ int word (char *search, int nlet);
+ int usertok (char *str, int *code);
+ int string (char *str);
+ int integer (int *n, int typecode, int boundcode, int bb1, int bb2);
+ bool readfloat (double *n, double typecode, double boundcode, double bb1, double bb2);
+ int skip ();
+ void reader ();
+ void gettext (char *str, int lim);
+ void usefile (int source, char *fn);
+ void closefile ();
+ int lookchar ();
+ int inchar ();
+ void ungetch (int c);
+ int get_inputline (FILE *fp);
+ void set_inputline (const char* const line);
+
+\r
+ enum {\r
+ MAXTOK = 200, // maximum length of a token \r
+ MAXLINE = 1024, // maximum line length\r
+ MAXIDENT = 20,\r
+ MAXSKIPWORD = 20,\r
+ MAXSKIPCHAR = 20,\r
+ MIN_INT = -2000000000,\r
+ MAX_INT = 2000000000,\r
+ };\r
+ \r
+ // token types \r
+ enum {\r
+ TT_STRING = 1, // string token \r
+ TT_INT, // integer token \r
+ TT_REAL, // floating point token \r
+ TT_ALPHA, // alphabetic token \r
+ TT_ALPNUM, // alphanumeric token \r
+ TT_NUMALPHA,\r
+ TT_SPECLCHAR,\r
+ TT_EOF, // end of file reached \r
+ TT_ERROR, // error in token, caused by call to wrong type of token reader \r
+ TT_BLANK, // white space token. pol_tok() skips these \r
+ TT_USERTOK, // user defined token \r
+ };\r
+\r
+ \r
+private:\r
+ \r
+ // codes for pol_int and pol_float \r
+ // if in reject catagory, get new number from terminal \r
+ enum {\r
+ P_FLTINT = 1, // get a real or integer number \r
+ P_BFLTINT, // get a real or integer number, clip against bounds \r
+ P_CBFLTINT, // get real or int, reject if outside bounds \r
+ P_FLT, // get a real number \r
+ P_BFLT, // get a real, clip against bounds \r
+ P_CBFLT, // get a floating, reject if outside bounds \r
+ P_INT, // get a integer number \r
+ P_BINT, // get a integer, clip against bounds \r
+ P_CBINT, // get a integer, reject if outside bounds \r
+ };\r
+ \r
+#define LETTER 'a'\r
+#define DIGIT '0'\r
+ \r
+ \r
+// typedef std::map<std::string,int> KeywordCodeList;\r
+ \r
+ struct symlist {\r
+ char *name;\r
+ int code;\r
+ struct symlist *next;\r
+ };\r
+ typedef struct symlist SYMBOL;\r
+ \r
+ struct token_st {\r
+ int ready; // TRUE if token is ready \r
+ // std::string tokstr; // token string \r
+ char tokstr[MAXTOK+1];\r
+ int type; // type of token 'TT_' \r
+ int code; // holds code for user defined tokens \r
+ double fnum; // real value of token \r
+ int inum; // integer value of token \r
+ };\r
+ typedef struct token_st TOKEN;\r
+ \r
+ \r
+ struct token_st token; // current token \r
+ enum {\r
+ HASHSIZE = 100,\r
+ };\r
+ \r
+ // Tables words stored with install() & found with lookup() \r
+ SYMBOL *skiptable[HASHSIZE]; // words to ignore and skip \r
+ SYMBOL *cmdtable[HASHSIZE]; // pol parameter commands \r
+ SYMBOL *usertable[HASHSIZE]; // user defined symbols \r
+ \r
+ struct metachar {\r
+ char eoc; /* end of command character */\r
+ char str; /* string delimiter */\r
+ char com; /* comment character */\r
+ char cmd; /* pol parameter command character */\r
+ char prg; /* program load character */\r
+ char con; /* continuation across newline character */\r
+ char out; /* character that delimits output to terminal */\r
+ char ter; /* character indicates insertion of input from terminal */\r
+ char inb; /* input from graphics device */\r
+ } meta;\r
+ \r
+ \r
+ // current pol state \r
+ struct pol_st {\r
+ char skipchars[MAXSKIPCHAR]; // characters to skip \r
+ int nl_eoc; // TRUE if newline character ends a command \r
+ int trace; // TRUE if trace is on \r
+ };\r
+ \r
+ struct pol_st pol;\r
+ \r
+ struct KeywordCodeList {\r
+ char *keyword;\r
+ int code;\r
+ };\r
+ \r
+ static const struct KeywordCodeList cmdlist[];\r
+ static const int NUMCMD;\r
+ \r
+ // Internal codes for pol commands \r
+ enum {\r
+ PC_EOC = 1,\r
+ PC_STR,\r
+ PC_COM,\r
+ PC_CMD,\r
+ PC_PRG,\r
+ PC_CON, \r
+ PC_OUT,\r
+ PC_TER,\r
+ PC_INB, \r
+ PC_NL_EOC,\r
+ PC_NL_NEOC,\r
+ PC_TRON,\r
+ PC_TROFF,\r
+ PC_FILE,\r
+ PC_DUMP,\r
+ };\r
+ \r
+ enum {\r
+ MAXFILE = 8,\r
+ };\r
+ \r
+ int currentf; /* pointer to current fp */\r
+ FILE *filep[MAXFILE]; /* == NULL for string input */\r
+ char *fname[MAXFILE]; /* pointer to filename */\r
+ \r
+ char inputline[MAXLINE]; /* current input line */\r
+ int lineptr; /* current position in inputline */\r
+ \r
+ enum {\r
+ BUFSIZE = 100,\r
+ };\r
+ int bp; // pointer to next free position \r
+ int buf[BUFSIZE]; // pushed back input characters \r
+ \r
+ int skiptok(char term[]);\r
+ int tok(struct token_st *token);\r
+ void dumptok(struct token_st *token);\r
+ \r
+ \r
+ int getpol_tok(struct token_st *token);\r
+ int getcmd();\r
+ int gettok (TOKEN *tok);\r
+ void getblank(char *s, int toksiz);\r
+ int getalpha(char *s, int toksiz);\r
+ void getquote(char *qs, int toksiz);\r
+ void getescape(char *s, int delim, int toksiz);\r
+ int getnumber (char str[], int strsize, double *fnum, int *inum);\r
+ void eatline();\r
+ int type(int c);\r
+ void inittable(SYMBOL *table[]);\r
+ void freetable(SYMBOL *table[]);\r
+ int hash(char *s);\r
+ SYMBOL *lookup(SYMBOL *table[], char *s);\r
+ SYMBOL *install(SYMBOL *table[], char *s, int def);\r
+ int getch(FILE *fp);\r
+ \r
};
-typedef struct token_st TOKEN;
-
-/* pol.c */
-void pol_init(void);
-void pol_skpword(char *w);
-void pol_skpchar(char *s);
-int pol_install(char *str, int code);
-int pol_word(char *search, int nlet);
-int pol_usertok(char *str, int *code);
-int pol_string(char *str);
-int pol_integer(int *n, int typecode, int boundcode, int bb1, int bb2);
-int pol_float(double *n, double typecode, double boundcode, double bb1, double bb2);
-int pol_skip(void);
-void pol_reader(void);
-void gettext(char *str, int lim);
-void pol_usefile(int source, char *fn);
-void pol_closefile(void);
-int pol_lookchar(void);
-int pol_inchar(void);
-void pol_ungetch(int c);
-int get_inputline(FILE *fp);
-void set_inputline(char *line);
-
#endif
+