shithub: scc

Download patch

ref: ac87e5669ad21698f1564d8dd58a95fc0b731528
parent: 4c7497f6177a9bfef0a5630634ab8ad948048a08
parent: 690e4af100608ab79133ca0743235ad8b9d9ad15
author: Roberto E. Vargas Caballero <[email protected]>
date: Tue Apr 15 03:47:14 EDT 2014

Merge remote-tracking branch 'hal/master'

--- a/cc.h
+++ b/cc.h
@@ -36,7 +36,6 @@
 	NS_LABEL,
 	NS_TAG,
 	NR_NAMESPACES,
-	NS_KEYWORD,
 	NS_FREE
 };
 
@@ -230,7 +229,7 @@
 
 extern void
 	emitsym(Node *), emitunary(Node *),
-	emitbin(Node *), emitexp(Node *);
+	emitbin(Node *), emitexp(Node *), emitconst(Node *np);
 
 extern Node
 	*node(Inst code, Type *tp, union unode u, uint8_t nchilds),
--- a/code.c
+++ b/code.c
@@ -72,6 +72,12 @@
 }
 
 void
+emitconst(Node *np)
+{
+	printf("\t#%X", np->u.sym->u.i);
+}
+
+void
 emitcast(Node *np)
 {
 	Node *child = np->childs[0];
--- a/decl.c
+++ b/decl.c
@@ -75,7 +75,7 @@
 		if (yytoken != IDEN) {
 			if (flags & ID_EXPECTED)
 				goto expected;
-			sym = install(NULL, ns);
+			sym = install("", ns);
 		} else {
 			sym = newiden(ns);
 		}
@@ -348,7 +348,7 @@
 		}
 		next();
 	} else {
-		sym = install(NULL, NS_TAG);
+		sym = install("", NS_TAG);
 	}
 	tp = sym->type = mktype(NULL, tag, NULL, 0);
 	sym->u.ns = ++namespace;
--- a/expr.c
+++ b/expr.c
@@ -293,8 +293,9 @@
 		next();
 		break;
 	case CONSTANT:
+		sym = yylval.sym;
+		np = node(emitconst, sym->type, SYM(sym), 0);
 		next();
-		/* TODO: do something */
 		break;
 	case '(':
 		next();
--- a/lex.c
+++ b/lex.c
@@ -24,11 +24,13 @@
 integer(char *s, char base)
 {
 	static Type *tp;
+	static Symbol *sym;
 	static char ch;
 
 	/* TODO: implement again */
 
-type:	switch (ch = toupper(getc(yyin))) {
+type:
+	switch (ch = toupper(getc(yyin))) {
 	case 'L':
 		goto type;
 	case 'U':
@@ -37,6 +39,10 @@
 		ungetc(ch, yyin);
 	}
 
+	sym = install("", NS_IDEN);
+	sym->type = inttype;
+	sym->u.i = atoi(yytext);
+	yynlval.sym = sym;
 	return CONSTANT;
 }
 
@@ -127,12 +133,14 @@
 		{NULL, 0, 0},
 	};
 	register Symbol *sym;
+	extern short symid;
 
 	for (bp = buff; bp->str; ++bp) {
-		sym = install(bp->str, NS_KEYWORD);
+		sym = install(bp->str, NS_IDEN);
 		sym->token = bp->token;
 		sym->u.token = bp->value;
 	}
+	symid = 0;
 }
 
 static uint8_t
--- a/symbol.c
+++ b/symbol.c
@@ -9,7 +9,8 @@
 #define NR_SYM_HASH 32
 
 uint8_t curctx;
-uint8_t namespace = NS_KEYWORD + 1 ;
+uint8_t namespace = NS_FREE + 1;
+short symid;
 
 static struct symtab {
 	Symbol *head;
@@ -77,34 +78,23 @@
 Symbol *
 install(char *s, uint8_t ns)
 {
-	register Symbol *sym;
-	register Symbol **t;
+	register Symbol *sym, **t;
 	struct symtab *tbl;
-	static short id;
 
-	if (ns == NS_KEYWORD) {
-		ns = NS_IDEN;
-	} else  {
-		++id;
-		if (s != NULL)
-			s = xstrdup(s);
-	}
-
 	sym = xcalloc(1, sizeof(*sym));
-	sym->name = s;
+	sym->name = xstrdup(s);
 	sym->ctx = curctx;
 	sym->token = IDEN;
 	sym->ns = ns;
-	sym->id = id;
+	sym->id = symid++;
 	tbl = &symtab[(ns >= NR_NAMESPACES) ? NS_IDEN : ns];
 	sym->next = tbl->head;
 	tbl->head = sym;
 
-	if (s != NULL) {
-		t = &tbl->htab[hash(s)];
-		sym->hash = *t;
-		*t = sym;
-	}
+
+	t = &tbl->htab[hash(s)];
+	sym->hash = *t;
+	*t = sym;
 
 	return sym;
 }