ref: f5199492fb98c0b6c2034041e6865de4dabaac33
parent: 1ae4a875f9b9215126fd406329cbfd267271492d
author: Roberto E. Vargas Caballero <[email protected]>
date: Sat May 9 13:21:56 EDT 2015
Add cpp.c file The preprocessor and the lexer are different things, so it is better to separate them
--- a/cc1/Makefile
+++ b/cc1/Makefile
@@ -1,6 +1,6 @@
OBJS = types.o decl.o lex.o error.o symbol.o main.o expr.o \
- code.o stmt.o
+ code.o stmt.o cpp.o
LIBS = -lcc
--- a/cc1/cc1.h
+++ b/cc1/cc1.h
@@ -278,6 +278,9 @@
/* expr.c */
extern Node *expr(void), *negate(Node *np);
+/* cpp.c */
+extern char *preprocessor(char *s);
+
/*
* Definition of global variables
*/
--- /dev/null
+++ b/cc1/cpp.c
@@ -1,0 +1,88 @@
+
+#include <ctype.h>
+#include <inttypes.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "../inc/cc.h"
+#include "cc1.h"
+
+/* TODO: preprocessor error must not rise recover */
+char *
+include(char *s)
+{
+ char fname[FILENAME_MAX], delim, c, *p;
+ size_t len;
+
+ if ((c = *s++) == '>')
+ delim = '>';
+ else if (c == '"')
+ delim = '"';
+ else
+ goto bad_include;
+
+ for (p = s; (c = *p) && c != delim; ++p)
+ /* nothing */;
+ if (c == '\0')
+ goto bad_include;
+
+ len = p - s;
+ if (delim == '"') {
+ if (len >= FILENAME_MAX)
+ goto too_long;
+ strncpy(fname, s, len);
+ fname[len] = '\0';
+ if (!addinput(fname))
+ goto not_found;
+ } else {
+ abort();
+ }
+
+ return p+1;
+
+not_found:
+ error("included file '%s' not found", fname);
+too_long:
+ error("file name in include too long");
+bad_include:
+ error("#include expects \"FILENAME\" or <FILENAME>");
+}
+
+char *
+preprocessor(char *p)
+{
+ char *q;
+ unsigned short n;
+ static char **bp, *cmds[] = {
+ "include",
+ NULL
+ };
+ static char *(*funs[])(char *) = {
+ include
+ };
+
+ while (isspace(*p))
+ ++p;
+ if (*p != '#')
+ return p;
+ for (++p; isspace(*p); ++p)
+ /* nothing */;
+ for (q = p; isalpha(*q); ++q)
+ /* nothing */;
+ n = q - p;
+ while (isspace(*q))
+ ++q;
+ for (bp = cmds; *bp; ++bp) {
+ if (strncmp(*bp, p, n))
+ continue;
+ q = (*funs[bp - cmds])(q);
+ while (isspace(*q++))
+ /* nothing */;
+ if (*q != '\0')
+ error("trailing characters after preprocessor directive");
+ return NULL;
+ }
+ error("incorrect preprocessor directive");
+}
+
--- a/cc1/lex.c
+++ b/cc1/lex.c
@@ -91,84 +91,6 @@
return input->nline;
}
-/* TODO: preprocessor error must not rise recover */
-char *
-include(char *s)
-{
- char fname[FILENAME_MAX], delim, c, *p;
- size_t len;
-
- if ((c = *s++) == '>')
- delim = '>';
- else if (c == '"')
- delim = '"';
- else
- goto bad_include;
-
- for (p = s; (c = *p) && c != delim; ++p)
- /* nothing */;
- if (c == '\0')
- goto bad_include;
-
- len = p - s;
- if (delim == '"') {
- if (len >= FILENAME_MAX)
- goto too_long;
- strncpy(fname, s, len);
- fname[len] = '\0';
- if (!addinput(fname))
- goto not_found;
- } else {
- abort();
- }
-
- return p+1;
-
-not_found:
- error("included file '%s' not found", fname);
-too_long:
- error("file name in include too long");
-bad_include:
- error("#include expects \"FILENAME\" or <FILENAME>");
-}
-
-static char *
-preprocessor(char *p)
-{
- char str[IDENTSIZ+1], *q;
- unsigned short n;
- static char **bp, *cmds[] = {
- "include",
- NULL
- };
- static char *(*funs[])(char *) = {
- include
- };
-
- while (isspace(*p))
- ++p;
- if (*p != '#')
- return p;
- for (++p; isspace(*p); ++p)
- /* nothing */;
- for (q = p; isalpha(*q); ++q)
- /* nothing */;
- n = q - p;
- while (isspace(*q))
- ++q;
- for (bp = cmds; *bp; ++bp) {
- if (strncmp(*bp, p, n))
- continue;
- q = (*funs[bp - cmds])(q);
- while (isspace(*q++))
- /* nothing */;
- if (*q != '\0')
- error("trailing characters after preprocessor directive");
- return NULL;
- }
- error("incorrect preprocessor directive");
-}
-
static int
readchar(void)
{