changeset 304:d85d173ba120 ccdev

Checkpoint lwcc development - preprocessor is runnable but nonfunctional The preprocessor is currently runnable but doesn't actually do anything useful. This is just a checkpoint.
author William Astle <lost@l-w.ca>
date Tue, 17 Sep 2013 19:33:41 -0600
parents 659e0e4ce50c
children 54f213c8fb81
files Makefile lwcc/cpp-main.c lwcc/cpp.c lwcc/cpp.h lwcc/lex.c lwcc/preproc.c lwcc/strpool.c lwcc/strpool.h lwcc/token.c
diffstat 9 files changed, 130 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Tue Sep 17 19:32:14 2013 -0600
+++ b/Makefile	Tue Sep 17 19:33:41 2013 -0600
@@ -101,7 +101,7 @@
 lwcc_driver_objs := $(lwcc_driver_srcs:.c=.o)
 lwcc_driver_deps := $(lwcc_driver_srcs:.c=.d)
 
-lwcc_cpp_srcs := cpp-main.c cpp.c lex.c strbuf.c token.c preproc.c symbol.c
+lwcc_cpp_srcs := cpp-main.c cpp.c lex.c strbuf.c token.c preproc.c symbol.c strpool.c 
 lwcc_cpp_srcs := $(addprefix lwcc/,$(lwcc_cpp_srcs))
 lwcc_cpp_objs := $(lwcc_cpp_srcs:.c=.o)
 lwcc_cpp_deps := $(lwcc_cpp_srcs:.c=.d)
--- a/lwcc/cpp-main.c	Tue Sep 17 19:32:14 2013 -0600
+++ b/lwcc/cpp-main.c	Tue Sep 17 19:33:41 2013 -0600
@@ -120,7 +120,6 @@
 	{
 		/* if no input files, work on stdin */
 		retval = process_file("-");
-		retval = 1;
 	}
 	else
 	{
@@ -142,7 +141,7 @@
 int process_file(const char *fn)
 {
 	struct preproc_info *pp;
-	struct token *tok;
+	struct token *tok = NULL;
 	
 	pp = preproc_init(fn);
 	if (!pp)
@@ -150,11 +149,13 @@
 	
 	for (;;)
 	{
-		tok = preproc_next_token(pp);
+		tok = preproc_next(pp);
 		if (tok -> ttype == TOK_EOF)
 			break;
 		token_print(tok, output_fp);
+		token_free(tok);
 	}
+	token_free(tok);
 	preproc_finish(pp);
 	return 0;
 }
--- a/lwcc/cpp.c	Tue Sep 17 19:32:14 2013 -0600
+++ b/lwcc/cpp.c	Tue Sep 17 19:33:41 2013 -0600
@@ -28,6 +28,7 @@
 #include <lw_string.h>
 
 #include "cpp.h"
+#include "strpool.h"
 
 struct token *preproc_lex_next_token(struct preproc_info *);
 
@@ -49,7 +50,8 @@
 	
 	pp = lw_alloc(sizeof(struct preproc_info));
 	memset(pp, 0, sizeof(struct preproc_info));
-	pp -> fn = lw_strdup(fn);
+	pp -> strpool = strpool_create();
+	pp -> fn = strpool_strdup(pp -> strpool, fn);
 	pp -> fp = fp;
 	pp -> ra = CPP_NOUNG;
 	pp -> ppeolseen = 1;
@@ -112,7 +114,6 @@
 
 void preproc_finish(struct preproc_info *pp)
 {
-	lw_free((void *)(pp -> fn));
 	fclose(pp -> fp);
 	if (pp -> curtok)
 		token_free(pp -> curtok);
@@ -121,6 +122,7 @@
 		preproc_next_token(pp);
 		token_free(pp -> curtok);
 	}
+	strpool_free(pp -> strpool);
 	lw_free(pp);
 }
 
--- a/lwcc/cpp.h	Tue Sep 17 19:32:14 2013 -0600
+++ b/lwcc/cpp.h	Tue Sep 17 19:33:41 2013 -0600
@@ -67,6 +67,7 @@
 	int lexstrloc;			// ditto
 	struct preproc_info *n;	// next in file stack
 	struct preproc_info *filestack;	// stack of saved files during include
+	struct strpool *strpool;
 };
 
 extern struct preproc_info *preproc_init(const char *);
@@ -78,5 +79,6 @@
 extern void preproc_throw_error(struct preproc_info *, const char *, ...);
 extern void preproc_throw_warning(struct preproc_info *, const char *, ...);
 extern void preproc_unget_token(struct preproc_info *, struct token *);
+extern struct token *preproc_next(struct preproc_info *);
 
 #endif // cpp_h_seen___
