shithub: scc

Download patch

ref: 6decf612c382dab28f910aca45b6e34a842fc4c5
parent: 64add66232ba4cf148aab299b3fa999444fe6e8e
author: Roberto E. Vargas Caballero <[email protected]>
date: Wed Apr 23 11:09:10 EDT 2014

Add character constants

--- a/lex.c
+++ b/lex.c
@@ -110,6 +110,7 @@
 {
 	char c;
 
+repeat:
 	switch (getc(yyin)) {
 	case '\\': c = '\''; break;
 	case 'a': c = '\a'; break;
@@ -125,7 +126,9 @@
 	case 'u': /* TODO: */
 	case '\n':
 		 ++linenum;
-		return s;
+		if ((c = getc(yyin)) == '\\')
+			goto repeat;
+		break;
 	default:
 		warn(1, "unknown escape sequence");
 		return s;
@@ -136,6 +139,25 @@
 }
 
 static uint8_t
+character(void)
+{
+	static char c;
+	register Symbol *sym;
+
+	getc(yyin);   /* discard the initial ' */
+	c = getc(yyin);
+	if (c == '\\')
+		escape(&c);
+	if (getc(yyin) != '\'')
+		error("invalid character constant");
+	sym = install("", NS_IDEN);
+	sym->u.i = c;
+	sym->type = inttype;
+	yynlval.sym = sym;
+	return CONSTANT;
+}
+
+static uint8_t
 string(void)
 {
 	static char buf[STRINGSIZ+1];
@@ -386,6 +408,8 @@
 		yyntoken = number();
 	else if (c == '"')
 		yyntoken = string();
+	else if (c == '\'')
+		yyntoken = character();
 	else
 		yyntoken = operator();