/*
 *  MODULE  : scan.h
 *  The scanner reads one Pascal symbol, also initializes symbol tables.
{*  last modification:	04.08.92  10:43   }

 *   uli 19.11.90 : cTemptype, cRecelem, cTempused  added.
 *   uli 22.11.90 : priority of := changed.
 *   uli 28.01.91 : sym_is[].leftexpr added.
 *   uli 16.04.91 : sym_is[].type add.
 *                  cLParU and cLParD added.
 *   uli 30.04.91 : cPCast and cFree added.
 *   uli 03.05.91 : cFreedNode added.
 *   uli 15.05.91 : rightexpr added.
 *   uli 06.08.91 : cPTRCHAR cSIZEOF added.
 *   uli 09.08.91 : cINT_OF_C, cFILE_OF_C, cPOINTER_OF_C added.
 *   uli 27.08.91 : cEnum added.
 *   uli 12.11.91 : FILENAME_MAX removed.
 *   uli 23.01.92 : cIDOT etc. added.
 *   uli 13.05.92 : sym_is[].lvalue added.
 *   PJ  04.08.92 : variables for DEMOVERSION variable restriction added.
 *   Tim 10.11.92 : cALLOCATE, cALLOCATED and cFREE added.
*/


#ifndef UCHAR_MAX
#define UCHAR_MAX 255
#endif


#define SYMSIZE  UCHAR_MAX

global postype  pos      ; /* position of symbol read */

extern boolean  in_terminal; /* reading source from terminal */


					/*---------------------------*/
					/* data type for symbol code */
