1 /*****************************************************************************
4 ** POL - Problem Oriented Language
6 ** This is part of the CTSim program
7 ** Copyright (C) 1983-2000 Kevin Rosenberg
9 ** $Id: pol.cpp,v 1.3 2000/12/04 05:36:57 kevin Exp $
11 ** This program is free software; you can redistribute it and/or modify
12 ** it under the terms of the GNU General Public License (version 2) as
13 ** published by the Free Software Foundation.
15 ** This program is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ** GNU General Public License for more details.
20 ** You should have received a copy of the GNU General Public License
21 ** along with this program; if not, write to the Free Software
22 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 ******************************************************************************/
25 /*----------------------------------------------------------------------*/
31 #include "ctsupport.h"
35 static const int HASHSIZE=20;
38 /* Tables words stored with install() & found with lookup() */
39 static SYMBOL *skiptable[HASHSIZE]; /* words to ignore and skip */
40 static SYMBOL *cmdtable[HASHSIZE]; /* pol parameter commands */
41 static SYMBOL *usertable[HASHSIZE]; /* user defined symbols */
43 static struct token_st token; /* current token */
45 static struct metachar {
46 char eoc; /* end of command character */
47 char str; /* string delimiter */
48 char com; /* comment character */
49 char cmd; /* pol parameter command character */
50 char prg; /* program load character */
51 char con; /* continuation across newline character */
52 char out; /* character that delimits output to terminal */
53 char ter; /* character indicates insertion of input from terminal */
54 char inb; /* input from graphics device */
57 /* current pol state */
59 static struct pol_st {
60 char skipchars[MAXSKIPCHAR]; /* characters to skip */
61 int nl_eoc; /* TRUE if newline character ends a command */
62 int trace; /* TRUE if trace is on */
70 /* internal codes for pol commands */
88 static struct key cmdlist[] = {
99 { "nl_eoc",PC_NL_EOC,},
100 { "nl_neoc", PC_NL_NEOC,},
102 { "troff", PC_TROFF,},
107 #define NUMCMD (sizeof(cmdlist) / sizeof (struct key))
109 static int skiptok(char term[]);
110 static int pol_tok(struct token_st *token);
111 static void dumptok(struct token_st *token);
114 static int getpol_tok(struct token_st *token);
115 static int getcmd(void);
116 static int gettok (TOKEN *tok);
117 static void getblank(char *s, int toksiz);
118 static int getalpha(char *s, int toksiz);
119 static void getquote(char *qs, int toksiz);
120 static void getescape(char *s, int delim, int toksiz);
121 static int getnumber (char str[], int strsize, double *fnum, int *inum);
122 static void eatline(void);
123 static int type(int c);
124 static void inittable(SYMBOL *table[]);
125 static void freetable(SYMBOL *table[]);
126 static int hash(char *s);
127 static SYMBOL *lookup(SYMBOL *table[], char *s);
128 static SYMBOL *install(SYMBOL *table[], char *s, int def);
129 static void synerr(char *msg);
130 static int pol_getch(FILE *fp);
140 meta.con = AMPERSAND;
146 pol.skipchars[0] = EOS;
148 inittable (cmdtable); /* initialize symbol tables */
149 inittable (usertable);
150 inittable (skiptable);
152 for (unsigned int i = 0; i < NUMCMD; i++)
153 install (cmdtable, cmdlist[i].keyword, cmdlist[i].code);
155 token.ready = FALSE; /* no token read yet */
160 * char *w - word for pol to ignore and skip over in input
162 * pol_tok() compares all tokens to words given to this routine. If it finds it,
163 * it will immediately read another token.
167 pol_skpword (char *w)
169 if (install (skiptable, w, 0) == NULL)
170 sys_error (ERR_SEVERE, "Too many skip words defined");
175 * skip all characters that appear in string s
178 pol_skpchar (char *s)
180 strncpy (pol.skipchars, s, MAXSKIPCHAR);
183 /* pol_install (str, code)
185 * char *str - token string to install
186 * int code - code to return for token
188 * pol_tok() looks for these user defined tokens. If it finds one,
189 * it stores the tokens code in the token structure and returns TT_USERTOK
192 pol_install (char *str, int code)
194 if (install (usertable, str, code) == NULL)
196 synerr ("Out ot memory installing user tokens");
203 /* get_word - matches tokens on a letter by letter basis
205 * char *search - string to search for
206 * int nlet - maximum number of chars to search for match
210 pol_word (char *search, int nlet)
213 if (pol.trace == TRUE)
214 printf ("matching current token %s against word %s\n", token.tokstr, search);
216 if (strncasecmp (search, token.tokstr, nlet) == 0) {
223 /* pol_usertok (str,code)
224 * see if current token is a user defined token set with pol_install()
226 * char *str - token string as read from input
227 * int *code - returned code for user defined symbol
228 * return value - TRUE if current token has been user defined
229 * FALSE if current token is not user defined
232 pol_usertok (char *str, int *code)
236 if (pol.trace == TRUE)
237 printf ("checking if current token '%s' is user defined\n", token.tokstr);
239 if (token.type == TT_USERTOK) {
241 strcpy (str, token.tokstr);
250 /* isstring (s) - returns TRUE if current token is a string
252 * char *s - pointer to place to store token string
256 pol_string (char *str)
260 if (token.type == TT_STRING) {
261 strcpy (str, token.tokstr);
268 /* pol_integer - test for an integer
270 * int *n: returned integer value
271 * int typecode = TT_INT if accept only integer values
272 * = TT_REAL if accept both real and integer values
273 * int boundcode= TRUE if force to lie between boundries
274 * = FALSE can take any value it likes
275 * int bb1: lower bound
276 * int bb2: upper bound
279 pol_integer (int *n, int typecode, int boundcode, int bb1, int bb2)
283 if (pol.trace == TRUE)
284 printf ("checking if current token %s is an integer\n", token.tokstr);
286 if (token.type == TT_INT || token.type == TT_REAL) {
287 if (boundcode == TRUE) {
288 if (token.inum < bb1)
290 else if (token.inum > bb2)
304 pol_float (double *n, double typecode, double boundcode, double bb1, double bb2)
308 if (pol.trace == TRUE)
309 printf ("checking if current token %s is an floating point number\n", token.tokstr);
311 if (token.type == TT_INT || token.type == TT_REAL) {
312 if (boundcode == TRUE) {
313 if (token.fnum < bb1)
315 else if (token.fnum > bb2)
328 /*----------------------------------------------------------------------*/
329 /* pol_skip() - skip over any token except for end of command sequence */
331 /* returns TRUE if succesful skip */
332 /* returns FALSE if already at end of command or EOF */
333 /*----------------------------------------------------------------------*/
337 char term[5]; /* string of characters not to skip */
340 if (pol.nl_eoc == TRUE) {
346 return (skiptok (term));
349 void pol_reader(void)
351 while (pol_skip() == TRUE)
354 dumptok (&token); /* skip end of command token */
357 /* skiptok (term) - skip a token unless the first character of a token is
358 * in the string of terminators, term.
359 * char *term - string of termination characters, don't skip these characters
360 * skiptok() also does NOT skip TT_EOF
361 * returns (TRUE) if succesful skip of a token
362 * returns (FALSE) if didn't skip, read termination character or TT_EOF
366 skiptok (char term[])
370 if (token.type == TT_EOF
371 || (token.type == TT_SPECLCHAR && strchr(term, token.tokstr[0]) != NULL))
380 pol_tok (struct token_st *token)
382 if (token->ready == FALSE)
385 if (token->type == TT_EOF && pol_lookchar() != EOF)
387 return (token->type);
391 dumptok (struct token_st *token)
393 if (token->ready == FALSE)
395 token->ready = FALSE;
399 getpol_tok (struct token_st *token)
403 token->ready = FALSE;
407 if (token->type == TT_BLANK)
409 if (token->type == TT_SPECLCHAR) {
410 if (strchr(pol.skipchars, token->tokstr[0]) != NULL)
412 if (token->tokstr[0] == NEWLINE)
414 if (token->tokstr[0] == meta.cmd) {
418 if (token->tokstr[0] == meta.com) { /* skip comment */
422 if (token->tokstr[0] == meta.out) {
423 getescape(token->tokstr, meta.out, MAXTOK);
424 fputs (token->tokstr, stderr);
427 if (token->tokstr[0] == meta.con) { /* continuation across NEWLINE */
428 while (pol_lookchar() == BLANK || pol_lookchar() == TAB)
430 if (pol_lookchar() == NEWLINE)
433 if (token->tokstr[0] == meta.ter) { /* get input from terminal */
434 pol_usefile (P_USE_FILE, "");
437 return (token->type);
441 /* look for filler words */
443 if (lookup (skiptable, token->tokstr) != NULL) /* ignore words in skip table */
446 /* look for user defined symbols */
448 if ((sym = lookup (usertable, token->tokstr)) != NULL) {
449 token->type = TT_USERTOK;
450 token->code = sym->code;
454 if (pol.trace == TRUE)
455 printf ("Read token '%s', type = %d\n", token->tokstr, token->type);
457 return (token->type);
461 static int getcmd(void)
468 tt = getalpha (str, MAXTOK);
469 if (tt == TT_ERROR) {
470 synerr ("error in pol parameter command");
474 if ((cmd = lookup (cmdtable,str)) == NULL) {
475 synerr ("unrecognized command");
492 pol_usefile (P_USE_FILE, tok.tokstr);
504 printf("eoc = %c str = %c com = %c cmd = %c prg = %c\n",
505 meta.eoc, meta.str, meta.com, meta.cmd, meta.prg);
506 printf("con = %c out = %c ter = %c inb = %c\n",
507 meta.con, meta.out, meta.ter, meta.inb);
510 if (found == FALSE) {
512 if (tt != TT_SPECLCHAR) {
513 synerr("illegal command character");
518 meta.eoc = tok.tokstr[0];
521 meta.str = tok.tokstr[0];
524 meta.com = tok.tokstr[0];
527 meta.cmd = tok.tokstr[0];
530 meta.prg = tok.tokstr[0];
533 meta.con = tok.tokstr[0];
536 meta.out = tok.tokstr[0];
539 meta.ter = tok.tokstr[0];
542 meta.inb = tok.tokstr[0];
545 printf("command not implemented\n");
547 } /* switch (tok->type) */
548 } /* if (found == FALSE) */
549 pol_reader(); /* clean up command */
550 } /* if legal command */
562 int toksiz = MAXTOK; /* maximum length of token string */
564 while ((c = pol_inchar()) == BLANK || c == TAB)
574 if (c == BLANK || c == TAB) { /* skip white space */
575 getblank(tok->tokstr, toksiz);
577 } else if (toktype == LETTER) {
578 toktype = getalpha (tok->tokstr, toksiz);
579 } else if (c == meta.str) { /* quoted string */
580 getquote (tok->tokstr, toksiz);
582 } else if (type(c) == DIGIT || c == PLUS || c == HYPHEN || c == PERIOD) {
583 toktype = getnumber (tok->tokstr, toksiz, &fnum, &inum);
584 } else if (c == EOF) {
585 tok->tokstr[0] = EOS;
590 tok->tokstr[1] = EOS;
591 toktype = TT_SPECLCHAR;
596 if (tok->type == TT_REAL || tok->type == TT_INT) {
609 getblank (char *s, int toksiz)
613 while ((c = pol_inchar()) == BLANK || c == TAB)
623 getalpha (char *s, int toksiz)
625 int i, chartype, alphatype;
627 if (type(pol_lookchar()) != LETTER) {
632 alphatype = TT_ALPHA;
633 for (i = 0; i < toksiz; i++) { /* get alphanumeric token */
635 chartype = type (s[i]);
636 if (chartype != LETTER && chartype != DIGIT)
638 if (chartype == DIGIT)
639 alphatype = TT_ALPNUM;
644 synerr("token too long.");
646 s[i] = EOS; /* terminate token */
651 /* getquote - get quoted string from file */
652 /* have already gotten delimiter in qs[0] */
654 getquote (char *qs, int toksiz)
658 delim = pol_inchar(); /* char = delimiter */
659 getescape(qs, delim, toksiz);
664 getescape ( /* reads up to delim */
672 for (i = 0; (c = pol_inchar()) != delim; i++) {
674 synerr ("Missing closing delimiter.");
678 synerr("string too long.");
683 synerr("end of file inside quotation");
685 } else if (c == BSLASH) { /* escape character */
687 c = pol_inchar(); /* get escaped character */
695 gettext (char *str, int lim)
699 while ((c = pol_inchar()) == BLANK || c == TAB)
703 for (i = 0; i < lim && (c = pol_inchar()) != EOF && c != NEWLINE; i++)
709 /*----------------------------------------------*/
710 /* Get a number for gettok() */
711 /*----------------------------------------------*/
715 char str[], /* string to return token in */
716 int strsize, /* maximum length of token string */
717 double *fnum, /* floating point value of number read */
718 int *inum /* integer value of number read */
722 double sign, whole, frac, powerof10, exp, expsign;
726 isSigned = FALSE; /* TRUE if number prefixed by '+' or '-' */
736 } else if (c == PLUS) {
740 } else if (c == PERIOD) {
741 if (type(pol_lookchar()) != DIGIT) {
744 return (TT_SPECLCHAR);
746 pol_ungetch (PERIOD);
747 } else if (type(c) != DIGIT) {
753 if (isSigned == TRUE) {
756 pol_inchar(); /* get period */
757 c = pol_lookchar(); /* look at character past period */
758 pol_ungetch (PERIOD); /* put back period */
759 if (type(c) != DIGIT) {
761 return (TT_SPECLCHAR);
763 } else if (type (c) != DIGIT) {
765 return (TT_SPECLCHAR);
770 while (type(c = pol_inchar()) == DIGIT) {
773 whole = 10.0 * whole + (c - '0');
775 pol_ungetch (c); /* put back non-numeric character */
777 if (c != PERIOD && tolower(c) != 'e') {
779 *fnum = whole * sign;
782 else if (*fnum > MAX_INT)
789 if (pol_lookchar() == PERIOD) {
798 while (type(c = pol_inchar()) == DIGIT) {
801 frac += (double) (c - '0') / powerof10;
809 if (tolower(c) != 'e')
814 if ((c = pol_inchar()) == PLUS) {
818 } else if (c == HYPHEN) {
822 } else if (type(c) != DIGIT) {
823 --sp; /* erase 'e' */
831 while (type(c = pol_inchar()) == DIGIT) {
834 exp = 10 * exp + (c - '0');
841 *fnum = sign * (whole + frac) * pow (10.0, expsign * exp);
844 else if (*fnum > MAX_INT)
862 type ( /* return type of ASCII character */
866 if (isalpha(c) || c == UNDERLIN)
874 /*----------------------------------------------------------------------*/
876 /* hash table routines. Kernighan & Ritchie */
878 /*----------------------------------------------------------------------*/
885 inittable (SYMBOL *table[])
889 for (i = 0; i < HASHSIZE; i++)
894 * free all memory allocated to table, then clear table
898 freetable (SYMBOL *table[])
903 for (i = 0; i < HASHSIZE; i++) {
915 hash ( /* form hash value of string s */
921 for (hashval = 0; *s != EOS; )
923 return (hashval % HASHSIZE);
926 /* Look for s in hash table */
928 lookup ( SYMBOL *table[], char *s )
931 SYMBOL *found = NULL;
933 for (np = table[hash(s)]; np != NULL; np = np->next)
934 if (strcasecmp(s, np->name) == 0) {
935 found = np; /* found it */
943 install (SYMBOL *table[], char *name, int def)
945 static char installerr[] = "install: out of memory";
949 if ((np = lookup (table, name)) == NULL) { /* not found */
950 np = (SYMBOL *) malloc (sizeof(*np));
955 if ((np->name = strdup(name)) == NULL) {
959 str_lower (np->name);
961 hashval = hash(np->name);
962 np->next = table[hashval];
964 } else /* already there */
969 /*----------------------------------------------------------------------*/
971 /*----------------------------------------------------------------------*/
975 static int currentf = -1; /* pointer to current fp */
976 static FILE *filep[MAXFILE]; /* == NULL for string input */
977 static char *fname[MAXFILE]; /* pointer to filename */
979 static char inputline[MAXLINE]; /* current input line */
980 static int lineptr; /* current position in inputline */
985 fputs (fname[currentf], stderr);
987 fputc (NEWLINE, stderr);
990 /*----------------------------------------------------------------------*/
992 /*----------------------------------------------------------------------*/
995 static int bp = 0; /* pointer to next free position */
996 static int buf[BUFSIZE]; /* pushed back input characters */
998 /* pol_usefile - set source of POL input
1000 * int source - source of input
1001 * P_USE_STR - have POL use strings as input
1002 * P_USE_FILE - use file. filename is in str
1007 pol_usefile (int source, char *fn)
1012 if (currentf >= MAXFILE) {
1014 synerr ("files nested too deeply");
1018 bp = 0; /* clear any pushed back input */
1020 if (source == P_USE_STR) {
1021 filep[currentf] = NULL;
1022 } else if (source == P_USE_FILE) {
1023 if (fn == NULL || strlen(fn) == 0) {
1025 } else if ((fp = fopen(fn, "r")) == NULL) {
1027 synerr ("can't open file");
1030 filep[currentf] = fp;
1031 fname[currentf] = strdup (fn);
1035 void pol_closefile(void)
1037 if (currentf >= 0) {
1038 if (filep[currentf] != NULL)
1039 fclose (filep[currentf]);
1044 /*-----------------------------*/
1045 /* Lowest Level Input Routines */
1046 /*-----------------------------*/
1049 int pol_lookchar(void)
1058 int pol_inchar(void)
1065 while (currentf >= 0 && (c = pol_getch(filep[currentf])) == EOF && filep[currentf] != NULL) {
1072 /*--------------------------------------------------------------*/
1073 /* getch - get a (possibly pushed back) character */
1074 /* if fp == NULL, then get character from inputline */
1075 /*--------------------------------------------------------------*/
1078 pol_getch (FILE *fp)
1086 if ((c = inputline[lineptr]) == EOS)
1098 /* push character back on input */
1103 sys_error (ERR_SEVERE, "too many characters pushed back [pol_ungetch]");
1110 get_inputline (FILE *fp)
1114 if (fgets (inputline, MAXLINE, fp) == NULL)
1121 set_inputline (char *line)
1125 strncpy (inputline, line, MAXLINE);