diff lwcc/cpp.c @ 296:83fcc1ed6ad6 ccdev

Checkpoint lwcc development Initial untested version of the preprocessor with macro expansion but without file inclusion.
author William Astle <lost@l-w.ca>
date Sat, 14 Sep 2013 20:04:38 -0600
parents 4b17780f2777
children d85d173ba120
line wrap: on
line diff
--- a/lwcc/cpp.c	Thu Sep 12 22:06:26 2013 -0600
+++ b/lwcc/cpp.c	Sat Sep 14 20:04:38 2013 -0600
@@ -52,6 +52,7 @@
 	pp -> fn = lw_strdup(fn);
 	pp -> fp = fp;
 	pp -> ra = CPP_NOUNG;
+	pp -> ppeolseen = 1;
 	return pp;
 }
 
@@ -59,6 +60,23 @@
 {
 	struct token *t;
 	
+	if (pp -> curtok)
+		token_free(pp -> curtok);
+
+	/*
+	If there is a list of tokens to process, move it to the "unget" queue
+	with an EOF marker at the end of it.
+	*/	
+	if (pp -> sourcelist)
+	{
+		for (t = pp -> sourcelist; t -> next; t = t -> next)
+			/* do nothing */ ;
+		t -> next = token_create(TOK_EOF, NULL, -1, -1, "");
+		t -> next -> next = pp -> tokqueue;
+		pp -> tokqueue = pp -> sourcelist;
+		pp -> sourcelist = NULL;
+	}
+again:
 	if (pp -> tokqueue)
 	{
 		t = pp -> tokqueue;
@@ -67,15 +85,42 @@
 			pp -> tokqueue -> prev = NULL;
 		t -> next = NULL;
 		t -> prev = NULL;
-		return t;
+		pp -> curtok = t;
+		goto ret;
 	}
-	return(preproc_lex_next_token(pp));
+	pp -> curtok = preproc_lex_next_token(pp);
+	t = pp -> curtok;
+ret:
+	if (t -> ttype == TOK_ENDEXPAND)
+	{
+		struct expand_e *e;
+		e = pp -> expand_list;
+		pp -> expand_list = e -> next;
+		lw_free(e);
+		goto again;
+	}
+	return t;
+}
+
+void preproc_unget_token(struct preproc_info *pp, struct token *t)
+{
+	t -> next = pp -> tokqueue;
+	pp -> tokqueue = t;
+	if (pp -> curtok == t)
+		pp -> curtok = NULL;
 }
 
 void preproc_finish(struct preproc_info *pp)
 {
 	lw_free((void *)(pp -> fn));
 	fclose(pp -> fp);
+	if (pp -> curtok)
+		token_free(pp -> curtok);
+	while (pp -> tokqueue)
+	{
+		preproc_next_token(pp);
+		token_free(pp -> curtok);
+	}
 	lw_free(pp);
 }