scc

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

commit 7b0ece05732ca278a0e18e40c16f29804f01c5cc
parent f41175b80c5d599aba9fa70611adf5e94bf4c6e1
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sun, 10 May 2015 20:17:17 +0200

Add initial version of #define

This version only accetps define without parameters and it
doesn't expand it.

Diffstat:
Mcc1/cpp.c | 33+++++++++++++++++++++++++++++++++
1 file changed, 33 insertions(+), 0 deletions(-)

diff --git a/cc1/cpp.c b/cc1/cpp.c @@ -5,11 +5,42 @@ #include <stdlib.h> #include <string.h> +#include "../inc/sizes.h" #include "../inc/cc.h" #include "cc1.h" /* TODO: preprocessor error must not rise recover */ static char * +define(char *s) +{ + char *t, name[IDENTSIZ+1]; + size_t len; + Symbol *sym; + + if (!isalnum(*s) && *s != '_') + goto bad_define; + for (t = s; isalnum(*t) || *t == '_'; ++t) + /* nothing */; + if ((len = t - s) > IDENTSIZ) + goto too_long; + strncpy(name, s, len); + name[len] = '\0'; + sym = install(name, NS_CPP); + + while (isspace(*t)) + ++t; + for (s = t + strlen(t); isspace(*--s); *s = '\0') + /* nothing */; + sym->u.s = xstrdup(t); + return s+1; + +too_long: + error("macro identifier too long"); +bad_define: + error("macro names must be identifiers"); +} + +static char * include(char *s) { char fname[FILENAME_MAX], delim, c, *p; @@ -103,6 +134,7 @@ preprocessor(char *p) char *q; unsigned short n; static char **bp, *cmds[] = { + "define", "include", "line", "pragma", @@ -110,6 +142,7 @@ preprocessor(char *p) NULL }; static char *(*funs[])(char *) = { + define, include, line, pragma,