1 /*****************************************************************************
4 ** POL - Problem Oriented Language
6 ** This is part of the CTSim program
7 ** Copyright (c) 1983-2001 Kevin Rosenberg
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 ******************************************************************************/
29 #include "ctsupport.h"
33 const struct POL::KeywordCodeList POL::cmdlist[] = {
44 { "nl_eoc",PC_NL_EOC,},
45 { "nl_neoc", PC_NL_NEOC,},
47 { "troff", PC_TROFF,},
52 const unsigned int POL::NUMCMD = (sizeof(POL::cmdlist) / sizeof (struct POL::KeywordCodeList));
79 m_bNewlineIsEOC = true;
80 m_szSkipChars[0] = EOS;
83 for (unsigned int i = 0; i < NUMCMD; i++)
84 cmdtable.installKeywordCode (cmdlist[i].keyword, cmdlist[i].code);
86 token.ready = false; // no token read yet
92 * char *w - word for pol to ignore and skip over in input
94 * tok() compares all tokens to words given to this routine. If it finds it,
95 * it will immediately read another token.
99 POL::addSkipWord (const char* const w)
101 skiptable.installKeywordCode (w, 0);
106 * skip all characters that appear in string s
109 POL::addSkipChar (int c)
111 int n = strlen (m_szSkipChars);
112 if (n < MAXSKIPCHAR) {
113 m_szSkipChars[n] = c;
114 m_szSkipChars[n+1] = 0;
118 // installKeyword (str, code)
120 // char *str - token string to install
121 // int code - code to return for token
123 // tok() looks for these user defined tokens. If it finds one,
124 // it stores the tokens code in the token structure and returns TT_USERTOK
126 POL::addKeyword (const char* const str, int code)
128 usertable.installKeywordCode (str, code);
131 /* get_word - matches tokens on a letter by letter basis
133 * char *search - string to search for
134 * int nlet - maximum number of chars to search for match
138 POL::readWord (char *search, int nlet)
142 sys_error (ERR_TRACE, "POL matching current token %s against word %s\n", token.tokstr, search);
144 if (strncasecmp (search, token.tokstr, nlet) == 0) {
151 /* usertok (str,code)
152 * see if current token is a user defined token set with install()
154 * char *str - token string as read from input
155 * int *code - returned code for user defined symbol
156 * return value - true if current token has been user defined
157 * false if current token is not user defined
160 POL::readUserToken (char *str, int *code)
165 sys_error (ERR_TRACE, "POL checking if current token '%s' is user defined\n", token.tokstr);
167 if (token.type == TT_USERTOK) {
169 strcpy (str, token.tokstr);
178 /* isstring (s) - returns true if current token is a string
180 * char *s - pointer to place to store token string
184 POL::readString (char *str)
188 if (token.type == TT_STRING) {
189 strcpy (str, token.tokstr);
196 /* integer - test for an integer
198 * int *n: returned integer value
199 * int typecode = TT_INT if accept only integer values
200 * = TT_REAL if accept both real and integer values
201 * int boundcode= true if force to lie between boundries
202 * = false can take any value it likes
203 * int bb1: lower bound
204 * int bb2: upper bound
207 POL::readInteger (int *n, int typecode, bool boundcode, int bb1, int bb2)
212 sys_error (ERR_TRACE, "POL checking if current token %s is an integer\n", token.tokstr);
214 if (token.type == TT_INT || token.type == TT_REAL) {
216 if (token.inum < bb1)
218 else if (token.inum > bb2)
232 POL::readFloat (double *n, double typecode, bool boundcode, double bb1, double bb2)
237 sys_error (ERR_TRACE, "POL checking if current token %s is an floating point number\n", token.tokstr);
239 if (token.type == TT_INT || token.type == TT_REAL) {
241 if (token.fnum < bb1)
243 else if (token.fnum > bb2)
256 /*----------------------------------------------------------------------*/
257 /* skip() - skip over any token except for end of command sequence */
259 /* returns true if succesful skip */
260 /* returns false if already at end of command or EOF */
261 /*----------------------------------------------------------------------*/
266 char term[5]; /* string of characters not to skip */
269 if (m_bNewlineIsEOC) {
275 return (skipSingleToken (term));
284 dumptok (&token); /* skip end of command token */
287 /* skiptok (term) - skip a token unless the first character of a token is
288 * in the string of terminators, term.
289 * char *term - string of termination characters, don't skip these characters
290 * skiptok() also does NOT skip TT_EOF
291 * returns (true) if succesful skip of a token
292 * returns (false) if didn't skip, read termination character or TT_EOF
296 POL::skipSingleToken (char term[])
300 if (token.type == TT_EOF
301 || (token.type == TT_SPECLCHAR && strchr(term, token.tokstr[0]) != NULL))
310 POL::tok (struct token_st *token)
312 if (token->ready == false)
315 if (token->type == TT_EOF && lookchar() != EOF)
317 return (token->type);
321 POL::dumptok (struct token_st *token)
323 if (token->ready == false)
325 token->ready = false;
329 POL::getpol_tok (struct token_st *token)
331 KeywordCodeEntry* sym;
333 token->ready = false;
337 if (token->type == TT_BLANK)
339 if (token->type == TT_SPECLCHAR) {
340 if (strchr(m_szSkipChars, token->tokstr[0]) != NULL)
342 if (token->tokstr[0] == NEWLINE)
344 if (token->tokstr[0] == meta.cmd) {
348 if (token->tokstr[0] == meta.com) { /* skip comment */
352 if (token->tokstr[0] == meta.out) {
353 getescape(token->tokstr, meta.out, MAXTOK);
354 fputs (token->tokstr, stderr);
357 if (token->tokstr[0] == meta.con) { /* continuation across NEWLINE */
358 while (lookchar() == BLANK || lookchar() == TAB)
360 if (lookchar() == NEWLINE)
363 if (token->tokstr[0] == meta.ter) { /* get input from terminal */
364 usefile (P_USE_FILE, "");
367 return (token->type);
371 /* look for filler words */
373 if (skiptable.lookup (token->tokstr) != NULL) /* ignore words in skip table */
376 /* look for user defined symbols */
378 if ((sym = usertable.lookup (token->tokstr)) != NULL) {
379 token->type = TT_USERTOK;
380 token->code = sym->getCode();
385 sys_error (ERR_TRACE, "POL read token '%s', type = %d\n", token->tokstr, token->type);
387 return (token->type);
396 KeywordCodeEntry *cmd;
399 tt = getalpha (str, MAXTOK);
400 if (tt == TT_ERROR) {
401 sys_error (ERR_WARNING, "Error in POL parameter command");
405 if ((cmd = cmdtable.lookup (str)) == NULL) {
406 sys_error (ERR_WARNING, "POL: Unrecognized command %s", cmd);
411 switch (cmd->getCode()) {
423 usefile (P_USE_FILE, tok.tokstr);
427 m_bNewlineIsEOC = true;
431 m_bNewlineIsEOC = false;
435 printf("eoc = %c str = %c com = %c cmd = %c prg = %c\n",
436 meta.eoc, meta.str, meta.com, meta.cmd, meta.prg);
437 printf("con = %c out = %c ter = %c inb = %c\n",
438 meta.con, meta.out, meta.ter, meta.inb);
441 if (found == false) {
443 if (tt != TT_SPECLCHAR) {
444 sys_error (ERR_SEVERE, "POL: Illegal command character");
447 switch(cmd->getCode()) {
449 meta.eoc = tok.tokstr[0];
452 meta.str = tok.tokstr[0];
455 meta.com = tok.tokstr[0];
458 meta.cmd = tok.tokstr[0];
461 meta.prg = tok.tokstr[0];
464 meta.con = tok.tokstr[0];
467 meta.out = tok.tokstr[0];
470 meta.ter = tok.tokstr[0];
473 meta.inb = tok.tokstr[0];
476 printf("command not implemented\n");
478 } /* switch (tok->type) */
479 } /* if (found == false) */
480 reader(); /* clean up command */
481 } /* if legal command */
488 POL::gettok (TOKEN *tok)
493 int toksiz = MAXTOK; /* maximum length of token string */
495 while ((c = inchar()) == BLANK || c == TAB)
505 if (c == BLANK || c == TAB) { /* skip white space */
506 getblank(tok->tokstr, toksiz);
508 } else if (toktype == LETTER) {
509 toktype = getalpha (tok->tokstr, toksiz);
510 } else if (c == meta.str) { /* quoted string */
511 getquote (tok->tokstr, toksiz);
513 } else if (type(c) == DIGIT || c == PLUS || c == HYPHEN || c == PERIOD) {
514 toktype = getnumber (tok->tokstr, toksiz, &fnum, &inum);
515 } else if (c == EOF) {
516 tok->tokstr[0] = EOS;
521 tok->tokstr[1] = EOS;
522 toktype = TT_SPECLCHAR;
527 if (tok->type == TT_REAL || tok->type == TT_INT) {
540 POL::getblank (char *s, int toksiz)
544 while ((c = inchar()) == BLANK || c == TAB)
554 POL::getalpha (char *s, int toksiz)
556 int i, chartype, alphatype;
558 if (type(lookchar()) != LETTER) {
563 alphatype = TT_ALPHA;
564 for (i = 0; i < toksiz; i++) { /* get alphanumeric token */
566 chartype = type (s[i]);
567 if (chartype != LETTER && chartype != DIGIT)
569 if (chartype == DIGIT)
570 alphatype = TT_ALPNUM;
575 sys_error (ERR_SEVERE, "POL token too long.");
577 s[i] = EOS; /* terminate token */
582 /* getquote - get quoted string from file */
583 /* have already gotten delimiter in qs[0] */
585 POL::getquote (char *qs, int toksiz)
589 delim = inchar(); /* char = delimiter */
590 getescape(qs, delim, toksiz);
595 POL::getescape ( /* reads up to delim */
603 for (i = 0; (c = inchar()) != delim; i++) {
605 sys_error (ERR_WARNING, "Missing closing delimiter.");
609 sys_error (ERR_SEVERE, "string too long.");
614 sys_error (ERR_SEVERE, "end of file inside quotation");
616 } else if (c == BSLASH) { /* escape character */
618 c = inchar(); /* get escaped character */
627 POL::readText (char *str, int lim)
630 while ((c = inchar()) == BLANK || c == TAB)
639 for (i = 0; i < lim && (c = inchar()) != EOF && c != NEWLINE; i++)
647 //----------------------------------------------
648 // Get a number for gettok()
649 //----------------------------------------------
654 char str[], /* string to return token in */
655 int strsize, /* maximum length of token string */
656 double *fnum, /* floating point value of number read */
657 int *inum /* integer value of number read */
662 bool isSigned = false; /* true if number prefixed by '+' or '-' */
672 } else if (c == PLUS) {
676 } else if (c == PERIOD) {
677 if (type(lookchar()) != DIGIT) {
680 return (TT_SPECLCHAR);
683 } else if (type(c) != DIGIT) {
692 inchar(); /* get period */
693 c = lookchar(); /* look at character past period */
694 ungetch (PERIOD); /* put back period */
695 if (type(c) != DIGIT) {
697 return (TT_SPECLCHAR);
699 } else if (type (c) != DIGIT) {
701 return (TT_SPECLCHAR);
706 while (type(c = inchar()) == DIGIT) {
709 whole = 10.0 * whole + (c - '0');
711 ungetch (c); /* put back non-numeric character */
713 if (c != PERIOD && tolower(c) != 'e') {
715 *fnum = whole * sign;
718 else if (*fnum > MAX_INT)
725 if (lookchar() == PERIOD) {
732 double powerof10 = 10.0;
734 while (type(c = inchar()) == DIGIT) {
737 frac += (double) (c - '0') / powerof10;
743 double expsign = 1.0;
745 if (tolower(c) != 'e')
750 if ((c = inchar()) == PLUS) {
754 } else if (c == HYPHEN) {
758 } else if (type(c) != DIGIT) {
759 --sp; /* erase 'e' */
767 while (type(c = inchar()) == DIGIT) {
770 exp = 10 * exp + (c - '0');
777 *fnum = sign * (whole + frac) * pow (10.0, expsign * exp);
780 else if (*fnum > MAX_INT)
794 skipSingleToken (term);
797 // return type of ASCII character
801 if (isalpha(c) || c == UNDERLIN)
810 //----------------------------------------------------------------------
812 //----------------------------------------------------------------------
815 /* usefile - set source of POL input
817 * int source - source of input
818 * P_USE_STR - have POL use strings as input
819 * P_USE_FILE - use file. filename is in str
824 POL::usefile (int source, char *fn)
829 if (currentf >= MAXFILE) {
831 sys_error (ERR_SEVERE, "files nested too deeply");
835 while (! m_stackPushBackInput.empty())
836 m_stackPushBackInput.pop();
838 if (source == P_USE_STR) {
839 filep[currentf] = NULL;
840 } else if (source == P_USE_FILE) {
841 if (fn == NULL || strlen(fn) == 0) {
843 } else if ((fp = fopen(fn, "r")) == NULL) {
845 sys_error (ERR_SEVERE, "can't open file");
848 filep[currentf] = fp;
849 fname[currentf] = strdup (fn);
857 if (filep[currentf] != NULL)
858 fclose (filep[currentf]);
863 /*-----------------------------*/
864 /* Lowest Level Input Routines */
865 /*-----------------------------*/
886 while (currentf >= 0 && (c = getch(filep[currentf])) == EOF && filep[currentf] != NULL) {
893 /*--------------------------------------------------------------*/
894 /* getch - get a (possibly pushed back) character */
895 /* if fp == NULL, then get character from inputline */
896 /*--------------------------------------------------------------*/
899 POL::getch (FILE *fp)
902 if (m_stackPushBackInput.size() > 0) {
903 c = m_stackPushBackInput.top();
904 m_stackPushBackInput.pop();
909 if ((c = inputline[lineptr]) == EOS)
921 // push character back on input
925 m_stackPushBackInput.push (c);
930 POL::get_inputline (FILE *fp)
932 while (! m_stackPushBackInput.empty())
933 m_stackPushBackInput.pop();
936 if (fgets (inputline, MAXLINE, fp) == NULL)
943 POL::set_inputline (const char* const line)
945 while (! m_stackPushBackInput.empty())
946 m_stackPushBackInput.pop();
948 strncpy (inputline, line, MAXLINE);