ack/lang/cem/lint/lpass2/read.c

224 lines
4 KiB
C
Raw Normal View History

1988-09-02 12:00:25 +00:00
/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
/* $Header$ */
1991-09-30 16:53:21 +00:00
#include "l_class.h"
1988-08-07 22:55:20 +00:00
#include "class.h"
#include "inpdef.h"
1988-07-08 22:24:06 +00:00
1988-08-07 22:55:20 +00:00
#include <ctype.h>
1988-07-08 22:24:06 +00:00
1988-09-30 15:20:24 +00:00
#define INP_NPUSHBACK 1
1988-07-08 22:24:06 +00:00
1988-08-07 22:55:20 +00:00
#include <inp_pkg.spec>
#include <inp_pkg.body>
1988-07-08 22:24:06 +00:00
1988-09-02 12:00:25 +00:00
#include "private.h"
1988-10-12 15:49:11 +00:00
int LineNr = 1;
1988-07-08 22:24:06 +00:00
/* Two dangerous macro's. They replace a single statement by
* two statements
*/
#define loadchar(ch) LoadChar(ch); if (ch=='\n') LineNr++
#define pushback(ch) PushBack(); if (ch=='\n') LineNr--
1988-10-12 15:49:11 +00:00
/* all the ReadX() functions return 0 upon EOI */
1988-07-08 22:24:06 +00:00
PRIVATE int ReadString();
PRIVATE int ReadInt();
PRIVATE int ReadArgs();
1988-10-12 15:49:11 +00:00
PRIVATE int ReadArg();
PRIVATE SkipChar();
1988-07-08 22:24:06 +00:00
int
get_id(id)
struct inpdef *id;
{
1988-08-07 22:55:20 +00:00
/* A low-level function which just reads a definition */
1988-07-08 22:24:06 +00:00
if (!ReadString(id->id_name, ':', NAMESIZE))
return 0;
if (!ReadInt(&id->id_statnr))
return 0;
SkipChar(':');
1988-08-07 22:55:20 +00:00
1988-07-08 22:24:06 +00:00
loadchar(id->id_class);
if (id->id_class == EOI)
return 0;
SkipChar(':');
1988-08-07 22:55:20 +00:00
if (is_class(id, CL_FUNC|CL_DEF) || is_class(id, CL_FUNC|CL_USAGE)) {
/* read the argument information */
id->id_args = 1;
1988-07-08 22:24:06 +00:00
if (!ReadInt(&id->id_nrargs))
return 0;
SkipChar(':');
if (!ReadArgs(id->id_nrargs, id->id_argtps))
return 0;
1988-08-07 22:55:20 +00:00
if (id->id_class == FC) {
/* function call */
if (!ReadInt(&id->id_valused))
return 0;
}
else {
/* function definition */
if (!ReadInt(&id->id_valreturned))
return 0;
}
1988-07-08 22:24:06 +00:00
SkipChar(':');
}
1988-08-07 22:55:20 +00:00
else {
id->id_args = 0;
}
1988-07-08 22:24:06 +00:00
if (!ReadString(id->id_type, ':', TYPESIZE))
return 0;
if (!ReadInt(&id->id_line))
return 0;
SkipChar(':');
1988-08-07 22:55:20 +00:00
1988-07-08 22:24:06 +00:00
if (!ReadString(id->id_file, '\n', FNAMESIZE))
return 0;
1988-10-12 15:49:11 +00:00
return 1;
1988-07-08 22:24:06 +00:00
}
PRIVATE int
ReadString(buf, delim, maxsize)
char *buf;
{
1988-10-12 15:49:11 +00:00
/* Reads a string until 'delim' is encountered; delim is
discarded.
If 'maxsize-1' is exceeded or the string contains a newline
1991-03-08 15:54:05 +00:00
panic() is called (unless delim == newline).
1988-08-07 22:55:20 +00:00
A '\0' is appended to the string.
*/
int ch = 0;
1988-07-08 22:24:06 +00:00
int nread = 0;
while (nread < maxsize - 1) {
loadchar(ch);
if (ch == EOI)
return 0;
if (ch == delim)
break;
1988-10-12 15:49:11 +00:00
if (ch == '\n') {
1991-03-08 15:54:05 +00:00
panic("incomplete line in intermediate file");
1988-10-12 15:49:11 +00:00
/*NOTREACHED*/
}
1988-07-08 22:24:06 +00:00
buf[nread++] = (char)ch;
}
1988-08-07 22:55:20 +00:00
buf[nread++] = '\0';
1988-07-08 22:24:06 +00:00
if (ch != delim) {
1991-03-08 15:54:05 +00:00
panic("line too long in intermediate file");
1988-07-08 22:24:06 +00:00
/*NOTREACHED*/
}
1988-10-12 15:49:11 +00:00
return 1;
1988-07-08 22:24:06 +00:00
}
PRIVATE int
ReadInt(ip)
int *ip;
{
/* Reads a decimal integer until a character which is not
* a digit is encountered.
* Non-digits except minus-sign in front of the number are discarded.
* Doesn't check on overflow.
* Just a minus-sign is interpreted as 0. (To prevent a look-ahead.)
*/
int ch;
int negative = 0;
int res = 0;
do {
loadchar(ch);
} while (!isdigit(ch) && ch != '-');
if (ch == EOI)
return 0;
if (ch == '-')
negative = 1;
else
res = ch - '0';
loadchar(ch);
while (isdigit(ch)) {
res = 10*res + ch - '0';
loadchar(ch);
}
pushback(ch);
1991-03-08 15:54:05 +00:00
*ip = (negative ? -res : res);
1988-07-08 22:24:06 +00:00
return 1;
}
PRIVATE int
ReadArgs(nrargs, buf)
char *buf;
{
1988-09-02 12:00:25 +00:00
/* Reads a string into buf with format
1988-10-12 15:49:11 +00:00
<type1>:<type2>: ... :<typeN>:\0
1988-09-02 12:00:25 +00:00
Note: format must include the final colon.
*/
1988-07-08 22:24:06 +00:00
int i;
int charcount = 1;
if (nrargs < 0) {
/* variable # of args */
nrargs = -nrargs - 1;
}
*buf = '\0';
for (i = 0; i < nrargs; i++) {
1988-09-30 15:20:24 +00:00
int n;
1988-10-12 15:49:11 +00:00
if (!ReadArg(buf, ARGTPSSIZE-charcount-1))
1988-07-08 22:24:06 +00:00
return 0;
n = strlen(buf) + 1;
charcount += n;
buf += n - 1;
*buf++ = ':';
}
*buf = '\0';
return 1;
}
1988-10-12 15:49:11 +00:00
PRIVATE int
ReadArg(buf, size)
char *buf;
int size;
{
int ch;
loadchar(ch);
switch (ch) {
case '"': /* formal format or actual string */
*buf++ = ch;
if (!ReadString(buf, ch, size-1))
return 0;
buf += strlen(buf);
*buf++ = ch;
*buf++ = '\0';
SkipChar(':');
return 1;
default: /* normal type */
pushback(ch);
return ReadString(buf, ':', size);
case EOI:
return 0;
}
}
PRIVATE SkipChar(ch)
{
int c;
loadchar(c);
1991-03-08 15:54:05 +00:00
if (c == ch)
return;
panic("bad format in intermediate file, '%c' expected; '%c' read",
ch, c
);
/*NOTREACHED*/
1988-10-12 15:49:11 +00:00
}