home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / graphic / qrt / lexer.c < prev    next >
C/C++ Source or Header  |  1989-03-26  |  6KB  |  276 lines

  1.  
  2. /**********************************************************
  3.  
  4.       Small lexical analyser for inout. Also contains
  5.       some bounds checking code.
  6.  
  7.  **********************************************************/
  8.  
  9.  
  10. #include "qrt.h"
  11.  
  12. extern int linenumber;
  13. char *malloc();
  14.  
  15. # ifdef AMIGA
  16.     float atof();
  17. # endif
  18.  
  19. # ifdef UNIX
  20. #   include <string.h>
  21. # endif
  22.  
  23. /**********************************************************
  24.  
  25.            Transforms all white spaces to blanks
  26.  
  27.     - changed 13 Mar 89 to handle tabs in input file
  28.  
  29.  **********************************************************/
  30.  
  31. char towhite(c) char c; {
  32.   if (c=='\n') linenumber++;
  33.  
  34.   if ((c=='\n') ||
  35.       (c=='\t') ||
  36.       (c=='=')  ||
  37.       (c==',')  ||
  38.       (c==';'))     return(' ');
  39.  
  40.   return(c);
  41. }
  42.  
  43.  
  44. /**********************************************************
  45.  
  46.         Removes blank space before next token
  47.  
  48.  **********************************************************/
  49.  
  50. rmspace() {
  51.   char c;
  52.   while (((c=towhite(fgetc(stdin)))==' ') && !feof(stdin));
  53.   ungetc(c,stdin);
  54. }
  55.  
  56. /**********************************************************
  57.  
  58.  Comment Killer - Added 16 Jun 88 to handle nested comments
  59.  
  60.  **********************************************************/
  61.  
  62. Comment_Killer()
  63. {
  64.   char c;
  65.  
  66.   c='\0';
  67.   while (c != '}' && !feof(stdin)) {
  68.     c = towhite(fgetc(stdin));
  69.     if (c == '{') Comment_Killer();
  70.   }
  71. }
  72.  
  73. /**********************************************************
  74.  
  75.                Get next token from stdin
  76.  
  77.    - Changed 12 Mar 89 to fix compatibility problem with
  78.      toupper() across different versions of UN*X.
  79.      Aparently some versions change a letter even if it is
  80.      already upper case.
  81.  
  82.  **********************************************************/
  83.  
  84. GetToken(s)                          /* get a token from stdio */
  85.   char s[];
  86. {                                    /* */
  87.   char c; int x;
  88.  
  89.   x=0; s[0]=c='\0';                       /* char count */
  90.  
  91.   rmspace();
  92.  
  93.   while (!feof(stdin) && x<(SLEN-1)) {
  94.  
  95.     c = towhite(fgetc(stdin));
  96.     if ('a'<=c && c<='z') c=toupper(c);
  97.     s[x++]=c;
  98.  
  99.     if (c==' ') { s[--x]='\0'; break; }
  100.     if (c=='(' || c==')') {
  101.       if (x==1) { s[x]='\0'; break; }
  102.       else {
  103.         ungetc(c,stdin); s[--x]='\0'; break;
  104.       }
  105.     }
  106.     if (c=='{') {
  107.       x--;
  108.       Comment_Killer();
  109.       rmspace();
  110.     }
  111.   }
  112. }
  113.  
  114.  
  115. /**********************************************************
  116.  
  117.       Return value if color is in range 0<=cnum<=CNUM
  118.       otherwise call error routine.
  119.  
  120.  **********************************************************/
  121.  
  122. float InRange(cnum)
  123.   float cnum;
  124. {
  125.   if (cnum>=0.0 && cnum<=1.00) return(cnum);
  126.   Error(COLOR_VALUE_ERR,1501);
  127.  
  128.   return(0);   /* this is to keep lint happy! */
  129. }
  130.  
  131.  
  132. /**********************************************************
  133.  
  134.          Return value if value is >=0.
  135.          otherwise call error routine.
  136.  
  137.  **********************************************************/
  138.  
  139. float IsPos(val)
  140.   float val;
  141. {
  142.   if (val >= 0.0) return(val);
  143.   Error(LESS_THAN_ZERO,1502);
  144.  
  145.   return(0);   /* this is to keep lint happy! */
  146. }
  147.  
  148. /**********************************************************
  149.  
  150.     Reads next number and converts to float from string
  151.  
  152.  **********************************************************/
  153.  
  154. float Get_Next_Num() {
  155.   char str[SLEN];
  156.   float val;
  157.  
  158.   GetToken(str);
  159.  
  160.   val=atof(str);
  161.  
  162. # ifdef IODEBUG
  163.     printf("GETNEXTNUM: token=%s, val=%f\n",str,val);
  164. # endif
  165.  
  166.   return(val);
  167. }
  168.  
  169.  
  170. /**********************************************************
  171.  
  172.       Reads a name from input, and allocates some space
  173.       for it. Returns a pointer to space.
  174.  
  175.  **********************************************************/
  176.  
  177. char *Get_Next_Name() {
  178.   char str[SLEN], *s;
  179.  
  180.   GetToken(str);
  181.  
  182.   if ((s=malloc(strlen(str)+1))==NULL)
  183.     Error(MALLOC_FAILURE,1503);
  184.  
  185.   strcpy(s,str);
  186.  
  187. # ifdef IODEBUG
  188.     printf("GETNEXTNAME: token=%s\n",str);
  189. # endif
  190.  
  191.   return(s);
  192. }
  193.  
  194. /**********************************************************
  195.  
  196.      Reads a number 0..1 and returns a color value
  197.      0..CNUM;
  198.  
  199.  **********************************************************/
  200.  
  201. short Get_Color_Val() {
  202.   return((short)(InRange(Get_Next_Num())*(float)CNUM));
  203. }
  204.  
  205.  
  206. /**********************************************************
  207.  
  208.        Returns true if the next token is a left paren
  209.  
  210.  **********************************************************/
  211.  
  212. GetLeftParen() {
  213.   char str[SLEN];
  214.  
  215.   GetToken(str);
  216.   if (strcmp(str,"(")!=0) Error(LPAREN_EXPECTED,1504);
  217.   return;
  218. }
  219.  
  220.  
  221. /**********************************************************
  222.  
  223.        Returns true if the next token is a left paren
  224.  
  225.  **********************************************************/
  226.  
  227. int GetRightParen() {
  228.   char str[SLEN];
  229.  
  230.   GetToken(str);
  231.   if (strcmp(str,")")!=0) return(FALSE);
  232.   return(TRUE);
  233. }
  234.  
  235.  
  236. /**********************************************************
  237.  
  238.            Gets a VECTOR structure of the form
  239.            (num1, num2, num3)
  240.  
  241.  **********************************************************/
  242.  
  243. GetVector(vector)
  244.   VECT_PTR vector;
  245. {
  246.   GetLeftParen();
  247.  
  248.   vector->x = Get_Next_Num();
  249.   vector->y = Get_Next_Num();
  250.   vector->z = Get_Next_Num();
  251.  
  252.   if(!GetRightParen()) Error(ILLEGAL_VECTOR,1505);
  253. }
  254.  
  255.  
  256. /**********************************************************
  257.  
  258.            Gets a SVECTOR structure of the form
  259.            (num1, num2, num3)
  260.  
  261.  **********************************************************/
  262.  
  263. GetSVector(svector)
  264.   SVECT_PTR svector;
  265. {
  266.   GetLeftParen();
  267.  
  268.   svector->r = Get_Color_Val();
  269.   svector->g = Get_Color_Val();
  270.   svector->b = Get_Color_Val();
  271.  
  272.   if(!GetRightParen()) Error(ILLEGAL_SVECTOR,1506);
  273. }
  274.  
  275.  
  276.