--- a/lwcc/lex.c	Tue Sep 17 19:32:14 2013 -0600
+++ b/lwcc/lex.c	Tue Sep 17 19:33:41 2013 -0600
@@ -350,7 +350,7 @@
 	int c, c2;
 	int cl;
 	struct strbuf *strbuf;
-	struct token *t;
+	struct token *t = NULL;
 	struct preproc_info *fs;
 					
 fileagain:
--- a/lwcc/preproc.c	Tue Sep 17 19:32:14 2013 -0600
+++ b/lwcc/preproc.c	Tue Sep 17 19:33:41 2013 -0600
@@ -48,7 +48,7 @@
 	if (ct -> ttype == TOK_EOL)
 		pp -> ppeolseen = 1;
 		
-	if (ct -> ttype == TOK_HASH && pp -> eolseen == 1)
+	if (ct -> ttype == TOK_HASH && pp -> ppeolseen == 1)
 	{
 		// preprocessor directive 
 		process_directive(pp);
@@ -1470,3 +1470,12 @@
 	
 	return 1;
 }
+
+struct token *preproc_next(struct preproc_info *pp)
+{
+	struct token *t;
+	
+	t = preproc_next_processed_token(pp);
+	pp -> curtok = NULL;
+	return t;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lwcc/strpool.c	Tue Sep 17 19:33:41 2013 -0600
@@ -0,0 +1,72 @@
+/*
+lwcc/strpool.c
+
+Copyright © 2013 William Astle
+
+This file is part of LWTOOLS.
+
+LWTOOLS is free software: you can redistribute it and/or modify it under the
+terms of the GNU General Public License as published by the Free Software
+Foundation, either version 3 of the License, or (at your option) any later
+version.
+
+This program is distributed in the hope that it will be useful, but WITHOUT
+ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+more details.
+
+You should have received a copy of the GNU General Public License along with
+this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <stdlib.h>
+#include <string.h>
+
+#include <lw_alloc.h>
+#include <lw_string.h>
+
+#include "strpool.h"
+
+struct strpool *strpool_create(void)
+{
+	struct strpool *sp;
+	
+	sp = lw_alloc(sizeof(struct strpool));
+	sp -> nstrs = 0;
+	sp -> strs = NULL;
+	return sp;
+}
+
+extern void strpool_free(struct strpool *sp)
+{
+	int i;
+	
+	for (i = 0; i < sp -> nstrs; i++)
+		lw_free(sp -> strs[i]);
+	lw_free(sp -> strs);
+	lw_free(sp);
+}
+
+char *strpool_strdup(struct strpool *sp, const char *s)
+{
+	int i;
+	
+	if (!s)
+		return NULL;
+
+	/* first do a fast scan for a pointer match */	
+	for (i = 0; i < sp -> nstrs; i++)
+		if (sp -> strs[i] == s)
+			return sp -> strs[i];
+	
+	/* no match - do a slow scan for a string match */
+	for (i = 0; i < sp -> nstrs; i++)
+		if (strcmp(sp -> strs[i], s) == 0)
+			return sp -> strs[i];
+	
+	/* no match - create a new string entry */
+	sp -> strs = lw_realloc(sp -> strs, sizeof(char *) * (sp -> nstrs + 1));
+	sp -> strs[sp -> nstrs] = lw_strdup(s);
+	sp -> nstrs++;
+	return sp -> strs[sp -> nstrs - 1];
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lwcc/strpool.h	Tue Sep 17 19:33:41 2013 -0600
@@ -0,0 +1,35 @@
+/*
+lwcc/strpool.h
+
+Copyright © 2013 William Astle
+
+This file is part of LWTOOLS.
+
+LWTOOLS is free software: you can redistribute it and/or modify it under the
+terms of the GNU General Public License as published by the Free Software
+Foundation, either version 3 of the License, or (at your option) any later
+version.
+
+This program is distributed in the hope that it will be useful, but WITHOUT
+ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+more details.
+
+You should have received a copy of the GNU General Public License along with
+this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef strpool_h_seen___
+#define strpool_h_seen___
+
+struct strpool
+{
+	int nstrs;
+	char **strs;
+};
+
+extern struct strpool *strpool_create(void);
+extern void strpool_free(struct strpool *);
+extern char *strpool_strdup(struct strpool *, const char *);
+
+#endif // strpool.h_seen___
--- a/lwcc/token.c	Tue Sep 17 19:32:14 2013 -0600
+++ b/lwcc/token.c	Tue Sep 17 19:33:41 2013 -0600
@@ -35,7 +35,7 @@
 	if (strval)
 		t -> strval = lw_strdup(strval);
 	else
-		strval = NULL;
+		t -> strval = NULL;
 	t -> lineno = row;
 	t -> column = col;
 	t -> fn = fn;