shithub: scc

Download patch

ref: 7b0ece05732ca278a0e18e40c16f29804f01c5cc
parent: f41175b80c5d599aba9fa70611adf5e94bf4c6e1
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Sun May 10 16:17:17 EDT 2015

Add initial version of #define

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

--- 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 @@
 	char *q;
 	unsigned short n;
 	static char **bp, *cmds[] = {
+		"define",
 		"include",
 		"line",
 		"pragma",
@@ -110,6 +142,7 @@
 		NULL
 	};
 	static char *(*funs[])(char *) = {
+		define,
 		include,
 		line,
 		pragma,