home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 2
/
FFMCD02.bin
/
new
/
dev
/
misc
/
p2c
/
src
/
trans.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-12-21
|
41KB
|
1,543 lines
/* "p2c", a Pascal to C translator.
Copyright (C) 1989, 1990, 1991 Free Software Foundation.
Author's address: daveg@csvax.caltech.edu; 256-80 Caltech/Pasadena CA 91125.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation (any version).
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#define define_globals
#define PROTO_TRANS_C
#include "trans.h"
#include <time.h>
/* Roadmap:
trans.h Declarations for all public global variables, types,
and macros. Functions are declared in separate
files p2c.{proto,hdrs} which are created
mechanically by the makeproto program.
trans.c Main program. Parses the p2crc file. Also reserves
storage for public globals in trans.h.
stuff.c Miscellaneous support routines.
out.c Routines to handle the writing of C code to the output
file. This includes line breaking and indentation
support.
comment.c Routines for managing comments and comment lists.
lex.c Lexical analyzer. Manages input files and streams,
splits input stream into Pascal tokens. Parses
compiler directives and special comments. Also keeps
the symbol table.
parse.c Parsing and writing statements and blocks.
decl.c Parsing and writing declarations.
expr.c Manipulating expressions.
pexpr.c Parsing and writing expressions.
funcs.c Built-in special functions and procedures.
dir.c Interface file to "external" functions and procedures
such as hpmods and citmods.
hpmods.c Definitions for HP-supplied Pascal modules.
citmods.c Definitions for some Caltech-local Pascal modules.
(Outside of Caltech this file is mostly useful
as a large body of examples of how to write your
own translator extensions.)
p2crc Control file (read when p2c starts up).
p2c.h Header file used by translated programs.
p2clib.c Run-time library used by translated programs.
*/
Static Strlist *tweaksymbols, *synonyms;
Strlist *addmacros;
Static void initrc()
{
int i;
for (i = 0; i < numparams; i++) {
switch (rctable[i].kind) {
case 'S':
case 'B':
*((short *)rctable[i].ptr) = rctable[i].def;
break;
case 'I':
case 'D':
*((int *)rctable[i].ptr) = rctable[i].def;
break;
case 'L':
*((long *)rctable[i].ptr) = rctable[i].def;
break;
case 'R':
*((double *)rctable[i].ptr) = rctable[i].def/100.0;
break;
case 'U':
case 'C':
*((char *)rctable[i].ptr) = 0;
break;
case 'A':
*((Strlist **)rctable[i].ptr) = NULL;
break;
case 'X':
if (rctable[i].def == 1)
*((Strlist **)rctable[i].ptr) = NULL;
break;
}
rcprevvalues[i] = NULL;
}
tweaksymbols = NULL;
synonyms = NULL;
addmacros = NULL;
varmacros = NULL;
constmacros = NULL;
fieldmacros = NULL;
funcmacros = NULL;
}
Static int readrc(rcname, need)
char *rcname;
int need;
{
FILE *rc;
char buf[500], *cp, *cp2;
long val = 0;
int i;
Strlist *sl;
rc = fopen(rcname, "r");
if (!rc) {
if (need)
perror(rcname);
return 0;
}
while (fgets(buf, 500, rc)) {
cp = my_strtok(buf, " =\t\n");
if (cp && *cp != '#') {
upc(cp);
i = numparams;
while (--i >= 0 && strcmp(rctable[i].name, cp)) ;
if (i >= 0) {
if (rctable[i].kind != 'M') {
cp = my_strtok(NULL, " =\t\n");
if (cp && *cp == '#')
cp = NULL;
if (cp && (isdigit(*cp) || *cp == '-' || *cp == '+'))
val = atol(cp);
else
val = rctable[i].def;
}
switch (rctable[i].kind) {
case 'S':
*((short *)rctable[i].ptr) = val;
break;
case 'I':
*((int *)rctable[i].ptr) = val;
break;
case 'D':
*((int *)rctable[i].ptr) =
parsedelta(cp, rctable[i].def);
break;
case 'L':
*((long *)rctable[i].ptr) = val;
break;
case 'R':
if (cp && (isdigit(*cp) || *cp == '-' || *cp == '.'))
*((double *)rctable[i].ptr) = atof(cp);
else
*((double *)rctable[i].ptr) = rctable[i].def/100.0;
break;
case 'U':
if (cp)
upc(cp);
/* fall through */
case 'C':
val = rctable[i].def;
strncpy((char *)rctable[i].ptr, cp ? cp : "", val-1);
((char *)rctable[i].ptr)[val-1] = 0;
break;
case 'F':
while (cp && *cp != '#') {
sl = strlist_append(&tweaksymbols,
format_s("*%s", cp));
sl->value = rctable[i].def;
cp = my_strtok(NULL, " \t\n");
}
break;
case 'G':
while (cp && *cp != '#') {
sl = strlist_append(&tweaksymbols, cp);
sl->value = rctable[i].def;
cp = my_strtok(NULL, " \t\n");
}
break;
case 'A':
while (cp && *cp != '#') {
strlist_insert((Strlist **)rctable[i].ptr, cp);
cp = my_strtok(NULL, " \t\n");
}
break;
case 'M':
cp = my_strtok(NULL, "\n");
if (cp) {
while (isspace(*cp)) cp++;
for (cp2 = cp; *cp2 && *cp2 != '#'; cp2++) ;
*cp2 = 0;
if (*cp) {
sl = strlist_append(&addmacros, cp);
sl->value = rctable[i].def;
}
}
break;
case 'B':
if (cp)
val = parse_breakstr(cp);
if (val != -1)
*((short *)rctable[i].ptr) = val;
break;
case 'X':
switch (rctable[i].def) {
case 1: /* strlist with string values */
if (cp) {
sl = strlist_append((Strlist **)rctable[i].ptr, cp);
cp = my_strtok(NULL, " =\t\n");
if (cp && *cp != '#')
sl->value = (long)stralloc(cp);
}
break;
case 2: /* Include */
if (cp)
readrc(for