ref: 63cde7ec0b3cc2c47cf7338ce3cc63b2591b473c
parent: d0763d72e63709ffc8af883c5dd2db4be98f6b77
author: Roberto E. Vargas Caballero <[email protected]>
date: Wed Aug 6 11:25:23 EDT 2014
Use error number instead of error strings This allows don't repeat error messages and makes easier can translate to another language.
--- a/cc2/.gitignore
+++ b/cc2/.gitignore
@@ -1,1 +1,2 @@
cc2
+error.h
--- a/cc2/Makefile
+++ b/cc2/Makefile
@@ -8,11 +8,15 @@
all: cc2
$(OBJS): ../inc/cc.h ../inc/sizes.h cc2.h
+main.o: error.h
+error.h: cc2.h
+ ./generror cc2.h > $@
+
cc2: $(OBJS) ../lib/libcc.a
$(CC) $(LDFLAGS) $(CFLAGS) $(OBJS) $(LIBS) -o $@
clean:
rm -f $(OBJS)
- rm -f cc2
+ rm -f cc2 error.h
--- a/cc2/cc2.h
+++ b/cc2/cc2.h
@@ -23,6 +23,22 @@
struct node *left, *right;
} Node;
-#define AUTO 'A'
+enum nerrors {
+ EINTNUM, /* too much internal identifiers */
+ EEXTNUM, /* too much external identifiers */
+ ENODEOV, /* node overflow */
+ ESTACKO, /* stack overflow */
+ ESTACKU, /* stack underflow */
+ EEXPROV, /* expression overflow */
+ ETYPERR, /* incorrect type in expression */
+ EEXPBAL, /* expression not balanced */
+ ESYNTAX, /* syntax error */
+ ENUMERR
+};
+
+#define AUTO 'A'
#define REGISTER 'R'
-#define STATIC 'S'
+#define STATIC 'S'
+
+extern void error(unsigned nerror, ...);
+
--- /dev/null
+++ b/cc2/generror
@@ -1,0 +1,23 @@
+#!/usr/bin/awk -f
+
+BEGIN {
+ print "char *errlist[] = {"
+}
+/^enum nerrors {/ {
+ inhome = 1
+}
+/E[A-Z]*, / {
+ if (inhome == 1) {
+ sub(/,/, "", $1)
+ printf("\t[%s] = \"", $1)
+ for (i = 3; i < NF-1; ++i)
+ printf("%s ", $i)
+ printf("%s\",\n", $(NF-1));
+ }
+}
+/^}/ {
+ if (inhome)
+ print "};"
+ inhome = 0
+}
+
--- a/cc2/main.c
+++ b/cc2/main.c
@@ -1,15 +1,29 @@
-#include <stddef.h>
+#include <stdarg.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
#include <cc.h>
#include <sizes.h>
+#include "cc2.h"
+#include "error.h"
+
extern void parse(void);
void
-esyntax(void)
+error(unsigned nerror, ...)
{
- die("incorrect intermediate file");
+ va_list va;
+ va_start(va, nerror);
+ if (nerror >= ENUMERR)
+ fprintf(stderr, "incorrect error number '%d'", nerror);
+ else
+ vfprintf(stderr, errlist[nerror], va);
+ va_end(va);
+ putc('\n', stderr);
+ exit(EXIT_FAILURE);
}
int
@@ -17,4 +31,3 @@
{
parse();
}
-
--- a/cc2/parser.c
+++ b/cc2/parser.c
@@ -14,18 +14,14 @@
#define NR_STACKSIZ 32
#define NR_NODEPOOL 128
#define NR_EXPRESSIONS 64
-#define NR_SYMBOLS 1024
static Node *stack[NR_STACKSIZ], **stackp = stack;
static Node *listexp[NR_EXPRESSIONS], **listp = &listexp[0];
static Node nodepool[NR_NODEPOOL], *newp = nodepool;
-static Symbol symtbl[NR_SYMBOLS];
static Symbol *localtbl;
static Symbol *globaltbl;
-extern void esyntax(void);
-
static Symbol *
local(void)
{
@@ -34,7 +30,7 @@
scanf("%u", &i);
if (i >= NR_INT_IDENT)
- esyntax();
+ error(EINTNUM);
if (i >= nr)
localtbl = xrealloc(localtbl, i+1);
return &localtbl[i];
@@ -48,7 +44,7 @@
scanf("%u", &i);
if (i >= NR_EXT_IDENT)
- esyntax();
+ error(EEXTNUM);
if (i >= nr)
globaltbl = xrealloc(globaltbl, i+1);
return &globaltbl[i];
@@ -58,7 +54,7 @@
newnode(void)
{
if (newp == &nodepool[NR_NODEPOOL])
- esyntax();
+ error(ENODEOV);
return newp++;
}
@@ -66,7 +62,7 @@
push(Node *np)
{
if (stackp == &stack[NR_STACKSIZ])
- esyntax();
+ error(ESTACKO);
*stackp++ = np;
}
@@ -74,7 +70,7 @@
pop(void)
{
if (stackp == stack)
- esyntax();
+ error(ESTACKU);
return *--stackp;
}
@@ -82,7 +78,7 @@
link(Node *np)
{
if (listp == &listexp[NR_EXPRESSIONS])
- esyntax();
+ error(EEXPROV);
*listp++ = np;
}
@@ -95,7 +91,7 @@
case L_INT16: case L_INT8:
return t;
default:
- esyntax();
+ error(ETYPERR);
}
}
@@ -170,7 +166,7 @@
{
Node *np = *--stackp;
if (stackp != stack)
- esyntax();
+ error(EEXPBAL);
return np;
}
@@ -195,14 +191,14 @@
do {
if (!isprint(c = getchar()))
- esyntax();
+ error(ESYNTAX);
if ((fun = optbl[c]) == NULL)
- esyntax();
+ error(ESYNTAX);
(*fun)(c);
} while ((c = getchar()) == '\t');
if (c != '\n')
- esyntax();
+ error(ESYNTAX);
link(getroot());
}
@@ -215,7 +211,7 @@
sym->u.v.storage = sclass;
sym->u.v.type = gettype();
if (getchar() != '\n')
- esyntax();
+ error(ESYNTAX);
}
static void
@@ -262,7 +258,7 @@
chop();
return;
default:
- esyntax();
+ error(ESYNTAX);
break;
}
}
@@ -287,7 +283,7 @@
return;
break;
default:
- esyntax();
+ error(ESYNTAX);
}
}
}