typedef enum symc {
 /* Codename       Pascal  C   kind   left right */
 cNosym = 0   , /* no     no          ---- -   used internally by scan */
		/*                             and in free list    */
 cEndofSrc    , /* no    EOF   bin0   ---- -   must be != cNosym */
 cFreedNode   , /* no     no          ---- n   used in free list */

 cPROGRAM     , /*             bin2   ^bin r */
 cMODULE      , /*             bin2   ^bin r */
 cPRIORITY    , /*             bin2   ^bin r */
 cGLOBAL      , /*                    ^sym r is USE GLOBAL in bintab */
 cUSE         , /*                    ^sym r */
 cREALCONSTANTCONVERSION , /*  bin1   ---- r */
 cLABEL       , /*             bin1   ---- r */
 cCONST       , /*             bin2   ^bin r */
 cTYPE        , /*             bin2   ^bin r */
 cPACKED      , /*                    ---- r */
 cFILE        , /*             bin1   ---- r */
 cSET         , /*             bin1   ---- r */
 cRECORD      , /*             bin2   ---- r */
 cCASE        , /*             bin2   ^bin r */
 cDYNAMIC     , /*             bin2   ^bin r */
 cARRAY       , /*             bin2   ^bin r */
 cOF          , /*                    ----   */
 cVAR         , /*             bin2   ^bin r */
 cPROCEDURE   , /*             bin2   ^bin r */
 cFUNCTION    , /*             bin2   ^bin r */
 cOPERATOR    , /*             bin2   ^bin r */
 cFORWARD     , /*             bin0   ---- - */
 cEXTERNAL    , /*             bin1   ---- r */

 cBEGIN       , /*         {   bin2   ^bin r */
 cEND         , /*         }          ---- - */
 cFOR         , /*       for   bin2   ^bin r */
 cTO          , /*             bin2   ^bin r */
 cDOWNTO      , /*             bin2   ^bin r */
 cDO          , /*             bin2   ---- - */
 cInit = cDO  ,
 cSUM         , /*                    ---- - */
 cGOTO        , /*             bin1   ---- r */
 cIF          , /*        if   bin2   ^bin r */
 cTHEN        , /*                    ---- - */
 cELSE        , /*      else   bin2   ^bin r */
 cREPEAT      , /*        do   bin2   ^bin r */
 cUNTIL       , /*                    ---- - */
 cWHILE       , /*     while   bin2   ^bin r */
 cWITH        , /*             bin2   ^bin r */
 cNew         , /*    malloc   bin2   ^bin r */ /* pseudo keyword */
 cDispose     , /*      free   bin2   ^bin r */ /* pseudo keyword */
 cNIL         , /*         0   bin0   ---- - */

 cString      , /* STRING      bin1   ---- r */ /* pseudo keyword */
 cSizeof      , /*    sizeof   bin1   ---- r */ /* pseudo keyword */
 cSemicolon   , /* ;       ;   list   ^bin n */
 cColon       , /* :           bin2   ^bin r */
 cComma       , /* ,       ,   list   ^bin n */
 cLParent     , /* (           bin2   ^bin n */
 cEnum = cLParent ,                          /* used for data types */
 cRParent     , /* )           bin2   ^bin r */ /* cast op in C */
 cPCast       , /*       * )   bin2   ^bin r */ /* C pointer cast Operator */
 cLBrack      , /* [           bin2   ^bin r */
 cArrVar      , /* [           bin2   ^bin r */ /* another priority */
 cRBrack      , /* ]                  ^bin r */

 cLParD       , /* (<          bin1   ---- r */
 cLParU       , /* (>          bin1   ---- r */
 cDot         , /* #           bin1   ---- r */
 cDotN        , /* #*          bin1   ---- r */
 cDotD        , /* #<          bin1   ---- r */
 cDotU        , /* #>          bin1   ---- r */
 cDotS        , /* ##          bin1   ---- r */
 cTill        , /* ..          bin2   ^bin r */
 cPoint       , /* .       .   bin2   ^bin r */
 cArrow       , /* ^       *   bin2?  ^bin - */
 cPArrow      , /* ^.     ->   bin2   ^bin r */
 cIrange      , /* *           bin0   ---- - */
 cAssign      , /* :=      =   bin2   ^bin r */

 cIN          , /* IN          bin2   ^bin r */
 cEQ          , /* =      ==   bin2   ^bin r */
 cNE          , /* <>     !=   bin2   ^bin r */
 cGE          , /* >=     >=   bin2   ^bin r */
 cGT          , /* >       >   bin2   ^bin r */
 cLE          , /* <=     <=   bin2   ^bin r */
 cLT          , /* <       <   bin2   ^bin r */
/* cStar      ,    *       *   bin2   ^bin r */
 cPlus        , /* +       +   bin2   ^bin r */
 cMinus       , /* -       -   bin2   ^bin r */
 cMult        , /* *       *   bin2   ^bin r */
 cDiv         , /* /       /   bin2   ^bin r */
 cDisj        , /* ><          bin2   ^bin r */
 cConj        , /* **          bin2   ^bin r */
 cCohu        , /* +*          bin2   ^bin r */
 cPlusD       , /* +<          bin2   ^bin r */
 cMinusD      , /* -<          bin2   ^bin r */
 cMultD       , /* *<          bin2   ^bin r */
 cDivD        , /* /<          bin2   ^bin r */
 cPlusU       , /* +>          bin2   ^bin r */
 cMinusU      , /* ->          bin2   ^bin r */
 cMultU       , /* *>          bin2   ^bin r */
 cDivU        , /* />          bin2   ^bin r */
 cOR          , /* OR      |   bin2   ^bin r */
 cAND         , /* AND     &   bin2   ^bin r */
 cDIV         , /* DIV     /   bin2   ^bin r */
 cMOD         , /* MOD     %   bin2   ^bin r */
 cMPlus       , /* +           bin1   ---- r */
 cMMinus      , /* -       -   bin1   ---- r */
 cAdres       , /*         &   bin1   ---- r */ /* C address Operator */
 cNOT         , /* NOT     !   bin1   ---- r */

 cReldmy      , /*             bin2   ^bin r */
 cAdddmy      , /*             bin2   ^bin r */
 cMuldmy      , /*             bin2   ^bin r */
 cRelop       , /* userdef     relop  ^sym r */
 cAddop       , /* userdef     addop  ^sym r */
 cMulop       , /* userdef     mulop  ^sym r */
 cMonop       , /* userdef     monop  ^sym r */

 cIcon        , /* -1          icon   valu - */
 cCcon        , /* 'c'   'c'   ccon   valu - */
 cScon        , /* 'ccccc'     ccon   ^str - */
/* cS2con     *//* 'cc'  "cc"  ccon   valu - */
/* cS3con     *//* 'ccc'       ccon   valu - */
/* cS4con     *//* 'cccc'      ccon   valu - */
 cRcon        , /* 1.1         rcon   ^str - */
 cBcon        , /* TRUE        bcon   valu - */

 cOpptr       , /* userdef     opptr  ^op  r */
 cBlkptr      , /* begin       blkptr ^bl  r */

 cUntemp      , /* utemp       bin2   ^bin r  generates untemp statements */
 cFree        , /* ufree       bin2   ^bin r  generates free statements */

 cCsymbol     , /* userdef     monop  ^sym n */
 cTemptype    , /* userdef     monop  ^sym n  is data type */
 cRecelem     , /* userdef     monop  ^sym n  in records only */
 cTempused    , /* userdef     monop  ^sym n */
 cTempvar     , /* userdef     monop  ^sym n  in declarations only */
 cRegister    , /* userdef     monop  ^sym n  in declarations only */
 cArrsym      , /* userdef [   index  ^sym r */
 cCall        , /* userdef (   call   ^sym r */
 cVarparam    , /* userdef ,   idli   ^id  n   indicates VAR parameter */
					     /* replaces cIdlist */
 cIdlist      , /* userdef ,   idli   ^id  n */
 cRout        , /* userdef (   rout   ^id  r */
 cArry        , /* userdef [   arry   ^id  r */
 cIdent       , /* userdef     id     ^id  - */

 cFALSE       , /*         0   id     ^id  - */
 cTRUE        , /*         1   id     ^id  - */
 cMAXINT      , /*             id     ^id  - */
 cBOOLEAN     , /*      char   id     ^id  - */
 cCHAR        , /*      char   id     ^id  - */
 cINTEGER     , /*       int   id     ^id  - */
 cREAL        , /*     float   id     ^id  - */
 cTEXT        , /*             id     ^id  - */
 cDOTPRECISION, /*             id     ^id  - */
 cIDOTPRECISION, /*            id     ^id  - */
 cCDOTPRECISION, /*            id     ^id  - */
 cCIDOTPRECISION, /*           id     ^id  - */
 cSTRING      , /*             arry   ^id  r */
 cINPUT       , /*             id     ^id  - */
 cOUTPUT      , /*             id     ^id  - */
 cLBOUND      , /*             rout   ^id  r */
 cLB          , /*             rout   ^id  r */
 cUBOUND      , /*             rout   ^id  r */
 cUB          , /*             rout   ^id  r */
 cSUCC        , /*        ++   rout   ^id  r */
 cPRED        , /*        --   rout   ^id  r */
 cCHR         , /*             rout   ^id  r */
 cORD         , /*             rout   ^id  r */
 cROUND       , /*             rout   ^id  r */
 cTRUNC       , /*             rout   ^id  r */
 cODD         , /*             rout   ^id  r */
 cLENGTH      , /*     strlen  rout   ^id  r */
 cMAXLENGTH   , /*             rout   ^id  r */
 cPOS         , /*             rout   ^id  r */
 cSUBSTRING   , /*             rout   ^id  r */
 cSCALP       , /*             rout   ^id  r */
 cEOF         , /*       EOF   rout   ^id  r */
 cEOLN        , /*        \n   rout   ^id  r */
 cRESET       , /*             rout   ^id  r */
 cREWRITE     , /*             rout   ^id  r */
 cGET         , /*             rout   ^id  r */
 cPUT         , /*             rout   ^id  r */
 cREAD        , /*             rout   ^id  r */
 cREADLN      , /*             rout   ^id  r */
 cWRITE       , /*             rout   ^id  r */
 cWRITELN     , /*             rout   ^id  r */
 cPAGE        , /*             rout   ^id  r */
 cIMAGE       , /*             rout   ^id  r */
 cPACK        , /*             rout   ^id  r */
 cUNPACK      , /*             rout   ^id  r */
 cNEW         , /*             rout   ^id  r */
 cDISPOSE     , /*             rout   ^id  r */
 cALLOCATE    , /*             rout   ^id  r */
 cALLOCATED   , /*             rout   ^id  r */
 cFREE        , /*             rout   ^id  r */

 cABS         , /*             rout   ^id  r */
 cARCTAN      , /*             rout   ^id  r */
 cCINTERVAL   , /*             id     ^id  - */
 cCOMPLEX     , /*             id     ^id  - */
 cCOS         , /*             rout   ^id  r */
 cEXP         , /*             rout   ^id  r */
 cIM          , /*             rout   ^id  r */
 cINF         , /*             rout   ^id  r */
 cINTERVAL    , /*             id     ^id  - */
 cIVAL        , /*             rout   ^id  r */
 cLN          , /*             rout   ^id  r */
 cRE          , /*             rout   ^id  r */
 cRVAL        , /*             rout   ^id  r */
 cSIN         , /*             rout   ^id  r */
 cSQR         , /*             rout   ^id  r */
 cSQRT        , /*             rout   ^id  r */
 cSUP         , /*             rout   ^id  r */

 cRVECTOR     , /*             id     ^id  - */
 cCVECTOR     , /*             id     ^id  - */
 cIVECTOR     , /*             id     ^id  - */
 cCIVECTOR    , /*             id     ^id  - */
 cRMATRIX     , /*             id     ^id  - */
 cCMATRIX     , /*             id     ^id  - */
 cIMATRIX     , /*             id     ^id  - */
 cCIMATRIX    , /*             id     ^id  - */
 cINTVAL      , /*             rout   ^id  r */
 cCOMPL       , /*             rout   ^id  r */
 cNULL        , /*             rout   ^id  r */
 cID          , /*             rout   ^id  r */
 cCONJ        , /*             rout   ^id  r */
 cTRANSP      , /*             rout   ^id  r */
 cHERM        , /*             rout   ^id  r */
 cBUFSIZ      , /*             C identifier  */
 cPTRCHAR     , /*     char*   id     ^id  - */  /* C data type */
 cSIZEOF      , /*    sizeof   id     ^id  r */  /* new standard fct */
 cINT_OF_C    , /*       int   id     ^id  r */  /* C data type */
 cFILE_OF_C   , /*     FILE*   id     ^id  r */  /* C data type */
 cPOINTER_OF_C, /*     void*   id     ^id  r */  /* C data type */
 cHighest     } /* dummy for maximal symbol code */
