home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Interactive Guide
/
c-cplusplus-interactive-guide.iso
/
c_ref
/
csource4
/
238_01
/
interp.c
< prev
next >
Wrap
Text File
|
1987-07-28
|
16KB
|
586 lines
#include <stdio.h>
#include <ctype.h>
#include <setjmp.h>
#include <stdarg.h>
/*==============================================================*
* *
* This program is used to interpret GRAD functions in *
* restricted C syntax of function call. *
* The method used is recursive descend. *
* *
*==============================================================*/
#define unreadc(ch) ungetc(ch,infile)
/* maximum number of parameter for a single function */
#define MAXPARM 9
#define Boolean int
#define TRUE 1
#define FALSE 0
#define ERROR (-1)
enum Ptype {
UNDEFINED, INTG, STRG
};
struct funcdesc {
char *name;
enum Ptype rettype;
int nuparm;
enum Ptype parmstype[MAXPARM];
};
/* The function names must arrange in ascending order */
struct funcdesc FUNCTIONS[] =
{ { "Arc1", UNDEFINED, 4, { INTG, INTG, INTG, INTG } } ,
{ "Arc2", UNDEFINED, 5, { INTG, INTG, INTG, INTG, INTG } },
{ "ArcPoint", UNDEFINED, 4, { INTG, INTG, INTG, INTG } },
{ "BlockCopy", INTG, 8,
{ INTG, INTG, INTG, INTG, INTG, INTG, INTG, INTG } },
{ "BlockLoad", INTG, 3, { INTG, INTG, STRG } },
{ "BlockSave", UNDEFINED, 5, { INTG, INTG, STRG, INTG, INTG } },
{ "Box", UNDEFINED, 6, { INTG, INTG, INTG, INTG, INTG, INTG } } ,
{ "Circle", UNDEFINED, 3, { INTG, INTG, INTG } } ,
{ "CreateFrame", INTG, 2, { INTG, INTG } } ,
{ "Dot", UNDEFINED, 2, { INTG, INTG } } ,
{ "Draw", UNDEFINED, 3, { STRG, INTG, INTG } },
{ "Earc1", UNDEFINED, 5, { INTG, INTG, INTG, INTG, INTG } } ,
{ "Earc2", UNDEFINED, 6, { INTG, INTG, INTG, INTG, INTG, INTG } },
{ "Ellipse", UNDEFINED, 4, { INTG, INTG, INTG, INTG } } ,
{ "EnvRsto", UNDEFINED, 2, { INTG, INTG } },
{ "EnvSave", INTG, 1, { INTG } },
{ "FillCircle", UNDEFINED, 3, { INTG, INTG, INTG } },
{ "FillEllipse",UNDEFINED, 4, { INTG, INTG, INTG, INTG } },
{ "HorzLine", UNDEFINED, 4, { INTG, INTG, INTG, INTG } } ,
{ "Line", UNDEFINED, 4, { INTG, INTG, INTG, INTG } } ,
{ "LoadFont", INTG, 1, { STRG } } ,
{ "NextXY", UNDEFINED, 2, { INTG, INTG } },
{ "PatternFill", UNDEFINED, 4, { INTG, INTG, STRG, INTG } },
{ "PlotType", INTG, 1, { INTG } } ,
{ "PrintFrame", UNDEFINED, 5, { INTG, STRG, INTG, INTG, INTG } } ,
{ "PrintPage", UNDEFINED, 0 },
{ "ReadStr", UNDEFINED, 7,
{ STRG, INTG, INTG, INTG, INTG, INTG, INTG } },
{ "Rectangle", UNDEFINED, 4, { INTG, INTG, INTG, INTG } } ,
{ "RelOrg", UNDEFINED, 2, { INTG, INTG } } ,
{ "RemvFont", INTG, 1, { INTG } } ,
{ "RemvFrame", INTG, 1, { INTG } } ,
{ "ResetWin", UNDEFINED, 0 } ,
{ "SelectFont", INTG, 1, { INTG } },
{ "SelectFrame", INTG, 1, { INTG } } ,
{ "SetOrg", UNDEFINED, 2, { INTG, INTG } } ,
{ "SetStyle", UNDEFINED, 1, { INTG } } ,
{ "SetWin", UNDEFINED, 4, { INTG, INTG, INTG, INTG } } ,
{ "SolidFill", UNDEFINED, 2, { INTG, INTG } },
{ "VertLine", UNDEFINED, 4, { INTG, INTG, INTG, INTG } },
{ "WriteStr", UNDEFINED, 7, { INTG, INTG, INTG, INTG, STRG, INTG, INTG }},
{ "XHLine", INTG, 5, { INTG, INTG, INTG, INTG, INTG } }
};
#define NFUNC (sizeof(FUNCTIONS) / sizeof(struct funcdesc))
char *ERRMSG[]= {
"Undefined Error Number\n",
"Variable/Function name expected\n",
"Variable name %s not found\n",
"Function name or expression expected\n",
"Function name %s not found\n",
"'(' expected after function name\n",
"Type if parameter %d is different from definition\n",
"')' expected after the parameters of a function\n",
"Less parameter than expected\n",
"End of string not detected before end of line\n",
"',' expected after a parameter\n",
"'C' or 'L' expected after '['\n",
"']' expected after a line or column specification\n",
"Identifier Expected\n"
};
#define NUOFERROR (sizeof(ERRMSG) / sizeof(char *))
char LINE[160];
int PATTERN[]={
0x5555, 0xaaaa, 0x5555, 0xaaaa,
0x5555, 0xaaaa, 0x5555, 0xaaaa,
0x5555, 0xaaaa, 0x5555, 0xaaaa,
0x5555, 0xaaaa, 0x5555, 0xaaaa,
0x8888, 0x4444, 0x2222, 0x1111,
0x8888, 0x4444, 0x2222, 0x1111,
0x8888, 0x4444, 0x2222, 0x1111,
0x8888, 0x4444, 0x2222, 0x1111,
0x1111, 0x2222, 0x4444, 0x8888,
0x1111, 0x2222, 0x4444, 0x8888,
0x1111, 0x2222, 0x4444, 0x8888,
0x1111, 0x2222, 0x4444, 0x8888
};
struct pval {
enum Ptype parmtype;
union {
int u_int;
char *u_strg;
} v;
};
struct pval RETVAL, *expr();
char IDNAME[12], CHARS[512], *PSPTR;
#define NUOFVAR 21
int nuofvar=NUOFVAR;
char VARNAME[NUOFVAR][12]={
"pattern1", "pattern2", "pattern3",
"line",
"font1", "font2", "font3", "font4",
"frame1", "frame2", "frame3",
"temp1", "temp2", "temp3",
"x1", "x2", "y1", "y2",
"env1", "env2", "env3"
};
struct pval VARTABLE[NUOFVAR];
int COL_OFFSET=0, ROW_OFFSET=0, LINE_HEIGHT=12, CHAR_WIDTH=12;
jmp_buf EOF_JMP, SYNERR_JMP;
int EOF_FLAG=0;
FILE *infile;
main(argc,argv)
int argc;
char *argv[];
{
int ret;
FILE *fopen();
if (argc <= 1) {
fprintf(stderr,"Usage: interp commandfile\n");
exit(1);
}
if ((infile=fopen(*(++argv), "r")) == (FILE *) NULL) {
fprintf(stderr,"interp: File not found\n");
exit(1);
}
GRADinit(); /* GRAD initialization function */
initvars(); /* initialize internal variables */
setgraph(); /* set graphics mode */
if (setjmp(EOF_JMP) != 0) {
/* return here on End_Of_File */
fclose(infile);
if (EOF_FLAG) {
printf("interp: Unexpected End Of File !\n");
}
getch();
settext();
cleanup(EOF_FLAG);
exit(0);
}
setjmp(SYNERR_JMP);
/* return here on syntax error */
for (;;) {
EOF_FLAG=0;
stmt();
}
}
/*------------------------------------------------------*
* read a single character from input file. *
* A string of comment is treated as a single space *
* Comments can be nested. *
*------------------------------------------------------*/
readc0()
{
int ch;
if ((ch=getc(infile)) == EOF) {
longjmp(EOF_JMP,1);
}
if (ch != '/') return(ch);
if ((ch=getc(infile)) == EOF) {
longjmp(EOF_JMP,1);
}
if (ch != '*') {
ungetc(ch,infile);
return('/');
}
/* begining of comment */
for (;;) {
if ((ch=readc0()) == '*') {
while((ch=readc0()) == '*') ;
if (ch=='/') return(' ');
}
}
}
/* skip all control characters except tab (0x09) and return (0x0d) */
/* but they will be returned as space (0x20) */
readc1()
{
int ch;
do {
ch=readc0();
if (isspace(ch)) return(' ');
} while (ch < 0x20);
return(ch);
}
/* read first non-blank character from input file */
readc2()
{
int ch;
do {
ch=readc1();
} while (ch==' ');
EOF_FLAG=1;
return(ch);
}
/* procdure to interpret a statement */
/* { } means optional, | means or
STMT := [ VARIABLE = ] FUNCTION
VARIABLE := identifier
FUNCTION := identifier ( PARAMETERS ) ;
PARAMETERS := PARAMETER , PARAMETERS | PARAMETER
PARAMETER := EXPRESSION
*/
stmt()
{
int ch, varidx,funcidx, nuparmoffunc, nparm;
struct pval *valptr, parmlist[MAXPARM];
PSPTR=CHARS;
varidx=-1; /* init */
if (!identifier()) { /* is first token an identifier */
synerr(1); /* syntax error 1 */
}
ch=readc2();
if (ch=='=') {
if ((varidx=findvar(I