parse and print floating points

This commit is contained in:
Quentin Carbonneaux 2015-11-30 15:50:46 -05:00
parent 62a4ff4566
commit 7c97ca6aae

View file

@ -73,7 +73,8 @@ enum {
TD, TD,
TS, TS,
TNum, TInt,
TFlt,
TTmp, TTmp,
TLbl, TLbl,
TGlo, TGlo,
@ -93,6 +94,7 @@ enum {
static FILE *inf; static FILE *inf;
static int thead; static int thead;
static struct { static struct {
double flt;
int64_t num; int64_t num;
char *str; char *str;
} tokval; } tokval;
@ -167,6 +169,10 @@ lex()
return TRBrace; return TRBrace;
case '=': case '=':
return TEq; return TEq;
case '`':
if (fscanf(inf, "%lf", &tokval.flt) != 1)
err("invalid floating point");
return TFlt;
case '%': case '%':
t = TTmp; t = TTmp;
goto Alpha; goto Alpha;
@ -202,8 +208,7 @@ lex()
tokval.num += c - '0'; tokval.num += c - '0';
} }
ungetc(c, inf); ungetc(c, inf);
tokval.num *= sgn; return TInt;
return TNum;
} }
if (c == '"') { if (c == '"') {
tokval.str = vnew(0, 1); tokval.str = vnew(0, 1);
@ -322,14 +327,17 @@ parseref()
switch (next()) { switch (next()) {
case TTmp: case TTmp:
return tmpref(tokval.str); return tmpref(tokval.str);
case TNum: case TInt:
c = (Con){.type = CNum, .val = tokval.num}; c = (Con){.type = CNum, .val = tokval.num};
strcpy(c.label, ""); goto Look;
if (0) { case TFlt:
c = (Con){.type = CNum, .flt = 1};
memcpy(&c.val, &tokval.flt, sizeof c.val);
goto Look;
case TGlo: case TGlo:
c = (Con){.type = CAddr, .val = 0}; c = (Con){.type = CAddr, .val = 0};
strcpy(c.label, tokval.str); strcpy(c.label, tokval.str);
} Look:
for (i=0; i<ncon; i++) for (i=0; i<ncon; i++)
if (con[i].type == c.type if (con[i].type == c.type
&& con[i].val == c.val && con[i].val == c.val
@ -646,7 +654,7 @@ parsetyp()
strcpy(ty->name, tokval.str); strcpy(ty->name, tokval.str);
t = nextnl(); t = nextnl();
if (t == TAlign) { if (t == TAlign) {
if (nextnl() != TNum) if (nextnl() != TInt)
err("alignment expected"); err("alignment expected");
for (al=0; tokval.num /= 2; al++) for (al=0; tokval.num /= 2; al++)
; ;
@ -656,7 +664,7 @@ parsetyp()
if (t != TLBrace) if (t != TLBrace)
err("type body must start with {"); err("type body must start with {");
t = nextnl(); t = nextnl();
if (t == TNum) { if (t == TInt) {
ty->dark = 1; ty->dark = 1;
ty->size = tokval.num; ty->size = tokval.num;
if (ty->align == -1) if (ty->align == -1)
@ -689,7 +697,7 @@ parsetyp()
} }
} }
t = nextnl(); t = nextnl();
if (t == TNum) { if (t == TInt) {
c = tokval.num; c = tokval.num;
t = nextnl(); t = nextnl();
} else } else
@ -724,6 +732,8 @@ static void
parsedat(void cb(Dat *)) parsedat(void cb(Dat *))
{ {
char s[NString]; char s[NString];
float fs;
double fd;
int t; int t;
Dat d; Dat d;
@ -734,7 +744,7 @@ parsedat(void cb(Dat *))
strcpy(s, tokval.str); strcpy(s, tokval.str);
t = nextnl(); t = nextnl();
if (t == TAlign) { if (t == TAlign) {
if (nextnl() != TNum) if (nextnl() != TInt)
err("alignment expected"); err("alignment expected");
d.type = DAlign; d.type = DAlign;
d.u.num = tokval.num; d.u.num = tokval.num;
@ -757,14 +767,27 @@ parsedat(void cb(Dat *))
case TW: d.type = DW; break; case TW: d.type = DW; break;
case TH: d.type = DH; break; case TH: d.type = DH; break;
case TB: d.type = DB; break; case TB: d.type = DB; break;
case TS: d.type = DW; break;
case TD: d.type = DL; break;
} }
if (nextnl() != TNum) t = nextnl();
if (t != TInt && t != TFlt)
err("number expected"); err("number expected");
do { do {
d.u.num = tokval.num;
if (t == TFlt) {
fd = tokval.flt;
fs = tokval.flt;
d.u.num = 0;
if (d.type == DL)
memcpy(&d.u.num, &fd, sizeof fd);
else
memcpy(&d.u.num, &fs, sizeof fs);
} else
d.u.num = tokval.num;
cb(&d); cb(&d);
t = nextnl(); t = nextnl();
} while (t == TNum); } while (t == TInt || t == TFlt);
if (t == TRBrace) if (t == TRBrace)
break; break;
if (t != TComma) if (t != TComma)
@ -804,6 +827,8 @@ parse(FILE *f, void data(Dat *), void func(Fn *))
static void static void
printcon(Con *c, FILE *f) printcon(Con *c, FILE *f)
{ {
double d;
switch (c->type) { switch (c->type) {
case CUndef: case CUndef:
break; break;
@ -813,7 +838,11 @@ printcon(Con *c, FILE *f)
fprintf(f, "%+"PRIi64, c->val); fprintf(f, "%+"PRIi64, c->val);
break; break;
case CNum: case CNum:
fprintf(f, "%"PRIi64, c->val); if (c->flt) {
memcpy(&d, &c->val, sizeof d);
fprintf(f, "`%lf", d);
} else
fprintf(f, "%"PRIi64, c->val);
break; break;
} }
} }
@ -917,7 +946,7 @@ printfn(Fn *fn, FILE *f)
fprintf(f, "\t"); fprintf(f, "\t");
if (!req(i->to, R)) { if (!req(i->to, R)) {
printref(i->to, fn, f); printref(i->to, fn, f);
fprintf(f, " =%c", ktoc[i->cls]); fprintf(f, " =%c ", ktoc[i->cls]);
} }
assert(opdesc[i->op].name); assert(opdesc[i->op].name);
fprintf(f, "%s", opdesc[i->op].name); fprintf(f, "%s", opdesc[i->op].name);