ack/util/grind/modula-2.c

558 lines
8.3 KiB
C
Raw Normal View History

1994-06-24 11:31:16 +00:00
/* $Id$ */
1990-08-31 18:22:53 +00:00
/* Language dependant support; this one is for Modula-2 */
#include <stdio.h>
1990-09-12 16:13:59 +00:00
#include <alloc.h>
#include <assert.h>
1990-08-31 18:22:53 +00:00
1990-09-12 16:13:59 +00:00
#include "position.h"
1990-09-07 14:56:24 +00:00
#include "class.h"
1990-08-31 18:22:53 +00:00
#include "langdep.h"
1990-09-07 14:56:24 +00:00
#include "Lpars.h"
#include "idf.h"
#include "token.h"
#include "expr.h"
1990-09-12 16:13:59 +00:00
#include "tree.h"
#include "operator.h"
1993-11-10 15:11:28 +00:00
#include "misc.h"
1990-08-31 18:22:53 +00:00
1990-09-07 14:56:24 +00:00
extern FILE *db_out, *db_in;
extern double
atof();
1990-08-31 18:22:53 +00:00
static int
1990-09-07 14:56:24 +00:00
print_string(),
1990-09-19 14:31:12 +00:00
print_char(),
1990-09-07 14:56:24 +00:00
get_number(),
1990-11-12 13:46:31 +00:00
getname(),
1990-09-07 14:56:24 +00:00
get_token(),
1990-11-12 13:46:31 +00:00
getstring(),
1990-09-12 16:13:59 +00:00
print_op(),
1990-09-19 14:31:12 +00:00
binop_prio(),
1990-09-20 17:51:14 +00:00
unop_prio(),
fix_bin_to_pref();
1990-08-31 18:22:53 +00:00
static long
array_elsize();
static struct langdep m2 = {
1990-09-12 16:13:59 +00:00
1,
1990-08-31 18:22:53 +00:00
"%ld",
"%loB",
"%lXH",
"%lu",
"%lXH",
1990-10-17 17:00:03 +00:00
"%.14G",
1990-08-31 18:22:53 +00:00
"[",
"]",
"(",
")",
"{",
"}",
print_string,
1990-09-19 14:31:12 +00:00
print_char,
1990-09-07 14:56:24 +00:00
array_elsize,
1990-09-19 14:31:12 +00:00
binop_prio,
unop_prio,
1990-11-12 13:46:31 +00:00
getstring,
getname,
1990-09-07 14:56:24 +00:00
get_number,
1990-09-12 16:13:59 +00:00
get_token,
1990-09-20 17:51:14 +00:00
print_op,
fix_bin_to_pref
1990-08-31 18:22:53 +00:00
};
struct langdep *m2_dep = &m2;
1990-11-12 13:46:31 +00:00
static
1990-09-19 14:31:12 +00:00
print_char(c)
int c;
{
1990-10-31 15:56:51 +00:00
c &= 0377;
1990-09-19 14:31:12 +00:00
fprintf(db_out, (c >= 040 && c < 0177) ? "'%c'" : "%oC", c);
}
1990-11-12 13:46:31 +00:00
static
1990-11-06 12:23:41 +00:00
print_string(f, s, len)
FILE *f;
1990-08-31 18:22:53 +00:00
char *s;
1990-09-14 14:37:26 +00:00
int len;
1990-08-31 18:22:53 +00:00
{
register char *str = s;
int delim = '\'';
while (*str) {
if (*str++ == '\'') delim = '"';
}
1990-11-06 12:23:41 +00:00
fprintf(f, "%c%.*s%c", delim, len, s, delim);
1990-08-31 18:22:53 +00:00
}
extern long int_size;
static long
array_elsize(size)
long size;
{
if (! (int_size % size)) return size;
if (! (size % int_size)) return size;
return ((size + int_size - 1) / int_size) * int_size;
}
1990-09-07 14:56:24 +00:00
static int
1990-09-19 14:31:12 +00:00
unop_prio(op)
1990-09-07 14:56:24 +00:00
int op;
{
1990-09-12 16:13:59 +00:00
switch(op) {
case E_NOT:
return 5;
1990-09-19 14:31:12 +00:00
case E_MIN:
case E_PLUS:
return 3;
}
return 1;
}
1990-09-12 16:13:59 +00:00
1990-09-19 14:31:12 +00:00
static int
binop_prio(op)
int op;
{
switch(op) {
case E_SELECT:
return 9;
case E_ARRAY:
return 5;
1990-09-12 16:13:59 +00:00
case E_AND:
case E_MUL:
case E_DIV:
case E_MOD:
return 4;
case E_PLUS:
case E_MIN:
case E_OR:
return 3;
case E_IN:
case E_EQUAL:
case E_NOTEQUAL:
case E_LTEQUAL:
case E_GTEQUAL:
case E_LT:
case E_GT:
return 2;
}
1990-09-07 14:56:24 +00:00
return 1;
}
static int
get_number(ch)
register int ch;
{
/* The problem arising with the "parsing" of a number
is that we don't know the base in advance so we
have to read the number with the help of a rather
complex finite automaton.
*/
enum statetp {Oct,Hex,Dec,OctEndOrHex,End,Real};
register enum statetp state;
char buf[512+1];
register int base = 10;
register char *np = &buf[0];
*np++ = ch;
state = is_oct(ch) ? Oct : Dec;
ch = getc(db_in);
for (;;) {
switch(state) {
case Oct:
while (is_oct(ch)) {
if (np < &buf[512]) *np++ = ch;
ch = getc(db_in);
}
if (ch == 'B' || ch == 'C') {
state = OctEndOrHex;
break;
}
/* Fall Through */
case Dec:
base = 10;
while (is_dig(ch)) {
if (np < &buf[512]) {
*np++ = ch;
}
ch = getc(db_in);
}
if (is_hex(ch)) state = Hex;
else if (ch == '.') state = Real;
else {
state = End;
if (ch == 'H') base = 16;
else ungetc(ch, db_in);
}
break;
case Hex:
while (is_hex(ch)) {
if (np < &buf[512]) *np++ = ch;
ch = getc(db_in);
}
base = 16;
state = End;
if (ch != 'H') {
error("H expected after hex number");
ungetc(ch, db_in);
}
break;
case OctEndOrHex:
if (np < &buf[512]) *np++ = ch;
ch = getc(db_in);
if (ch == 'H') {
base = 16;
state = End;
break;
}
if (is_hex(ch)) {
state = Hex;
break;
}
ungetc(ch, db_in);
ch = *--np;
*np++ = '\0';
/* Fall through */
case End:
*np = '\0';
if (np >= &buf[512]) {
tok.ival = 1;
error("constant too long");
}
else {
np = &buf[0];
while (*np == '0') np++;
tok.ival = 0;
while (*np) {
int c;
if (is_dig(*np)) {
c = *np++ - '0';
}
else {
c = *np++ - 'A' + 10;
}
tok.ival *= base;
tok.ival += c;
}
}
return INTEGER;
}
if (state == Real) break;
}
/* a real real constant */
if (np < &buf[512]) *np++ = '.';
1990-09-19 14:31:12 +00:00
ch = getc(db_in);
1990-09-07 14:56:24 +00:00
while (is_dig(ch)) {
/* Fractional part
*/
if (np < &buf[512]) *np++ = ch;
ch = getc(db_in);
}
if (ch == 'E') {
/* Scale factor
*/
if (np < &buf[512]) *np++ = ch;
ch = getc(db_in);
if (ch == '+' || ch == '-') {
/* Signed scalefactor
*/
if (np < &buf[512]) *np++ = ch;
ch = getc(db_in);
}
if (is_dig(ch)) {
do {
if (np < &buf[512]) *np++ = ch;
ch = getc(db_in);
} while (is_dig(ch));
}
else {
error("bad scale factor");
}
}
*np++ = '\0';
ungetc(ch, db_in);
if (np >= &buf[512]) {
tok.fval = 0.0;
error("real constant too long");
}
else tok.fval = atof(buf);
return REAL;
}
static int
1990-11-12 13:46:31 +00:00
getname(c)
1990-09-07 14:56:24 +00:00
register int c;
{
char buf[512+1];
register char *p = &buf[0];
register struct idf *id;
do {
if (p - buf < 512) *p++ = c;
c = getc(db_in);
} while (in_idf(c));
ungetc(c, db_in);
*p = 0;
/* now recognize AND, DIV, IN, MOD, NOT, OR */
switch(buf[0]) {
case 'A':
if (strcmp(buf, "AND") == 0) {
tok.ival = E_AND;
return BIN_OP;
}
break;
case 'D':
if (strcmp(buf, "DIV") == 0) {
tok.ival = E_DIV;
return BIN_OP;
}
break;
case 'I':
if (strcmp(buf, "IN") == 0) {
tok.ival = E_IN;
return BIN_OP;
}
break;
case 'M':
if (strcmp(buf, "MOD") == 0) {
tok.ival = E_MOD;
return BIN_OP;
}
break;
case 'N':
if (strcmp(buf, "NOT") == 0) {
tok.ival = E_NOT;
return PREF_OP;
}
break;
case 'O':
if (strcmp(buf, "OR") == 0) {
tok.ival = E_OR;
return BIN_OP;
}
break;
}
id = str2idf(buf, 1);
tok.idf = id;
tok.str = id->id_text;
return id->id_reserved ? id->id_reserved : NAME;
}
static int
get_token(c)
register int c;
{
switch(c) {
case '[':
tok.ival = E_ARRAY;
/* fall through */
1990-09-07 14:56:24 +00:00
case '(':
case ')':
case ']':
case '`':
case '{':
case '}':
case ':':
case ',':
case '\\':
1990-09-07 14:56:24 +00:00
return c;
case '.':
tok.ival = E_SELECT;
return SEL_OP;
case '+':
tok.ival = E_PLUS;
return PREF_OR_BIN_OP;
case '-':
tok.ival = E_MIN;
return PREF_OR_BIN_OP;
case '*':
tok.ival = E_MUL;
return BIN_OP;
case '/':
tok.ival = E_DIV;
return BIN_OP;
case '&':
tok.ival = E_AND;
return BIN_OP;
case '|':
tok.ival = E_OR;
return BIN_OP;
case '=':
tok.ival = E_EQUAL;
return BIN_OP;
case '#':
tok.ival = E_NOTEQUAL;
return BIN_OP;
case '<':
c = getc(db_in);
if (c == '>') {
tok.ival = E_NOTEQUAL;
return BIN_OP;
}
if (c == '=') {
tok.ival = E_LTEQUAL;
return BIN_OP;
}
ungetc(c, db_in);
tok.ival = E_LT;
return BIN_OP;
case '>':
c = getc(db_in);
if (c == '=') {
tok.ival = E_GTEQUAL;
return BIN_OP;
}
ungetc(c, db_in);
tok.ival = E_GT;
return BIN_OP;
case '^':
tok.ival = E_DEREF;
return POST_OP;
case '~':
tok.ival = E_NOT;
return PREF_OP;
default:
error((c >= 040 && c < 0177) ? "%s'%c'" : "%s'\\0%o'", "illegal character ", c);
1990-09-07 14:56:24 +00:00
return LLlex();
}
}
1990-09-12 16:13:59 +00:00
static int
1990-11-12 13:46:31 +00:00
getstring(c)
1990-09-12 16:13:59 +00:00
int c;
{
register int ch;
char buf[512];
register int len = 0;
while (ch = getc(db_in), ch != c) {
if (ch == '\n') {
error("newline in string");
ungetc(ch, db_in);
1990-09-12 16:13:59 +00:00
break;
}
buf[len++] = ch;
}
buf[len++] = 0;
tok.str = Salloc(buf, (unsigned) len);
return STRING;
}
1990-11-12 13:46:31 +00:00
static
1990-11-06 12:23:41 +00:00
print_op(f, p)
FILE *f;
1990-09-12 16:13:59 +00:00
p_tree p;
{
switch(p->t_oper) {
case OP_UNOP:
switch(p->t_whichoper) {
case E_MIN:
1990-11-06 12:23:41 +00:00
fputs("-", f);
print_node(f, p->t_args[0], 0);
1990-09-12 16:13:59 +00:00
break;
case E_PLUS:
1990-11-06 12:23:41 +00:00
fputs("+", f);
print_node(f, p->t_args[0], 0);
1990-09-12 16:13:59 +00:00
break;
case E_NOT:
1990-11-06 12:23:41 +00:00
fputs("~", f);
print_node(f, p->t_args[0], 0);
1990-09-12 16:13:59 +00:00
break;
case E_DEREF:
1990-11-06 12:23:41 +00:00
print_node(f, p->t_args[0], 0);
fputs("^", f);
1990-09-12 16:13:59 +00:00
break;
}
break;
case OP_BINOP:
1990-09-19 14:31:12 +00:00
if (p->t_whichoper == E_ARRAY) {
1990-11-06 12:23:41 +00:00
print_node(f, p->t_args[0], 0);
fputs("[", f);
print_node(f, p->t_args[1], 0);
fputs("]", f);
1990-09-19 14:31:12 +00:00
break;
}
if (p->t_whichoper == E_SELECT) {
1990-11-06 12:23:41 +00:00
print_node(f, p->t_args[0], 0);
fputs(".", f);
print_node(f, p->t_args[1], 0);
1990-09-19 14:31:12 +00:00
break;
}
1990-11-06 12:23:41 +00:00
fputs("(", f);
print_node(f, p->t_args[0], 0);
1990-09-12 16:13:59 +00:00
switch(p->t_whichoper) {
case E_AND:
1990-11-06 12:23:41 +00:00
fputs("&", f);
1990-09-12 16:13:59 +00:00
break;
case E_OR:
1990-11-06 12:23:41 +00:00
fputs("|", f);
1990-09-12 16:13:59 +00:00
break;
case E_DIV:
1990-11-06 12:23:41 +00:00
fputs("/", f);
1990-09-12 16:13:59 +00:00
break;
case E_MOD:
1990-11-06 12:23:41 +00:00
fputs(" MOD ", f);
1990-09-12 16:13:59 +00:00
break;
case E_IN:
1990-11-06 12:23:41 +00:00
fputs(" IN ", f);
1990-09-12 16:13:59 +00:00
break;
case E_PLUS:
1990-11-06 12:23:41 +00:00
fputs("+", f);
1990-09-12 16:13:59 +00:00
break;
case E_MIN:
1990-11-06 12:23:41 +00:00
fputs("-", f);
1990-09-12 16:13:59 +00:00
break;
case E_MUL:
1990-11-06 12:23:41 +00:00
fputs("*", f);
1990-09-12 16:13:59 +00:00
break;
case E_EQUAL:
1990-11-06 12:23:41 +00:00
fputs("=", f);
1990-09-12 16:13:59 +00:00
break;
case E_NOTEQUAL:
1990-11-06 12:23:41 +00:00
fputs("#", f);
1990-09-12 16:13:59 +00:00
break;
case E_LTEQUAL:
1990-11-06 12:23:41 +00:00
fputs("<=", f);
1990-09-12 16:13:59 +00:00
break;
case E_GTEQUAL:
1990-11-06 12:23:41 +00:00
fputs(">=", f);
1990-09-12 16:13:59 +00:00
break;
case E_LT:
1990-11-06 12:23:41 +00:00
fputs("<", f);
1990-09-12 16:13:59 +00:00
break;
case E_GT:
1990-11-06 12:23:41 +00:00
fputs(">", f);
1990-09-12 16:13:59 +00:00
break;
}
1990-11-06 12:23:41 +00:00
print_node(f, p->t_args[1], 0);
fputs(")", f);
1990-09-12 16:13:59 +00:00
break;
}
}
1990-09-20 17:51:14 +00:00
1990-11-12 13:46:31 +00:00
static
1990-09-20 17:51:14 +00:00
fix_bin_to_pref()
{
/* No problems of this kind in Modula-2 */
}