home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / MODEM / UWPC201.ZIP / UW-SRC.ZIP / TERMCC.Y < prev    next >
Encoding:
Text File  |  1991-07-25  |  10.4 KB  |  420 lines

  1. /*-------------------------------------------------------------------------
  2.  
  3.   TERMCC.Y - Grammar for the Termcap Compiler.
  4.  
  5.     This file is part of the Termcap Compiler source code.
  6.     Copyright (C) 1990-1991  Rhys Weatherley
  7.  
  8.     This program is free software; you can redistribute it and/or modify
  9.     it under the terms of the GNU General Public License as published by
  10.     the Free Software Foundation; either version 1, or (at your option)
  11.     any later version.
  12.  
  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.
  17.  
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22.   Revision History:
  23.   ================
  24.  
  25.    Version  DD/MM/YY  By  Description
  26.    -------  --------  --  --------------------------------------
  27.      1.0    23/03/91  RW  Original Version of TERMCC.Y
  28.      1.1    25/05/91  RW  Add more instructions to support ANSI
  29.      1.2    25/07/91  RW  Added the "client" instruction
  30.  
  31.   You may contact the author by:
  32.   =============================
  33.  
  34.    e-mail: rhys@cs.uq.oz.au
  35.      mail: Rhys Weatherley
  36.        5 Horizon Drive
  37.        Jamboree Heights
  38.        Queensland 4074
  39.        Australia
  40.  
  41. -------------------------------------------------------------------------*/
  42.  
  43. %token CMD_SEND CMD_SEND52 CMD_CR CMD_LF CMD_BS CMD_BSWRAP CMD_MOVE
  44. %token CMD_CLEAR CMD_CLREOL CMD_CLREOS CMD_CLRSOL CMD_CLRSOS CMD_INSLINE
  45. %token CMD_DELLINE CMD_INSCHAR CMD_DELCHAR CMD_SETATTR CMD_SETSCRL CMD_GETCH
  46. %token CMD_SETX CMD_SETY CMD_LOAD CMD_ADD CMD_SUB CMD_CMP CMD_SWITCH
  47. %token CMD_JE CMD_JNE CMD_JA CMD_JAE CMD_JB CMD_JBE CMD_JMP CMD_JSR CMD_RET
  48. %token CMD_REMOTE CMD_ESCAPE CMD_ENDSW CMD_TAB CMD_BELL CMD_SAVEXY CMD_RESTXY
  49. %token CMD_GETXY CMD_GETX CMD_GETY CMD_SCRLUP CMD_SCRLDN CMD_SET CMD_RESET
  50. %token CMD_TEST CMD_NAME CMD_KEY CMD_ENDKEYS CMD_RESARR CMD_GETARG CMD_GETA
  51. %token CMD_DEC CMD_SHIFT CMD_SETC CMD_SAVEATTR CMD_RESTATTR CMD_INSBLANK
  52. %token CMD_CLIENT
  53. %token NUMBER STRING IDENT COMMA SEMI COLON VAL_WIDTH VAL_HEIGHT
  54.  
  55. /* YYSTYPE is the semantic type for non-terminals and terminals */
  56.  
  57. %{
  58.  
  59. #include "opcodes.h"        /* Abstract machine opcodes */
  60. #include "symbols.h"        /* Symbol manipulation routines */
  61. #include <stdio.h>
  62.  
  63. #define yyoverflow    yyover    /* Where to go on overflows */
  64. /* #define    YYDEBUG        1 */
  65.  
  66. extern    int    lexline;
  67.  
  68. int    numerrors=0;        /* Number of errors that occurred */
  69. int    numwarnings=0;        /* Number of warnings that occurred */
  70.  
  71. #define    BUFFER_SIZE    16384
  72.  
  73. unsigned char    OutBuffer[BUFFER_SIZE];
  74. int    OutBufPosn=0;
  75.  
  76. char    Buffer[100];
  77.  
  78. %}
  79.  
  80. %%
  81.  
  82. program:  cmds
  83.     ;
  84.  
  85. cmds:      cmds cmd
  86.     |
  87.     ;
  88.  
  89. cmd:      CMD_SEND
  90.         { genbyte (OP_SEND); }
  91.     | CMD_SEND52
  92.         { genbyte (OP_SEND52); }
  93.     | CMD_CR
  94.         { genbyte (OP_CR); }
  95.     | CMD_LF
  96.         { genbyte (OP_LF); }
  97.     | CMD_BS
  98.         { genbyte (OP_BS); }
  99.     | CMD_TAB
  100.         { genbyte (OP_TAB); }
  101.     | CMD_BELL
  102.         { genbyte (OP_BELL); }
  103.     | CMD_BSWRAP
  104.         { genbyte (OP_BSWRAP); }
  105.     | CMD_MOVE
  106.         { genbyte (OP_MOVE); }
  107.     | CMD_CLEAR
  108.         { genbyte (OP_CLEAR); }
  109.     | CMD_CLREOL
  110.         { genbyte (OP_CLREOL); }
  111.     | CMD_CLREOS
  112.         { genbyte (OP_CLREOS); }
  113.     | CMD_CLRSOL
  114.         { genbyte (OP_CLRSOL); }
  115.     | CMD_CLRSOS
  116.         { genbyte (OP_CLRSOS); }
  117.     | CMD_INSLINE
  118.         { genbyte (OP_INSLINE); }
  119.     | CMD_DELLINE
  120.         { genbyte (OP_DELLINE); }
  121.     | CMD_INSCHAR
  122.         { genbyte (OP_INSCHAR); }
  123.     | CMD_DELCHAR
  124.         { genbyte (OP_DELCHAR); }
  125.     | CMD_SETATTR NUMBER
  126.         { genbyte (OP_SETATTR); genbyte ($2); }
  127.     | CMD_SETATTR
  128.         { genbyte (OP_SETATTR_ACC); }
  129.     | CMD_SETSCRL NUMBER
  130.         { genbyte (OP_SETSCRL); genbyte ($2); }
  131.     | CMD_SETSCRL
  132.         { genbyte (OP_SETSCRL_ACC); }
  133.     | CMD_GETCH
  134.         { genbyte (OP_GETCH); }
  135.     | CMD_SETX
  136.         { genbyte (OP_SETX); }
  137.     | CMD_SETY
  138.         { genbyte (OP_SETY); }
  139.     | CMD_LOAD VAL_WIDTH
  140.         { genbyte (OP_LOAD_WIDTH); }
  141.     | CMD_LOAD VAL_HEIGHT
  142.         { genbyte (OP_LOAD_HEIGHT); }
  143.     | CMD_LOAD NUMBER
  144.         { genalt (OP_LOAD,OP_LOAD_WORD,$2); }
  145.     | CMD_ADD VAL_WIDTH
  146.         { genbyte (OP_ADD_WIDTH); }
  147.     | CMD_ADD VAL_HEIGHT
  148.         { genbyte (OP_ADD_HEIGHT); }
  149.     | CMD_ADD NUMBER
  150.         { genalt (OP_ADD,OP_ADD_WORD,$2); }
  151.     | CMD_SUB VAL_WIDTH
  152.         { genbyte (OP_SUB_WIDTH); }
  153.     | CMD_SUB VAL_HEIGHT
  154.         { genbyte (OP_SUB_HEIGHT); }
  155.     | CMD_SUB NUMBER
  156.         { genalt (OP_SUB,OP_SUB_WORD,$2); }
  157.     | CMD_CMP VAL_WIDTH
  158.         { genbyte (OP_CMP_WIDTH); }
  159.     | CMD_CMP VAL_HEIGHT
  160.         { genbyte (OP_CMP_HEIGHT); }
  161.     | CMD_CMP NUMBER
  162.         { genalt (OP_CMP,OP_CMP_WORD,$2); }
  163.     | CMD_SWITCH switches CMD_ENDSW
  164.     | CMD_JE ref
  165.         { genbyte (OP_JE); doaddref ($2,OutBufPosn); }
  166.     | CMD_JNE ref
  167.         { genbyte (OP_JNE); doaddref ($2,OutBufPosn); }
  168.     | CMD_JA ref
  169.         { genbyte (OP_JA); doaddref ($2,OutBufPosn); }
  170.     | CMD_JAE ref
  171.         { genbyte (OP_JAE); doaddref ($2,OutBufPosn); }
  172.     | CMD_JB ref
  173.         { genbyte (OP_JB); doaddref ($2,OutBufPosn); }
  174.     | CMD_JBE ref
  175.         { genbyte (OP_JBE); doaddref ($2,OutBufPosn); }
  176.     | CMD_JMP ref
  177.         { genbyte (OP_JMP); doaddref ($2,OutBufPosn); }
  178.     | CMD_JSR ref
  179.         { genbyte (OP_JSR); doaddref ($2,OutBufPosn); }
  180.     | CMD_RET
  181.         { genbyte (OP_RET); }
  182.     | CMD_REMOTE
  183.         { genbyte (OP_REMOTE); }
  184.     | CMD_ESCAPE NUMBER
  185.         { genalt (OP_ESCAPE,OP_ESCAPE_WORD,$2); }
  186.     | CMD_SAVEXY
  187.         { genbyte (OP_SAVEXY); }
  188.     | CMD_RESTXY
  189.         { genbyte (OP_RESTXY); }
  190.     | CMD_GETXY
  191.         { genbyte (OP_GETXY); }
  192.     | CMD_GETX
  193.         { genbyte (OP_GETX); }
  194.     | CMD_GETY
  195.         { genbyte (OP_GETY); }
  196.     | CMD_SCRLUP
  197.         { genbyte (OP_SCRLUP); }
  198.     | CMD_SCRLDN
  199.         { genbyte (OP_SCRLDN); }
  200.     | CMD_SET NUMBER
  201.         { genbyte (OP_SET); genbyte ($2); }
  202.     | CMD_RESET NUMBER
  203.         { genbyte (OP_RESET); genbyte ($2); }
  204.     | CMD_TEST NUMBER
  205.         { genbyte (OP_TEST); genbyte ($2); }
  206.     | CMD_NAME STRING
  207.         { genstring ($2); }
  208.     | CMD_KEY NUMBER COMMA STRING
  209.         { genword ($2); genstring2 ($4); }
  210.     | CMD_ENDKEYS
  211.         { genword (0); }
  212.     | CMD_RESARR
  213.         { genbyte (OP_RESARR); }
  214.     | CMD_GETARG
  215.         { genbyte (OP_GETCH); genbyte (OP_GETARG); }
  216.     | CMD_GETA NUMBER COMMA NUMBER
  217.         { genbyte (OP_GETA); genbyte ($2); genbyte ($4); }
  218.     | CMD_DEC
  219.         { genbyte (OP_DEC); }
  220.     | CMD_SHIFT
  221.         { genbyte (OP_SHIFT); }
  222.     | CMD_SETC
  223.         { genbyte (OP_SETC); }
  224.     | CMD_SAVEATTR
  225.         { genbyte (OP_SAVEATTR); }
  226.     | CMD_RESTATTR
  227.         { genbyte (OP_RESTATTR); }
  228.     | CMD_INSBLANK
  229.         { genbyte (OP_INSBLANK); }
  230.     | CMD_CLIENT
  231.         { genbyte (OP_CLIENT); }
  232.     | IDENT COLON
  233.         { addposn ($1,OutBufPosn); }
  234.     | error
  235.         { parserr ("Illegal instruction"); }
  236.     ;
  237.  
  238. ref:      IDENT
  239.         { $$ = $1; }
  240.     ;
  241.  
  242. switches: switches switch
  243.     |
  244.     ;
  245.  
  246. switch:      NUMBER COMMA ref
  247.         { genalt (OP_SWITCH,OP_SWITCH_WORD,$1);
  248.           doaddref ($3,OutBufPosn); }
  249.     | error
  250.         { parserr ("Illegal switch"); }
  251.     ;
  252.  
  253. %%
  254.  
  255. /* Indicate an overflow of the parse stack and abort */
  256. yyover ()
  257. {
  258.   parsefatal ("Parse stack overflow");
  259. }
  260.  
  261. /* Report an error from FLEX/BISON - ignored in this version */
  262. yyerror (msg)
  263. char    *msg;
  264. {
  265.   /* fprintf (stderr,"%s (%d)\n",msg,lexline); */
  266. }
  267.  
  268. /* Report a parsing error, and update the number of errors */
  269. parserr (msg)
  270. char    *msg;
  271. {
  272.   fprintf (stderr,"Error (%d): %s\n",lexline,msg);
  273.   ++numerrors;
  274. }
  275.  
  276. /* Report a warning, and update the number of warnings */
  277. parsewarn (msg)
  278. char    *msg;
  279. {
  280.   fprintf (stderr,"Warning (%d): %s\n",lexline,msg);
  281.   ++numwarnings;
  282. }
  283.  
  284. /* Report a fatal parsing error and abort */
  285. parsefatal (msg)
  286. char    *msg;
  287. {
  288.   fprintf (stderr,"Fatal (%d): %s\n",lexline,msg);
  289.   exit (1);
  290. }
  291.  
  292. /* Generate a single byte of code to the output buffer */
  293. genbyte    (byte)
  294. int    byte;
  295. {
  296.   OutBuffer[OutBufPosn++] = byte;
  297. } /* genbyte */
  298.  
  299. /* Generate a 2-byte word of code to the output buffer */
  300. genword (word)
  301. int    word;
  302. {
  303.   genbyte (word & 255);
  304.   genbyte ((word >> 8) & 255);
  305. } /* genword */
  306.  
  307. /* Generate an instruction that has alternate byte and word types */
  308. genalt (inst1,inst2,value)
  309. int    inst1,inst2,value;
  310. {
  311.   if (value & 0xFF00)
  312.     {
  313.       genbyte (inst2);
  314.       genword (value);
  315.     } /* then */
  316.    else
  317.     {
  318.       genbyte (inst1);
  319.       genbyte (value);
  320.     } /* else */
  321. } /* genalt */
  322.  
  323. /* Generate a string from the symbol table */
  324. genstring (ident)
  325. int    ident;
  326. {
  327.   char *str;
  328.   str = getname (ident);
  329.   while (*str)
  330.     genbyte (*str++);
  331.   genbyte (0);
  332. } /* genstring */
  333.  
  334. /* Generate a string starting with a length */
  335. genstring2 (ident)
  336. int    ident;
  337. {
  338.   char *str;
  339.   str = getname (ident);
  340.   genbyte (strlen (str) + 2);    /* Record string length + 2 for speed */
  341.   while (*str)            /* when translating the keys */
  342.     genbyte (*str++);
  343.   genbyte (0);
  344. } /* genstring2 */
  345.  
  346. /* Add a reference to a label and set links correctly */
  347. doaddref (ident,address)
  348. int    ident,address;
  349. {
  350.   int old;
  351.   old = getref (ident);
  352.   genbyte (old & 255);
  353.   genbyte ((old >> 8) & 255);
  354.   addref (ident,address);
  355. } /* doaddref */
  356.  
  357. /* Fix up all label references */
  358. fixups ()
  359. {
  360.   int ident,address,posn,temp;
  361.   ident = firstref ();
  362.   while (ident != -1)
  363.     {
  364.       address = getref (ident);
  365.       posn = getposn (ident);
  366.       if (posn == -1)
  367.         {
  368.       sprintf (Buffer,"Label not defined: %s",getname (ident));
  369.       parserr (Buffer);
  370.     } /* then */
  371.        else
  372.         while (address != 0)
  373.           {
  374.         temp = OutBuffer[address] | (OutBuffer[address + 1] << 8);
  375.         OutBuffer[address] = posn & 255;
  376.         OutBuffer[address + 1] = (posn >> 8) & 255;
  377.         address = temp;
  378.       } /* while */
  379.       ident = nextref ();
  380.     } /* while */
  381.   if ((ident = addident ("start")) == -1 ||
  382.       (posn = getposn (ident)) == -1)
  383.     {
  384.       parserr ("No entry point 'start' defined");
  385.       exit (1);
  386.     } /* if */
  387.   if ((ident = addident ("keys")) == -1 ||
  388.       (posn = getposn (ident)) == -1)
  389.     {
  390.       parserr ("No entry point 'keys' defined");
  391.       exit (1);
  392.     } /* if */
  393. } /* fixups */
  394.  
  395. extern    char    OutFileName[];
  396.  
  397. /* Dump the generated code to the output file */
  398. dumpcode ()
  399. {
  400.   FILE *outfile;
  401.   int posn,keys,index;
  402.   if ((outfile = fopen (OutFileName,"wb")) == NULL)
  403.     {
  404.       fprintf (stderr,"\n");
  405.       perror (OutFileName);
  406.       exit (1);
  407.     } /* if */
  408.   posn = getposn (addident ("start"));
  409.   fputc (posn & 255,outfile);
  410.   fputc ((posn >> 8) & 255,outfile);
  411.   keys = getposn (addident ("keys"));
  412.   fputc (keys & 255,outfile);
  413.   fputc ((keys >> 8) & 255,outfile);
  414.   fputc (UW_TERM_VERSION & 255,outfile);
  415.   fputc ((UW_TERM_VERSION >> 8) & 255,outfile);
  416.   for (index = 0;index < OutBufPosn;++index)
  417.     fputc (OutBuffer[index],outfile);
  418.   fclose (outfile);
  419. } /* dumpcode */
  420.