commit 2be17bc418f7d887b43f3ddc0297aad6eeef9bd9
parent 502db9d50f65a31aecda5116fc65e803689c488f
Author: m21c <ho*******@gmail.com>
Date: Thu, 2 Feb 2023 02:50:32 +0100
use (u)intmax_t + fixed code and print formatting
Diffstat:
| M | compiler.c | | | 178 | ++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------- |
1 file changed, 113 insertions(+), 65 deletions(-)
diff --git a/compiler.c b/compiler.c
@@ -268,7 +268,7 @@ struct Env Env;
typedef
enum Flags {
- FRASSOC = 1,
+ FRASSOC = 1
} Flads;
typedef
@@ -330,7 +330,7 @@ enum DeclKind {
DVAR,
DPARAM,
DFUNCTION,
- DFIELDALIAS,
+ DFIELDALIAS
/*
DMACRO,
DENFOLD
@@ -349,7 +349,7 @@ enum EnvKind {
SIF,
SELSE,
SSTRUCT,
- SUNION,
+ SUNION
/*
SUNION,
SSTRUCT,
@@ -373,7 +373,7 @@ enum Qualifier {
QVISIB = QEXTERN | QINTERN,
QSTORAGE = QSTATIC,
QTYPE = QCONST,
- QINFER = QVAR,
+ QINFER = QVAR
} Qualifier;
@@ -398,8 +398,8 @@ struct Node {
int key;
double d;
- uint64_t u;
- int64_t s;
+ uintmax_t u;
+ intmax_t s;
Node *payload;
Decl *declref;
@@ -576,7 +576,9 @@ const uint8_t opinfo[] = {
/* SECTION: - utility functions - */
+#ifndef lengthof
#define lengthof(array) ((int) sizeof(array) / (int) sizeof(*(array)))
+#endif
static int
mystrncasecmp(const char *str1, const char *str2, size_t max_len)
@@ -1125,7 +1127,7 @@ advancenum:
/* remove underscores */
for (j = 0, i = source->tok.loc.column;
- i < source->currloc.column;
+ i < (int) source->currloc.column;
++i)
{
if (source->line[i] != '_') {
@@ -2586,7 +2588,7 @@ readatom(Source *source, int flags)
lhs = tokennode(source, NULL);
lhs->kind = NUMBER;
lhs->type = primitive(TBOOL);
- lhs->u.u = (uint64_t) (getkind(source) == KTRUE);
+ lhs->u.u = (uintmax_t) (getkind(source) == KTRUE);
gettok(source);
break;
@@ -2594,7 +2596,7 @@ readatom(Source *source, int flags)
lhs = tokennode(source, NULL);
lhs->kind = NUMBER;
lhs->type = maketype(&source->tok.loc, primitive(TPTR), primitive(TVOID));
- lhs->u.u = (uint64_t) (getkind(source) == KTRUE);
+ lhs->u.u = (uintmax_t) (getkind(source) == KTRUE);
gettok(source);
break;
@@ -3089,8 +3091,8 @@ islvalue(Node *node)
}
/* TODO(m21c): also mask int/float values in the tokenizer */
-static uint64_t
-maskint(int size, uint64_t value)
+static uintmax_t
+maskint(int size, uintmax_t value)
{
if (size == 1) return value & 0xfful;
if (size == 2) return value & 0xfffful;
@@ -3107,13 +3109,13 @@ maskfloat(int size, double value)
return value;
}
-static uint64_t
-convint(int srcsize, bool srcsigned, uint64_t value)
+static uintmax_t
+convint(int srcsize, bool srcsigned, uintmax_t value)
{
if (!srcsigned) return value;
- if (srcsize == 1) return (uint64_t) (int8_t ) value;
- if (srcsize == 2) return (uint64_t) (int16_t) value;
- if (srcsize == 4) return (uint64_t) (int32_t) value;
+ if (srcsize == 1) return (uintmax_t) (int8_t ) value;
+ if (srcsize == 2) return (uintmax_t) (int16_t) value;
+ if (srcsize == 4) return (uintmax_t) (int32_t) value;
return value;
}
@@ -3145,14 +3147,14 @@ wrap(Type *type, Node *node)
} else if (isintorbooltype(type)) {
node->u.u = maskint(
type->size,
- (int64_t) node->u.d
+ (intmax_t) node->u.d
);
}
} else if (isintorbooltype(nodetype)) {
if (isfloattype(type)) {
node->u.d = maskfloat(
type->size, (double)
- (int64_t) convint(node->type->size,
+ (intmax_t) convint(node->type->size,
!isunsignedtype(node->type),
node->u.u
)
@@ -3232,6 +3234,8 @@ arithtuplereorder(Env *env, Node *expr, int numops)
{
Node *tmp;
+ (void) env;
+
if (numops == 2) {
if (expr->lhs->kind != ACOMMA)
return false;
@@ -3941,11 +3945,11 @@ tryprompt(Source *source, const char ch)
Env *currenv = source->currenv;
if (ch == '.' && currenv && currenv->kind != STOPLEVEL) {
- fputs("\e[1;30m", stdout);
+ fputs("\x1b[1;30m", stdout);
promptenvpath(currenv);
- fprintf(stdout, "\n\e[35m%c \e[0m", ch);
+ fprintf(stdout, "\n\x1b[35m%c \x1b[0m", ch);
} else {
- fprintf(stdout, "\e[35m%c \e[0m", ch);
+ fprintf(stdout, "\x1b[35m%c \x1b[0m", ch);
}
} else if (source->filein == stdin) {
@@ -3995,7 +3999,7 @@ highlight(FILE *out, Highlight kind)
return 0;
if (kind == HLNONE)
- return lasthighlight = kind, fprintf(out, "\e[0m");
+ return lasthighlight = kind, fprintf(out, "\x1b[0m");
if (lasthighlight == HLDELIM || kind == HLDELIM ||
lasthighlight == HLFUNCTION || kind == HLFUNCTION ||
@@ -4006,61 +4010,61 @@ highlight(FILE *out, Highlight kind)
lasthighlight == HLDECL || kind == HLDECL ||
#endif
lasthighlight == HLUNKNOWN || kind == HLUNKNOWN)
- n += fprintf(out, "\e[0m");
+ n += fprintf(out, "\x1b[0m");
switch (kind) {
case HLDELIM:
- n += fprintf(out, "\e[2m");
+ n += fprintf(out, "\x1b[2m");
break;
case HLUNKNOWN:
- n += fprintf(out, "\e[41;30m");
+ n += fprintf(out, "\x1b[41;30m");
break;
case HLKEYWORD:
- n += fprintf(out, "\e[35m");
+ n += fprintf(out, "\x1b[35m");
break;
case HLNUMBER:
- n += fprintf(out, "\e[36m");
+ n += fprintf(out, "\x1b[36m");
break;
case HLSTRING:
- n += fprintf(out, "\e[31m");
+ n += fprintf(out, "\x1b[31m");
break;
case HLTYPE:
- n += fprintf(out, "\e[34m");
+ n += fprintf(out, "\x1b[34m");
break;
case HLFUNCTION:
- n += fprintf(out, "\e[1;3m");
+ n += fprintf(out, "\x1b[1;3m");
break;
case HLPARAM:
- n += fprintf(out, "\e[3m");
+ n += fprintf(out, "\x1b[3m");
break;
#if 0
case HLFUNCTIONDECL:
- n += fprintf(out, "\e[1;4;3m");
+ n += fprintf(out, "\x1b[1;4;3m");
break;
case HLPARAMDECL:
- n += fprintf(out, "\e[4;3m");
+ n += fprintf(out, "\x1b[4;3m");
break;
case HLDECL:
- n += fprintf(out, "\e[4m");
+ n += fprintf(out, "\x1b[4m");
break;
#endif
case HLINFO:
- n += fprintf(out, "\e[33m");
+ n += fprintf(out, "\x1b[33m");
break;
case HLPROMPT:
- n += fprintf(out, "\e[35m");
+ n += fprintf(out, "\x1b[35m");
break;
default:
break;
@@ -4202,8 +4206,9 @@ isclauseorempty(Node *expr)
return true;
kind = expr->kind;
- return kind == KDO || kind == KIF || kind == KFOR || kind == KGOTO ||
- kind == KRETURN || kind == KBREAK || kind == KCONTINUE;
+ return kind == KDO || kind == KIF || kind == KFOR ||
+ kind == KGOTO || kind == KRETURN || kind == KBREAK ||
+ kind == KCONTINUE;
}
static int
@@ -4280,16 +4285,17 @@ printoperant(FILE *out, Node *expr, int opprec, bool braceequalprec, int indent)
return 0;
prec = getprec(expr->kind);
- if (!isatomnode(expr->kind) && (
- !getnumops(expr->kind) || prec < opprec ||
- (braceequalprec && prec == opprec)))
- {
+ if (!isatomnode(expr->kind) && (!getnumops(expr->kind) ||
+ prec < opprec ||
+ (braceequalprec && prec == opprec))) {
n += highlight(out, HLDELIM);
+
n += fprintf(out, "(");
n += highlight(out, HLNONE);
n += printexpr(out, expr, indent);
n += highlight(out, HLDELIM);
n += fprintf(out, ")");
+
n += highlight(out, HLNONE);
} else {
n += printexpr(out, expr, indent);
@@ -4393,20 +4399,32 @@ printexpr(FILE *out, Node *expr, int indent)
}
if (getnumops(expr->kind) == 2) {
- n += printoperant(out, expr->lhs, getprec(expr->kind), israssoc(expr->kind), indent);
+ n += printoperant(out, expr->lhs,
+ getprec(expr->kind),
+ israssoc(expr->kind),
+ indent);
+
n += highlight(out, HLDELIM);
n += fprintf(out, " %s ", nodestrings[expr->kind]);
- n += printoperant(out, expr->rhs, getprec(expr->kind), !israssoc(expr->kind), indent);
+
+ n += printoperant(out, expr->rhs,
+ getprec(expr->kind),
+ !israssoc(expr->kind),
+ indent);
+
} else if (getnumops(expr->kind) == 1) {
if (getprec(expr->kind) == PUNSUF) {
printoperant(out, expr->lhs, PUNSUF, false, indent);
+
switch (expr->kind) {
case OARRAY:
case OCALL:
n += highlight(out, HLDELIM);
n += fprintf(out, "%c", nodestrings[expr->kind][0]);
+
if (expr->rhs)
n += printexpr(out, expr->rhs, indent);
+
n += highlight(out, HLDELIM);
n += fprintf(out, "%c", nodestrings[expr->kind][1]);
break;
@@ -4421,6 +4439,7 @@ printexpr(FILE *out, Node *expr, int indent)
n += highlight(out, HLDELIM);
n += fprintf(out, "%s", nodestrings[expr->kind]);
}
+
} else {
switch (expr->kind) {
case OCAST:
@@ -4435,6 +4454,7 @@ printexpr(FILE *out, Node *expr, int indent)
default:
n += highlight(out, HLDELIM);
n += fprintf(out, "%s", nodestrings[expr->kind]);
+
if (getprec(expr->lhs->kind) == PUNARY &&
expr->kind != ODEREF &&
expr->kind != OADDR)
@@ -4445,6 +4465,7 @@ printexpr(FILE *out, Node *expr, int indent)
n += printoperant(out, expr->lhs, PUNARY, false, indent);
}
+
} else {
switch (expr->kind)
{
@@ -4466,13 +4487,13 @@ printexpr(FILE *out, Node *expr, int indent)
case TINFER:
case TS8: case TS16: case TS32: case TS64:
- n += fprintf(out, "%li", expr->u.s);
+ n += fprintf(out, "%lli", expr->u.s);
n += printtypesuffix(out, expr->type, indent);
break;
case TUINFER:
case TU8: case TU16: case TU32: case TU64:
- n += fprintf(out, "%lu", expr->u.s);
+ n += fprintf(out, "%llu", expr->u.s);
n += printtypesuffix(out, expr->type, indent);
break;
@@ -4482,7 +4503,7 @@ printexpr(FILE *out, Node *expr, int indent)
else if (expr->u.u == 1)
n += fprintf(out, "true");
else
- n += fprintf(out, "0x%016lx", expr->u.u);
+ n += fprintf(out, "0x%016llx", expr->u.u);
break;
@@ -4490,7 +4511,7 @@ printexpr(FILE *out, Node *expr, int indent)
if (expr->u.u == 0)
n += fprintf(out, "null");
else
- n += fprintf(out, "0x%016lx", expr->u.u);
+ n += fprintf(out, "0x%016llx", expr->u.u);
break;
case TVOID:
@@ -4508,11 +4529,12 @@ printexpr(FILE *out, Node *expr, int indent)
break;
case ADECLREF:
- n += highlight(out,
- expr->u.declref->kind == DFUNCTION ? HLFUNCTION :
- expr->u.declref->kind == DPARAM ? HLPARAM :
- HLIDENT);
- n += fprintf(out, "%s", getstring(idents, expr->u.declref->key));
+ n += highlight(out, expr->u.declref->kind == DFUNCTION ?
+ HLFUNCTION : expr->u.declref->kind == DPARAM ?
+ HLPARAM : HLIDENT);
+
+ n += fprintf(out, "%s",
+ getstring(idents, expr->u.declref->key));
break;
case ADECL:
@@ -4532,6 +4554,7 @@ printexpr(FILE *out, Node *expr, int indent)
n += highlight(out, HLKEYWORD);
n += fprintf(out, "%s", nodestrings[expr->kind]);
n += highlight(out, HLDELIM);
+
n += fprintf(out, "(");
n += printexpr(out, expr->lhs, indent);
n += highlight(out, HLDELIM);
@@ -4543,9 +4566,11 @@ printexpr(FILE *out, Node *expr, int indent)
n += fprintf(out, "bitcast");
n += highlight(out, HLDELIM);
n += fprintf(out, "(");
+
n += highlight(out, HLTYPE);
n += printtype(out, expr->rhs->type, indent);
n += highlight(out, HLDELIM);
+
n += fprintf(out, ") (");
n += printexpr(out, expr->lhs, indent);
n += highlight(out, HLDELIM);
@@ -4577,12 +4602,15 @@ printexpr(FILE *out, Node *expr, int indent)
n += fprintf(out, "if ");
n += printexpr(out, expr->u.payload, indent);
n += printclause(out, expr->lhs, indent);
+
if (expr->rhs) {
int i;
n += fprintf(out, "\n");
+
for (i = 0; i < indent; ++i)
n += fprintf(out, "\t");
+
n += highlight(out, HLKEYWORD);
n += fprintf(out, "else");
n += printclause(out, expr->rhs, indent);
@@ -4590,6 +4618,7 @@ printexpr(FILE *out, Node *expr, int indent)
break;
+
case KDO:
n += highlight(out, HLKEYWORD);
n += fprintf(out, "do");
@@ -4599,10 +4628,14 @@ printexpr(FILE *out, Node *expr, int indent)
case KUNION:
case KSTRUCT:
n += highlight(out, HLKEYWORD);
- n += fprintf(out, expr->kind == KSTRUCT ? "struct" : "union");
+
+ n += fprintf(out,
+ expr->kind == KSTRUCT ? "struct" : "union");
+
if (expr->lhs && expr->lhs->kind == IDENT) {
n += highlight(out, HLTYPE);
- n += fprintf(out, " %s", getstring(idents, expr->lhs->u.key));
+ n += fprintf(out, " %s",
+ getstring(idents, expr->lhs->u.key));
}
if (expr->rhs)
@@ -4635,11 +4668,13 @@ printexpr(FILE *out, Node *expr, int indent)
break;
case ACONV:
- n += highlight(out, HLDELIM);
+ n += highlight(out, HLNUMBER);
n += fprintf(out, "conv(");
+
n += highlight(out, HLTYPE);
n += printtype(out, expr->type, indent);
n += highlight(out, HLDELIM);
+
n += fprintf(out, ") ");
n += printoperant(out, expr->lhs, PUNARY, false, indent);
break;
@@ -4773,13 +4808,16 @@ processcommand(Source *source)
if (!strcmp(command, "delete")) {
command = isolatecommand(&commandline);
- if (!strcmp(command, "node") || !strcmp(command, "ast-node") ||
- isdigit(*command))
+ if (!strcmp(command, "node") ||
+ !strcmp(command, "ast-node") ||
+ isdigit(*command))
{
+ int i;
+
if (!isdigit(*command))
command = isolatecommand(&commandline);
- int i = atoi(command);
+ i = atoi(command);
if (i < 4096 && i >= 0) {
Node *node = nodebuf + i;
@@ -4830,7 +4868,8 @@ main(int argc, char **argv)
pushenv(source, STOPLEVEL);
while (getkind(source) != 0) {
- /* printf("token:%i:%i: %c '%.*s'\n", lastline, lastcol + 1, tok.u.id, currcol - lastcol, line + lastcol);*/
+ /* printf("token:%i:%i: %c '%.*s'\n", lastline, lastcol + 1,
+ tok.u.id, currcol - lastcol, line + lastcol);*/
Node *ast;
ast = exprlist(source, false, NULL);
@@ -4839,8 +4878,13 @@ main(int argc, char **argv)
printast(ast, 0);
printf("\n");
*/
- if (ast->kind != ADECL || !ast->u.payload || ast->u.payload->kind != ASCOPE)
- ast = foldexpr(source->currenv, typecheck(source->currenv, ast));
+ if (ast->kind != ADECL ||
+ !ast->u.payload ||
+ ast->u.payload->kind != ASCOPE)
+ {
+ ast = typecheck(source->currenv, ast);
+ ast = foldexpr(source->currenv, ast);
+ }
if (source->filein == stdin) {
highlight(stdout, HLINFO);
@@ -4873,10 +4917,13 @@ main(int argc, char **argv)
gettok(source);
}
- if (source->lastkind != SEMIDELIM && source->lastkind != LINEDELIM) {
+ if (source->lastkind != SEMIDELIM &&
+ source->lastkind != LINEDELIM)
+ {
error(getloc(source), "expected new line");
while (getkind(source) != SEMIDELIM &&
- getkind(source) != LINEDELIM && getkind(source) != 0)
+ getkind(source) != LINEDELIM &&
+ getkind(source) != 0)
{
gettok(source);
}
@@ -4899,7 +4946,8 @@ main(int argc, char **argv)
for (p = source->pendingenvhead; p; p = p->pendingnext) {
if (p->stmts) {
- p->stmts = foldexpr(source->currenv, typecheck(source->currenv, p->stmts));
+ p->stmts = typecheck(source->currenv, p->stmts);
+ p->stmts = foldexpr(source->currenv, p->stmts);
/* debug prints: */
highlight(stdout, HLINFO);