commit 690e4af100608ab79133ca0743235ad8b9d9ad15
parent 688c48119a25ceef7429ee6cc280cfbe1b2c017a
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Tue, 15 Apr 2014 08:44:21 +0200
Install constant as symbols
It is necessary do this task, because in other case it is impossible
to know what value a previous constant had.
Diffstat:
4 files changed, 16 insertions(+), 3 deletions(-)
diff --git a/cc.h b/cc.h
@@ -227,7 +227,7 @@ enum {
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),
diff --git a/code.c b/code.c
@@ -71,6 +71,12 @@ emitsym(Node *np)
}
void
+emitconst(Node *np)
+{
+ printf("\t#%X", np->u.sym->u.i);
+}
+
+void
emitcast(Node *np)
{
Node *child = np->childs[0];
diff --git a/expr.c b/expr.c
@@ -235,8 +235,9 @@ primary(void)
next();
break;
case CONSTANT:
+ sym = yylval.sym;
+ np = node(emitconst, sym->type, SYM(sym), 0);
next();
- /* TODO: do something */
break;
case '(':
next();
diff --git a/lex.c b/lex.c
@@ -24,11 +24,13 @@ static uint8_t
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 @@ type: switch (ch = toupper(getc(yyin))) {
ungetc(ch, yyin);
}
+ sym = install("", NS_IDEN);
+ sym->type = inttype;
+ sym->u.i = atoi(yytext);
+ yynlval.sym = sym;
return CONSTANT;
}