1 /*****************************************************************************
4 ** POL - Problem Oriented Language
6 ** This is part of the CTSim program
7 ** Copyright (c) 1983-2009 Kevin Rosenberg
9 ** This program is free software; you can redistribute it and/or modify
10 ** it under the terms of the GNU General Public License (version 2) as
11 ** published by the Free Software Foundation.
13 ** This program is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ** GNU General Public License for more details.
18 ** You should have received a copy of the GNU General Public License
19 ** along with this program; if not, write to the Free Software
20 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 ******************************************************************************/
27 #include "ctsupport.h"
31 const struct POL::KeywordCodeList POL::cmdlist[] = {
42 { "nl_eoc",PC_NL_EOC,},
43 { "nl_neoc", PC_NL_NEOC,},
45 { "troff", PC_TROFF,},
50 const unsigned int POL::NUMCMD = (sizeof(POL::cmdlist) / sizeof (struct POL::KeywordCodeList));
77 m_bNewlineIsEOC = true;
78 m_szSkipChars[0] = EOS;
81 for (unsigned int i = 0; i < NUMCMD; i++)
82 cmdtable.installKeywordCode (cmdlist[i].keyword, cmdlist[i].code);
84 token.ready = false; // no token read yet
90 * char *w - word for pol to ignore and skip over in input
92 * tok() compares all tokens to words given to this routine. If it finds it,
93 * it will immediately read another token.
97 POL::addSkipWord (const char* const w)
99 skiptable.installKeywordCode (w, 0);
104 * skip all characters that appear in string s
107 POL::addSkipChar (int c)
109 int n = strlen (m_szSkipChars);
110 if (n < MAXSKIPCHAR) {
111 m_szSkipChars[n] = c;
112 m_szSkipChars[n+1] = 0;
116 // installKeyword (str, code)
118 // char *str - token string to install
119 // int code - code to return for token
121 // tok() looks for these user defined tokens. If it finds one,
122 // it stores the tokens code in the token structure and returns TT_USERTOK
124 POL::addKeyword (const char* const str, int code)
126 usertable.installKeywordCode (str, code);
129 /* get_word - matches tokens on a letter by letter basis
131 * char *search - string to search for
132 * int nlet - maximum number of chars to search for match
136 POL::readWord (const char *search, int nlet)
140 sys_error (ERR_TRACE, "POL matching current token %s against word %s\n", token.tokstr, search);
142 if (strncasecmp (search, token.tokstr, nlet) == 0) {
149 /* usertok (str,code)
150 * see if current token is a user defined token set with install()
152 * char *str - token string as read from input
153 * int *code - returned code for user defined symbol
154 * return value - true if current token has been user defined
155 * false if current token is not user defined
158 POL::readUserToken (char *str, int *code)
163 sys_error (ERR_TRACE, "POL checking if current token '%s' is user defined\n", token.tokstr);
165 if (token.type == TT_USERTOK) {
167 strcpy (str, token.tokstr);
176 /* isstring (s) - returns true if current token is a string
178 * char *s - pointer to place to store token string
182 POL::readString (char *str)
186 if (token.type == TT_STRING) {
187 strcpy (str, token.tokstr);
194 /* integer - test for an integer
196 * int *n: returned integer value
197 * int typecode = TT_INT if accept only integer values
198 * = TT_REAL if accept both real and integer values
199 * int boundcode= true if force to lie between boundries
200 * = false can take any value it likes
201 * int bb1: lower bound
202 * int bb2: upper bound
205 POL::readInteger (int *n, int typecode, bool boundcode, int bb1, int bb2)
210 sys_error (ERR_TRACE, "POL checking if current token %s is an integer\n", token.tokstr);
212 if (token.type == TT_INT || token.type == TT_REAL) {
214 if (token.inum < bb1)
216 else if (token.inum > bb2)
230 POL::readFloat (double *n, double typecode, bool boundcode, double bb1, double bb2)
235 sys_error (ERR_TRACE, "POL checking if current token %s is an floating point number\n", token.tokstr);
237 if (token.type == TT_INT || token.type == TT_REAL) {
239 if (token.fnum < bb1)
241 else if (token.fnum > bb2)
254 /*----------------------------------------------------------------------*/
255 /* skip() - skip over any token except for end of command sequence */
257 /* returns true if succesful skip */
258 /* returns false if already at end of command or EOF */
259 /*----------------------------------------------------------------------*/
264 char term[5]; /* string of characters not to skip */
267 if (m_bNewlineIsEOC) {
273 return (skipSingleToken (term));
282 dumptok (&token); /* skip end of command token */
285 /* skiptok (term) - skip a token unless the first character of a token is
286 * in the string of terminators, term.
287 * char *term - string of termination characters, don't skip these characters
288 * skiptok() also does NOT skip TT_EOF
289 * returns (true) if succesful skip of a token
290 * returns (false) if didn't skip, read termination character or TT_EOF
294 POL::skipSingleToken (char term[])
298 if (token.type == TT_EOF
299 || (token.type == TT_SPECLCHAR && strchr(term, token.tokstr[0]) != NULL))
308 POL::tok (struct token_st *token)
310 if (token->ready == false)
313 if (token->type == TT_EOF && lookchar() != EOF)
315 return (token->type);
319 POL::dumptok (struct token_st *token)
321 if (token->ready == false)
323 token->ready = false;
327 POL::getpol_tok (struct token_st *token)
329 KeywordCodeEntry* sym;
331 token->ready = false;
335 if (token->type == TT_BLANK)
337 if (token->type == TT_SPECLCHAR) {
338 if (strchr(m_szSkipChars, token->tokstr[0]) != NULL)
340 if (token->tokstr[0] == NEWLINE)
342 if (token->tokstr[0] == meta.cmd) {
346 if (token->tokstr[0] == meta.com) { /* skip comment */
350 if (token->tokstr[0] == meta.out) {
351 getescape(token->tokstr, meta.out, MAXTOK);
352 fputs (token->tokstr, stderr);
355 if (token->tokstr[0] == meta.con) { /* continuation across NEWLINE */
356 while (lookchar() == BLANK || lookchar() == TAB)
358 if (lookchar() == NEWLINE)
361 if (token->tokstr[0] == meta.ter) { /* get input from terminal */
362 usefile (P_USE_FILE, "");
365 return (token->type);
369 /* look for filler words */
371 if (skiptable.lookup (token->tokstr) != NULL) /* ignore words in skip table */
374 /* look for user defined symbols */
376 if ((sym = usertable.lookup (token->tokstr)) != NULL) {
377 token->type = TT_USERTOK;
378 token->code = sym->getCode();
383 sys_error (ERR_TRACE, "POL read token '%s', type = %d\n", token->tokstr, token->type);
385 return (token->type);
394 KeywordCodeEntry *cmd;
397 tt = getalpha (str, MAXTOK);
398 if (tt == TT_ERROR) {
399 sys_error (ERR_WARNING, "Error in POL parameter command");
403 if ((cmd = cmdtable.lookup (str)) == NULL) {
404 sys_error (ERR_WARNING, "POL: Unrecognized command %s", cmd);
409 switch (cmd->getCode()) {
421 usefile (P_USE_FILE, tok.tokstr);
425 m_bNewlineIsEOC = true;
429 m_bNewlineIsEOC = false;
433 printf("eoc = %c str = %c com = %c cmd = %c prg = %c\n",
434 meta.eoc, meta.str, meta.com, meta.cmd, meta.prg);
435 printf("con = %c out = %c ter = %c inb = %c\n",
436 meta.con, meta.out, meta.ter, meta.inb);
439 if (found == false) {
441 if (tt != TT_SPECLCHAR) {
442 sys_error (ERR_SEVERE, "POL: Illegal command character");
445 switch(cmd->getCode()) {
447 meta.eoc = tok.tokstr[0];
450 meta.str = tok.tokstr[0];
453 meta.com = tok.tokstr[0];
456 meta.cmd = tok.tokstr[0];
459 meta.prg = tok.tokstr[0];
462 meta.con = tok.tokstr[0];
465 meta.out = tok.tokstr[0];
468 meta.ter = tok.tokstr[0];
471 meta.inb = tok.tokstr[0];
474 printf("command not implemented\n");
476 } /* switch (tok->type) */
477 } /* if (found == false) */
478 reader(); /* clean up command */
479 } /* if legal command */
486 POL::gettok (TOKEN *tok)
491 int toksiz = MAXTOK; /* maximum length of token string */
493 while ((c = inchar()) == BLANK || c == TAB)
503 if (c == BLANK || c == TAB) { /* skip white space */
504 getblank(tok->tokstr, toksiz);
506 } else if (toktype == LETTER) {
507 toktype = getalpha (tok->tokstr, toksiz);
508 } else if (c == meta.str) { /* quoted string */
509 getquote (tok->tokstr, toksiz);
511 } else if (type(c) == DIGIT || c == PLUS || c == HYPHEN || c == PERIOD) {
512 toktype = getnumber (tok->tokstr, toksiz, &fnum, &inum);
513 } else if (c == EOF) {
514 tok->tokstr[0] = EOS;
519 tok->tokstr[1] = EOS;
520 toktype = TT_SPECLCHAR;
525 if (tok->type == TT_REAL || tok->type == TT_INT) {
538 POL::getblank (char *s, int toksiz)
542 while ((c = inchar()) == BLANK || c == TAB)
552 POL::getalpha (char *s, int toksiz)
554 int i, chartype, alphatype;
556 if (type(lookchar()) != LETTER) {
561 alphatype = TT_ALPHA;
562 for (i = 0; i < toksiz; i++) { /* get alphanumeric token */
564 chartype = type (s[i]);
565 if (chartype != LETTER && chartype != DIGIT)
567 if (chartype == DIGIT)
568 alphatype = TT_ALPNUM;
573 sys_error (ERR_SEVERE, "POL token too long.");
575 s[i] = EOS; /* terminate token */
580 /* getquote - get quoted string from file */
581 /* have already gotten delimiter in qs[0] */
583 POL::getquote (char *qs, int toksiz)
587 delim = inchar(); /* char = delimiter */
588 getescape(qs, delim, toksiz);
593 POL::getescape ( /* reads up to delim */
601 for (i = 0; (c = inchar()) != delim; i++) {
603 sys_error (ERR_WARNING, "Missing closing delimiter.");
607 sys_error (ERR_SEVERE, "string too long.");
612 sys_error (ERR_SEVERE, "end of file inside quotation");
614 } else if (c == BSLASH) { /* escape character */
616 c = inchar(); /* get escaped character */
625 POL::readText (char *str, int lim)
628 while ((c = inchar()) == BLANK || c == TAB)
637 for (i = 0; i < lim && (c = inchar()) != EOF && c != NEWLINE; i++)
645 //----------------------------------------------
646 // Get a number for gettok()
647 //----------------------------------------------
652 char str[], /* string to return token in */
653 int strsize, /* maximum length of token string */
654 double *fnum, /* floating point value of number read */
655 int *inum /* integer value of number read */
660 bool isSigned = false; /* true if number prefixed by '+' or '-' */
670 } else if (c == PLUS) {
674 } else if (c == PERIOD) {
675 if (type(lookchar()) != DIGIT) {
678 return (TT_SPECLCHAR);
681 } else if (type(c) != DIGIT) {
690 inchar(); /* get period */
691 c = lookchar(); /* look at character past period */
692 ungetch (PERIOD); /* put back period */
693 if (type(c) != DIGIT) {
695 return (TT_SPECLCHAR);
697 } else if (type (c) != DIGIT) {
699 return (TT_SPECLCHAR);
704 while (type(c = inchar()) == DIGIT) {
707 whole = 10.0 * whole + (c - '0');
709 ungetch (c); /* put back non-numeric character */
711 if (c != PERIOD && tolower(c) != 'e') {
713 *fnum = whole * sign;
716 else if (*fnum > MAX_INT)
723 if (lookchar() == PERIOD) {
730 double powerof10 = 10.0;
732 while (type(c = inchar()) == DIGIT) {
735 frac += (double) (c - '0') / powerof10;
741 double expsign = 1.0;
743 if (tolower(c) != 'e')
748 if ((c = inchar()) == PLUS) {
752 } else if (c == HYPHEN) {
756 } else if (type(c) != DIGIT) {
757 --sp; /* erase 'e' */
765 while (type(c = inchar()) == DIGIT) {
768 exp = 10 * exp + (c - '0');
775 *fnum = sign * (whole + frac) * pow (10.0, expsign * exp);
778 else if (*fnum > MAX_INT)
792 skipSingleToken (term);
795 // return type of ASCII character
799 if (isalpha(c) || c == UNDERLIN)
808 //----------------------------------------------------------------------
810 //----------------------------------------------------------------------
813 /* usefile - set source of POL input
815 * int source - source of input
816 * P_USE_STR - have POL use strings as input
817 * P_USE_FILE - use file. filename is in str
822 POL::usefile (int source, const char *fn)
827 if (currentf >= MAXFILE) {
829 sys_error (ERR_SEVERE, "files nested too deeply");
833 while (! m_stackPushBackInput.empty())
834 m_stackPushBackInput.pop();
836 if (source == P_USE_STR) {
837 filep[currentf] = NULL;
838 } else if (source == P_USE_FILE) {
839 if (fn == NULL || strlen(fn) == 0) {
841 } else if ((fp = fopen(fn, "r")) == NULL) {
843 sys_error (ERR_SEVERE, "can't open file");
846 filep[currentf] = fp;
847 fname[currentf] = strdup (fn);
855 if (filep[currentf] != NULL)
856 fclose (filep[currentf]);
861 /*-----------------------------*/
862 /* Lowest Level Input Routines */
863 /*-----------------------------*/
884 while (currentf >= 0 && (c = getch(filep[currentf])) == EOF && filep[currentf] != NULL) {
891 /*--------------------------------------------------------------*/
892 /* getch - get a (possibly pushed back) character */
893 /* if fp == NULL, then get character from inputline */
894 /*--------------------------------------------------------------*/
897 POL::getch (FILE *fp)
900 if (m_stackPushBackInput.size() > 0) {
901 c = m_stackPushBackInput.top();
902 m_stackPushBackInput.pop();
907 if ((c = inputline[lineptr]) == EOS)
919 // push character back on input
923 m_stackPushBackInput.push (c);
928 POL::get_inputline (FILE *fp)
930 while (! m_stackPushBackInput.empty())
931 m_stackPushBackInput.pop();
934 if (fgets (inputline, MAXLINE, fp) == NULL)
941 POL::set_inputline (const char* const line)
943 while (! m_stackPushBackInput.empty())
944 m_stackPushBackInput.pop();
946 strncpy (inputline, line, MAXLINE);