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.2 2000/07/31 14:48:35 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 /*----------------------------------------------------------------------*/
30 #include "ctsupport.h"
34 static const int HASHSIZE=20;
37 /* Tables words stored with install() & found with lookup() */
38 static SYMBOL *skiptable[HASHSIZE]; /* words to ignore and skip */
39 static SYMBOL *cmdtable[HASHSIZE]; /* pol parameter commands */
40 static SYMBOL *usertable[HASHSIZE]; /* user defined symbols */
42 static struct token_st token; /* current token */
44 static struct metachar {
45 char eoc; /* end of command character */
46 char str; /* string delimiter */
47 char com; /* comment character */
48 char cmd; /* pol parameter command character */
49 char prg; /* program load character */
50 char con; /* continuation across newline character */
51 char out; /* character that delimits output to terminal */
52 char ter; /* character indicates insertion of input from terminal */
53 char inb; /* input from graphics device */
56 /* current pol state */
58 static struct pol_st {
59 char skipchars[MAXSKIPCHAR]; /* characters to skip */
60 int nl_eoc; /* TRUE if newline character ends a command */
61 int trace; /* TRUE if trace is on */
69 /* internal codes for pol commands */
87 static struct key cmdlist[] = {
98 { "nl_eoc",PC_NL_EOC,},
99 { "nl_neoc", PC_NL_NEOC,},
101 { "troff", PC_TROFF,},
106 #define NUMCMD (sizeof(cmdlist) / sizeof (struct key))
108 static int skiptok(char term[]);
109 static int pol_tok(struct token_st *token);
110 static void dumptok(struct token_st *token);
113 static int getpol_tok(struct token_st *token);
114 static int getcmd(void);
115 static int gettok (TOKEN *tok);
116 static void getblank(char *s, int toksiz);
117 static int getalpha(char *s, int toksiz);
118 static void getquote(char *qs, int toksiz);
119 static void getescape(char *s, int delim, int toksiz);
120 static int getnumber (char str[], int strsize, double *fnum, int *inum);
121 static void eatline(void);
122 static int type(int c);
123 static void inittable(SYMBOL *table[]);
124 static void freetable(SYMBOL *table[]);
125 static int hash(char *s);
126 static SYMBOL *lookup(SYMBOL *table[], char *s);
127 static SYMBOL *install(SYMBOL *table[], char *s, int def);
128 static void synerr(char *msg);
129 static int pol_getch(FILE *fp);
139 meta.con = AMPERSAND;
145 pol.skipchars[0] = EOS;
147 inittable (cmdtable); /* initialize symbol tables */
148 inittable (usertable);
149 inittable (skiptable);
151 for (unsigned int i = 0; i < NUMCMD; i++)
152 install (cmdtable, cmdlist[i].keyword, cmdlist[i].code);
154 token.ready = FALSE; /* no token read yet */
159 * char *w - word for pol to ignore and skip over in input
161 * pol_tok() compares all tokens to words given to this routine. If it finds it,
162 * it will immediately read another token.
166 pol_skpword (char *w)
168 if (install (skiptable, w, 0) == NULL)
169 sys_error (ERR_SEVERE, "Too many skip words defined");
174 * skip all characters that appear in string s
177 pol_skpchar (char *s)
179 strncpy (pol.skipchars, s, MAXSKIPCHAR);
182 /* pol_install (str, code)
184 * char *str - token string to install
185 * int code - code to return for token
187 * pol_tok() looks for these user defined tokens. If it finds one,
188 * it stores the tokens code in the token structure and returns TT_USERTOK
191 pol_install (char *str, int code)
193 if (install (usertable, str, code) == NULL)
195 synerr ("Out ot memory installing user tokens");
202 /* get_word - matches tokens on a letter by letter basis
204 * char *search - string to search for
205 * int nlet - maximum number of chars to search for match
209 pol_word (char *search, int nlet)
212 if (pol.trace == TRUE)
213 printf ("matching current token %s against word %s\n", token.tokstr, search);
215 if (strncasecmp (search, token.tokstr, nlet) == 0) {
222 /* pol_usertok (str,code)
223 * see if current token is a user defined token set with pol_install()
225 * char *str - token string as read from input
226 * int *code - returned code for user defined symbol
227 * return value - TRUE if current token has been user defined
228 * FALSE if current token is not user defined
231 pol_usertok (char *str, int *code)
235 if (pol.trace == TRUE)
236 printf ("checking if current token '%s' is user defined\n", token.tokstr);
238 if (token.type == TT_USERTOK) {
240 strcpy (str, token.tokstr);
249 /* isstring (s) - returns TRUE if current token is a string
251 * char *s - pointer to place to store token string
255 pol_string (char *str)
259 if (token.type == TT_STRING) {
260 strcpy (str, token.tokstr);
267 /* pol_integer - test for an integer
269 * int *n: returned integer value
270 * int typecode = TT_INT if accept only integer values
271 * = TT_REAL if accept both real and integer values
272 * int boundcode= TRUE if force to lie between boundries
273 * = FALSE can take any value it likes
274 * int bb1: lower bound
275 * int bb2: upper bound
278 pol_integer (int *n, int typecode, int boundcode, int bb1, int bb2)
282 if (pol.trace == TRUE)
283 printf ("checking if current token %s is an integer\n", token.tokstr);
285 if (token.type == TT_INT || token.type == TT_REAL) {
286 if (boundcode == TRUE) {
287 if (token.inum < bb1)
289 else if (token.inum > bb2)
303 pol_float (double *n, double typecode, double boundcode, double bb1, double bb2)
307 if (pol.trace == TRUE)
308 printf ("checking if current token %s is an floating point number\n", token.tokstr);
310 if (token.type == TT_INT || token.type == TT_REAL) {
311 if (boundcode == TRUE) {
312 if (token.fnum < bb1)
314 else if (token.fnum > bb2)
327 /*----------------------------------------------------------------------*/
328 /* pol_skip() - skip over any token except for end of command sequence */
330 /* returns TRUE if succesful skip */
331 /* returns FALSE if already at end of command or EOF */
332 /*----------------------------------------------------------------------*/
336 char term[5]; /* string of characters not to skip */
339 if (pol.nl_eoc == TRUE) {
345 return (skiptok (term));
348 void pol_reader(void)
350 while (pol_skip() == TRUE)
353 dumptok (&token); /* skip end of command token */
356 /* skiptok (term) - skip a token unless the first character of a token is
357 * in the string of terminators, term.
358 * char *term - string of termination characters, don't skip these characters
359 * skiptok() also does NOT skip TT_EOF
360 * returns (TRUE) if succesful skip of a token
361 * returns (FALSE) if didn't skip, read termination character or TT_EOF
365 skiptok (char term[])
369 if (token.type == TT_EOF
370 || (token.type == TT_SPECLCHAR && strchr(term, token.tokstr[0]) != NULL))
379 pol_tok (struct token_st *token)
381 if (token->ready == FALSE)
384 if (token->type == TT_EOF && pol_lookchar() != EOF)
386 return (token->type);
390 dumptok (struct token_st *token)
392 if (token->ready == FALSE)
394 token->ready = FALSE;
398 getpol_tok (struct token_st *token)
402 token->ready = FALSE;
406 if (token->type == TT_BLANK)
408 if (token->type == TT_SPECLCHAR) {
409 if (strchr(pol.skipchars, token->tokstr[0]) != NULL)
411 if (token->tokstr[0] == NEWLINE)
413 if (token->tokstr[0] == meta.cmd) {
417 if (token->tokstr[0] == meta.com) { /* skip comment */
421 if (token->tokstr[0] == meta.out) {
422 getescape(token->tokstr, meta.out, MAXTOK);
423 fputs (token->tokstr, stderr);
426 if (token->tokstr[0] == meta.con) { /* continuation across NEWLINE */
427 while (pol_lookchar() == BLANK || pol_lookchar() == TAB)
429 if (pol_lookchar() == NEWLINE)
432 if (token->tokstr[0] == meta.ter) { /* get input from terminal */
433 pol_usefile (P_USE_FILE, "");
436 return (token->type);
440 /* look for filler words */
442 if (lookup (skiptable, token->tokstr) != NULL) /* ignore words in skip table */
445 /* look for user defined symbols */
447 if ((sym = lookup (usertable, token->tokstr)) != NULL) {
448 token->type = TT_USERTOK;
449 token->code = sym->code;
453 if (pol.trace == TRUE)
454 printf ("Read token '%s', type = %d\n", token->tokstr, token->type);
456 return (token->type);
460 static int getcmd(void)
467 tt = getalpha (str, MAXTOK);
468 if (tt == TT_ERROR) {
469 synerr ("error in pol parameter command");
473 if ((cmd = lookup (cmdtable,str)) == NULL) {
474 synerr ("unrecognized command");
491 pol_usefile (P_USE_FILE, tok.tokstr);
503 printf("eoc = %c str = %c com = %c cmd = %c prg = %c\n",
504 meta.eoc, meta.str, meta.com, meta.cmd, meta.prg);
505 printf("con = %c out = %c ter = %c inb = %c\n",
506 meta.con, meta.out, meta.ter, meta.inb);
509 if (found == FALSE) {
511 if (tt != TT_SPECLCHAR) {
512 synerr("illegal command character");
517 meta.eoc = tok.tokstr[0];
520 meta.str = tok.tokstr[0];
523 meta.com = tok.tokstr[0];
526 meta.cmd = tok.tokstr[0];
529 meta.prg = tok.tokstr[0];
532 meta.con = tok.tokstr[0];
535 meta.out = tok.tokstr[0];
538 meta.ter = tok.tokstr[0];
541 meta.inb = tok.tokstr[0];
544 printf("command not implemented\n");
546 } /* switch (tok->type) */
547 } /* if (found == FALSE) */
548 pol_reader(); /* clean up command */
549 } /* if legal command */
561 int toksiz = MAXTOK; /* maximum length of token string */
563 while ((c = pol_inchar()) == BLANK || c == TAB)
573 if (c == BLANK || c == TAB) { /* skip white space */
574 getblank(tok->tokstr, toksiz);
576 } else if (toktype == LETTER) {
577 toktype = getalpha (tok->tokstr, toksiz);
578 } else if (c == meta.str) { /* quoted string */
579 getquote (tok->tokstr, toksiz);
581 } else if (type(c) == DIGIT || c == PLUS || c == HYPHEN || c == PERIOD) {
582 toktype = getnumber (tok->tokstr, toksiz, &fnum, &inum);
583 } else if (c == EOF) {
584 tok->tokstr[0] = EOS;
589 tok->tokstr[1] = EOS;
590 toktype = TT_SPECLCHAR;
595 if (tok->type == TT_REAL || tok->type == TT_INT) {
608 getblank (char *s, int toksiz)
612 while ((c = pol_inchar()) == BLANK || c == TAB)
622 getalpha (char *s, int toksiz)
624 int i, chartype, alphatype;
626 if (type(pol_lookchar()) != LETTER) {
631 alphatype = TT_ALPHA;
632 for (i = 0; i < toksiz; i++) { /* get alphanumeric token */
634 chartype = type (s[i]);
635 if (chartype != LETTER && chartype != DIGIT)
637 if (chartype == DIGIT)
638 alphatype = TT_ALPNUM;
643 synerr("token too long.");
645 s[i] = EOS; /* terminate token */
650 /* getquote - get quoted string from file */
651 /* have already gotten delimiter in qs[0] */
653 getquote (char *qs, int toksiz)
657 delim = pol_inchar(); /* char = delimiter */
658 getescape(qs, delim, toksiz);
663 getescape ( /* reads up to delim */
671 for (i = 0; (c = pol_inchar()) != delim; i++) {
673 synerr ("Missing closing delimiter.");
677 synerr("string too long.");
682 synerr("end of file inside quotation");
684 } else if (c == BSLASH) { /* escape character */
686 c = pol_inchar(); /* get escaped character */
694 gettext (char *str, int lim)
698 while ((c = pol_inchar()) == BLANK || c == TAB)
702 for (i = 0; i < lim && (c = pol_inchar()) != EOF && c != NEWLINE; i++)
708 /*----------------------------------------------*/
709 /* Get a number for gettok() */
710 /*----------------------------------------------*/
714 char str[], /* string to return token in */
715 int strsize, /* maximum length of token string */
716 double *fnum, /* floating point value of number read */
717 int *inum /* integer value of number read */
721 double sign, whole, frac, powerof10, exp, expsign;
725 isSigned = FALSE; /* TRUE if number prefixed by '+' or '-' */
735 } else if (c == PLUS) {
739 } else if (c == PERIOD) {
740 if (type(pol_lookchar()) != DIGIT) {
743 return (TT_SPECLCHAR);
745 pol_ungetch (PERIOD);
746 } else if (type(c) != DIGIT) {
752 if (isSigned == TRUE) {
755 pol_inchar(); /* get period */
756 c = pol_lookchar(); /* look at character past period */
757 pol_ungetch (PERIOD); /* put back period */
758 if (type(c) != DIGIT) {
760 return (TT_SPECLCHAR);
762 } else if (type (c) != DIGIT) {
764 return (TT_SPECLCHAR);
769 while (type(c = pol_inchar()) == DIGIT) {
772 whole = 10.0 * whole + (c - '0');
774 pol_ungetch (c); /* put back non-numeric character */
776 if (c != PERIOD && tolower(c) != 'e') {
778 *fnum = whole * sign;
781 else if (*fnum > MAX_INT)
788 if (pol_lookchar() == PERIOD) {
797 while (type(c = pol_inchar()) == DIGIT) {
800 frac += (double) (c - '0') / powerof10;
808 if (tolower(c) != 'e')
813 if ((c = pol_inchar()) == PLUS) {
817 } else if (c == HYPHEN) {
821 } else if (type(c) != DIGIT) {
822 --sp; /* erase 'e' */
830 while (type(c = pol_inchar()) == DIGIT) {
833 exp = 10 * exp + (c - '0');
840 *fnum = sign * (whole + frac) * pow (10.0, expsign * exp);
843 else if (*fnum > MAX_INT)
861 type ( /* return type of ASCII character */
865 if (isalpha(c) || c == UNDERLIN)
873 /*----------------------------------------------------------------------*/
875 /* hash table routines. Kernighan & Ritchie */
877 /*----------------------------------------------------------------------*/
884 inittable (SYMBOL *table[])
888 for (i = 0; i < HASHSIZE; i++)
893 * free all memory allocated to table, then clear table
897 freetable (SYMBOL *table[])
902 for (i = 0; i < HASHSIZE; i++) {
914 hash ( /* form hash value of string s */
920 for (hashval = 0; *s != EOS; )
922 return (hashval % HASHSIZE);
925 /* Look for s in hash table */
927 lookup ( SYMBOL *table[], char *s )
930 SYMBOL *found = NULL;
932 for (np = table[hash(s)]; np != NULL; np = np->next)
933 if (strcasecmp(s, np->name) == 0) {
934 found = np; /* found it */
942 install (SYMBOL *table[], char *name, int def)
944 static char installerr[] = "install: out of memory";
948 if ((np = lookup (table, name)) == NULL) { /* not found */
949 np = (SYMBOL *) malloc (sizeof(*np));
954 if ((np->name = strdup(name)) == NULL) {
958 str_lower (np->name);
960 hashval = hash(np->name);
961 np->next = table[hashval];
963 } else /* already there */
968 /*----------------------------------------------------------------------*/
970 /*----------------------------------------------------------------------*/
974 static int currentf = -1; /* pointer to current fp */
975 static FILE *filep[MAXFILE]; /* == NULL for string input */
976 static char *fname[MAXFILE]; /* pointer to filename */
978 static char inputline[MAXLINE]; /* current input line */
979 static int lineptr; /* current position in inputline */
984 fputs (fname[currentf], stderr);
986 fputc (NEWLINE, stderr);
989 /*----------------------------------------------------------------------*/
991 /*----------------------------------------------------------------------*/
994 static int bp = 0; /* pointer to next free position */
995 static int buf[BUFSIZE]; /* pushed back input characters */
997 /* pol_usefile - set source of POL input
999 * int source - source of input
1000 * P_USE_STR - have POL use strings as input
1001 * P_USE_FILE - use file. filename is in str
1006 pol_usefile (int source, char *fn)
1011 if (currentf >= MAXFILE) {
1013 synerr ("files nested too deeply");
1017 bp = 0; /* clear any pushed back input */
1019 if (source == P_USE_STR) {
1020 filep[currentf] = NULL;
1021 } else if (source == P_USE_FILE) {
1022 if (fn == NULL || strlen(fn) == 0) {
1024 } else if ((fp = fopen(fn, "r")) == NULL) {
1026 synerr ("can't open file");
1029 filep[currentf] = fp;
1030 fname[currentf] = strdup (fn);
1034 void pol_closefile(void)
1036 if (currentf >= 0) {
1037 if (filep[currentf] != NULL)
1038 fclose (filep[currentf]);
1043 /*-----------------------------*/
1044 /* Lowest Level Input Routines */
1045 /*-----------------------------*/
1048 int pol_lookchar(void)
1057 int pol_inchar(void)
1064 while (currentf >= 0 && (c = pol_getch(filep[currentf])) == EOF && filep[currentf] != NULL) {
1071 /*--------------------------------------------------------------*/
1072 /* getch - get a (possibly pushed back) character */
1073 /* if fp == NULL, then get character from inputline */
1074 /*--------------------------------------------------------------*/
1077 pol_getch (FILE *fp)
1085 if ((c = inputline[lineptr]) == EOS)
1097 /* push character back on input */
1102 sys_error (ERR_SEVERE, "too many characters pushed back [pol_ungetch]");
1109 get_inputline (FILE *fp)
1113 if (fgets (inputline, MAXLINE, fp) == NULL)
1120 set_inputline (char *line)
1124 strncpy (inputline, line, MAXLINE);