symctype ;  /* variables of this type occupy 4 byte */

					/* data type for symbol code */
					/*---------------------------*/

						/*-------------------------*/
global short int prio [cHighest+1] ;            /* prioritytable  p r i o  */
global short int cprio [cHighest+1] ;

typedef enum prio {                             /*  p r i o  values        */
	  priobegin     ,
	  priosemicolon ,
	  priotypes     ,
	  priostm       ,
	  priopar       ,
	  prioindex     ,
	  priocomma     ,
	  prioass       ,
	  priocolon     ,
	  priotill      ,
	  prio_orop     ,
	  prioandop     ,
	  prionotop     ,  /* obsolete ! */
	  priorelop     ,
	  prioaddop     ,
	  priomulop     ,
	  priomonop     ,
	  prioderef     ,  /* dereferencing in C has lower prio than "." */
	  priovar       ,
	  prioleaf      }  enumprio ;           /*  p r i o  values        */
						/*-------------------------*/

						/*---------------------------*/
						/* symbol sets  s y m _ i s  */
global struct {
	 unsigned twochars    :1 ; /* 2 char symbols used by scanner    */
	 unsigned identifier  :1 ; /* left is idptr                     */
	 unsigned symbol      :1 ; /* left is symptr                    */
	 unsigned Operator    :1 ;
	 unsigned operand     :1 ; /* symbol to be appended by expressi */
	 unsigned variable    :1 ;
	 unsigned constant    :1 ;
	 unsigned userdefined :1 ;
	 unsigned declaration :1 ;
	 unsigned type        :1 ;
	 unsigned statement   :1 ;
	 unsigned dummy       :1 ;
	 unsigned leaf        :1 ;
	 unsigned leftbin     :1 ; /* left is binptr                    */
	 unsigned rightbin    :1 ; /* right or next pointer exists      */
	 unsigned value       :1 ; /* left is integer value             */
	 unsigned rename      :1 ; /* identifier to be renamed          */
	 unsigned scalartype  :1 ; /* index data type                   */
	 unsigned list        :1 ;
	 unsigned arraytype   :1 ; /* data type, implemented as array   */
				   /*  in C */
	 unsigned stmstopper  :1 ; /* stopsymbols for statements        */
	 unsigned std_procdre :1 ; /* standardproc. called in statements  */
	 unsigned leftexpr    :1 ; /* left is not data type */
	 unsigned rightexpr   :1 ; /* right is not data type */
	 unsigned lvalue      :1 ; /* subtree is variable in C		*/
       }  sym_is [cHighest+1] ;                 /*              s y m _ i s  */
						/*---------------------------*/

						/*------------------------*/
						/* Pascal representation: */
global char * symptext [cHighest+1] ;           /*  s y m p t e x t       */
						/* C representation: */
global char * symctext [cHighest+1] ;           /*  s y m c t e x t  */
						/*-------------------*/

#ifdef LINT_ARGS
global void       opensrc    (char * filename) ;
global void       closesrc   (void) ;
global symctype   getsym     (char * string[]);
global void       symsetorg  (boolean);
global void       symkeyword (symctype, boolean);
global short int  symprio    (symctype);
global void       scaninit   (void);

#else

global void       opensrc () ;
global void       closesrc () ;
global symctype   getsym ();
global void       symsetorg ();
global void       symkeyword ();
global short int  symprio();
global void       scaninit ();
#endif

