scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit 63cde7ec0b3cc2c47cf7338ce3cc63b2591b473c
parent d0763d72e63709ffc8af883c5dd2db4be98f6b77
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Wed,  6 Aug 2014 15:25:23 +0200

Use error number instead of error strings

This allows don't repeat error messages and makes easier
can translate to another language.

Diffstat:
Mcc2/.gitignore | 1+
Mcc2/Makefile | 6+++++-
Mcc2/cc2.h | 20++++++++++++++++++--
Acc2/generror | 23+++++++++++++++++++++++
Mcc2/main.c | 21+++++++++++++++++----
Mcc2/parser.c | 32++++++++++++++------------------
6 files changed, 78 insertions(+), 25 deletions(-)

diff --git a/cc2/.gitignore b/cc2/.gitignore @@ -1 +1,2 @@ cc2 +error.h diff --git a/cc2/Makefile b/cc2/Makefile @@ -8,11 +8,15 @@ LIBS = -lcc 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 diff --git a/cc2/cc2.h b/cc2/cc2.h @@ -23,6 +23,22 @@ typedef struct node { 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, ...); + diff --git a/cc2/generror b/cc2/generror @@ -0,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 +} + diff --git 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 @@ main(void) { parse(); } - diff --git 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 @@ local(void) 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 @@ global(void) 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 @@ static Node * newnode(void) { if (newp == &nodepool[NR_NODEPOOL]) - esyntax(); + error(ENODEOV); return newp++; } @@ -66,7 +62,7 @@ static void push(Node *np) { if (stackp == &stack[NR_STACKSIZ]) - esyntax(); + error(ESTACKO); *stackp++ = np; } @@ -74,7 +70,7 @@ static Node * pop(void) { if (stackp == stack) - esyntax(); + error(ESTACKU); return *--stackp; } @@ -82,7 +78,7 @@ static void link(Node *np) { if (listp == &listexp[NR_EXPRESSIONS]) - esyntax(); + error(EEXPROV); *listp++ = np; } @@ -95,7 +91,7 @@ gettype(void) case L_INT16: case L_INT8: return t; default: - esyntax(); + error(ETYPERR); } } @@ -170,7 +166,7 @@ getroot(void) { Node *np = *--stackp; if (stackp != stack) - esyntax(); + error(EEXPBAL); return np; } @@ -195,14 +191,14 @@ expression(void) 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 @@ declaration(char sclass, char islocal) sym->u.v.storage = sclass; sym->u.v.type = gettype(); if (getchar() != '\n') - esyntax(); + error(ESYNTAX); } static void @@ -262,7 +258,7 @@ function(void) chop(); return; default: - esyntax(); + error(ESYNTAX); break; } } @@ -287,7 +283,7 @@ parse(void) return; break; default: - esyntax(); + error(ESYNTAX); } } }