commit 6afdc62d5cf0b002c0b42cd7d5e6c30c8237d321
parent eb78fdb2b036f871216ba8f09748f3331a972259
Author: m21c <ho*******@gmail.com>
Date: Sat, 2 Oct 2021 14:28:49 +0200
imporoved code formatting
Diffstat:
| M | compiler.c | | | 292 | +++++++++++++++++++++++++++++++++++++++---------------------------------------- |
1 file changed, 145 insertions(+), 147 deletions(-)
diff --git a/compiler.c b/compiler.c
@@ -268,7 +268,7 @@ struct Decl {
Env *parentenv, *contentenv;
union {
- Node *content; /* init or function body */
+ Node *content; /* init or function body */
Decl *target; /* for field aliases */
} u;
@@ -945,45 +945,44 @@ error(SrcLoc *loc, const char *fmt, ...)
static int
tokenizealphanumeric(Source *source, register int c0)
{
- int keyword;
+ int keyword;
- while (isalnum(c0) || c0 == '_')
- c0 = source->line[++source->currloc.column];
+ while (isalnum(c0) || c0 == '_')
+ c0 = source->line[++source->currloc.column];
- keyword = getkeyword(
- source->line + source->tok.loc.column,
- source->currloc.column - source->tok.loc.column
- );
+ keyword = getkeyword(
+ source->line + source->tok.loc.column,
+ source->currloc.column - source->tok.loc.column
+ );
- if (source->tok.kind != '@' &&
- keyword >= 0 &&
- source->tok.kind != ODISP)
- {
- if (keyword == KOR - KSTART ||
- keyword == KAND - KSTART)
- {
- return source->tok.kind =
- keyword == KOR - KSTART ? OLOR : OLAND;
- } else if (keywordtypeids[keyword + KSTART]) {
- source->tok.u.key =
- keywordtypeids[keyword + KSTART];
- source->tok.type = prim + source->tok.u.key;
-
- return source->tok.kind = 'T';
- }
+ if (source->tok.kind != '@' && keyword >= 0 &&
+ source->tok.kind != ODISP)
+ {
+ if (keyword == KOR - KSTART || keyword == KAND - KSTART) {
+ return source->tok.kind =
+ keyword == KOR - KSTART ? OLOR : OLAND;
+ } else if (keywordtypeids[keyword + KSTART]) {
- return source->tok.kind = keyword + KSTART;
- }
+ source->tok.u.key =
+ keywordtypeids[keyword + KSTART];
- source->tok.u.key = getstringkey(
- &idents,
- source->line + source->tok.loc.column,
- source->currloc.column - source->tok.loc.column
- );
+ source->tok.type = prim + source->tok.u.key;
- return source->tok.kind = 'I';
+ return source->tok.kind = 'T';
+ }
+
+ return source->tok.kind = keyword + KSTART;
}
+ source->tok.u.key = getstringkey(
+ &idents,
+ source->line + source->tok.loc.column,
+ source->currloc.column - source->tok.loc.column
+ );
+
+ return source->tok.kind = 'I';
+}
+
static Type *
suffixfloattype(Source *source, const char *end)
{
@@ -1068,157 +1067,156 @@ errorint:
static int
tokenizenumber(Source *source, register int c0)
+{
+ int l = c0, t = source->line[source->currloc.column+1], i, j;
+ bool hasdec = false, hasexp = false;
+ char *end;
+
+advancenum:
+ while (isalnum(c0) || c0 == '_' || (c0 == '.' &&
+ source->line[source->currloc.column+1] != '.' && !hasdec))
{
- int l = c0, t = source->line[source->currloc.column+1], i, j;
- bool hasdec = false, hasexp = false;
- char *end;
-
- advancenum:
- while (isalnum(c0) || c0 == '_' || (c0 == '.' &&
- source->line[source->currloc.column+1] != '.' &&
- !hasdec))
- {
- if (c0 != '_')
- l = c0;
- if (c0 == '.')
- hasdec = true;
+ if (c0 != '_')
+ l = c0;
+ if (c0 == '.')
+ hasdec = true;
- c0 = source->line[++source->currloc.column];
- }
+ c0 = source->line[++source->currloc.column];
+ }
- if (hasdec && !hasexp && (c0 == '+' || c0 == '-')) {
- t = tolower(t);
- l = tolower(l);
+ if (hasdec && !hasexp && (c0 == '+' || c0 == '-')) {
+ t = tolower(t);
+ l = tolower(l);
- if ((l == 'e' && t != 'x') || (l == 'p' && t == 'x')) {
- c0 = source->line[++source->currloc.column];
- hasexp = true;
+ if ((l == 'e' && t != 'x') || (l == 'p' && t == 'x')) {
+ c0 = source->line[++source->currloc.column];
+ hasexp = true;
- goto advancenum;
- }
+ goto advancenum;
}
+ }
- /* remove underscores */
- for (j = 0, i = source->tok.loc.column;
- i < source->currloc.column;
- ++i)
- {
- if (source->line[i] != '_') {
- if (j >= lengthof(source->stringbuf) - 1) {
- error(
- &source->currloc,
- "number-literal is too long"
- );
-
- source->tok.u.u = 0;
- source->tok.type = prim + TINT;
+ /* remove underscores */
+ for (j = 0, i = source->tok.loc.column;
+ i < source->currloc.column;
+ ++i)
+ {
+ if (source->line[i] != '_') {
+ if (j >= lengthof(source->stringbuf) - 1) {
+ error(
+ &source->currloc,
+ "number-literal is too long"
+ );
- return source->tok.kind = 'N';
- }
+ source->tok.u.u = 0;
+ source->tok.type = prim + TINT;
- source->stringbuf[j++] = source->line[i];
+ return source->tok.kind = 'N';
}
+
+ source->stringbuf[j++] = source->line[i];
}
- source->stringbuf[j] = 0;
+ }
+ source->stringbuf[j] = 0;
- if (strpbrk(source->stringbuf, ".pPrR") ||
- (!strpbrk(source->stringbuf, "xX") &&
- strpbrk(source->stringbuf, "eEfF")))
- {
- source->tok.u.d = strtod(source->stringbuf, &end);
+ if (strpbrk(source->stringbuf, ".pPrR") ||
+ (!strpbrk(source->stringbuf, "xX") &&
+ strpbrk(source->stringbuf, "eEfF")))
+ {
+ source->tok.u.d = strtod(source->stringbuf, &end);
source->tok.type = suffixfloattype(source, end);
- } else {
- if (mystrncasecmp(source->stringbuf, "0b", 2) == 0) {
- source->tok.u.u = strtoull(
- source->stringbuf + 2,
- &end, 2
- );
-
- } else {
- source->tok.u.u = strtoull(
- source->stringbuf,
- &end, 0
- );
+ } else {
+ if (mystrncasecmp(source->stringbuf, "0b", 2) == 0) {
+ source->tok.u.u = strtoull(
+ source->stringbuf + 2,
+ &end, 2
+ );
- }
+ } else {
+ source->tok.u.u = strtoull(
+ source->stringbuf,
+ &end, 0
+ );
- source->tok.type = suffixinttype(source, end);
}
- return source->tok.kind = 'N';
+ source->tok.type = suffixinttype(source, end);
}
+ return source->tok.kind = 'N';
+}
+
static int
tokenizestring(Source *source, register int c0)
{
- int delim = c0, j;
+ int delim = c0, j;
- c0 = source->line[++source->currloc.column];
- source->tok.loc.column = source->currloc.column;
+ c0 = source->line[++source->currloc.column];
+ source->tok.loc.column = source->currloc.column;
- j = source->currloc.column;
- while (c0 != delim && c0 != 0) {
- if (c0 == '\\') {
- c0 = source->line[++source->currloc.column];
+ j = source->currloc.column;
+ while (c0 != delim && c0 != 0) {
+ if (c0 == '\\') {
+ c0 = source->line[++source->currloc.column];
- switch (c0) {
- case '\\':
- c0 = '\\';
- break;
+ switch (c0) {
+ case '\\':
+ c0 = '\\';
+ break;
- case 'n':
- c0 = '\n';
- break;
+ case 'n':
+ c0 = '\n';
+ break;
- case 'r':
- c0 = '\r';
- break;
+ case 'r':
+ c0 = '\r';
+ break;
- case 't':
- c0 = '\t';
- break;
+ case 't':
+ c0 = '\t';
+ break;
- case '\'':
- c0 = '\'';
- break;
+ case '\'':
+ c0 = '\'';
+ break;
- case '"':
- c0 = '"';
- break;
+ case '"':
+ c0 = '"';
+ break;
- /* TODO(m21c): read more escape sequences */
- case 0:
- goto stringeol;
+ /* TODO(m21c): read more escape sequences */
+ case 0:
+ goto stringeol;
- default:
+ default:
error(&source->currloc,
"invalid escape sequence '\\%c'", c0);
- }
}
-
- source->line[j++] = c0;
- c0 = source->line[++source->currloc.column];
}
- ++source->currloc.column;
- source->line[j++] = 0;
+ source->line[j++] = c0;
+ c0 = source->line[++source->currloc.column];
+ }
+
+ ++source->currloc.column;
+ source->line[j++] = 0;
- if (c0 == 0) {
- stringeol:
+ if (c0 == 0) {
+ stringeol:
error(&source->currloc, "unexpected end-of-line");
- return source->tok.kind = '\n';
- }
+ return source->tok.kind = '\n';
+ }
- /* TODO(m21c): read '\''-token as character-literal 'C' */
+ /* TODO(m21c): read '\''-token as character-literal 'C' */
- source->tok.u.key = getstringkey(
- &strings,
- source->line + source->tok.loc.column,
- j - source->tok.loc.column
- );
+ source->tok.u.key = getstringkey(
+ &strings,
+ source->line + source->tok.loc.column,
+ j - source->tok.loc.column
+ );
- return source->tok.kind = 'S';
+ return source->tok.kind = 'S';
}
static int
@@ -1330,18 +1328,18 @@ skipwhite:
case '<':
c0 = select('=', OLEQ,
- select('<',
- select('=', OLSHA, OLSH),
- OLET));
+ select('<',
+ select('=', OLSHA, OLSH),
+ OLET));
goto joinop;
case '>':
c0 = select('=', OGEQ,
+ select('>',
select('>',
- select('>',
- select('=', OARSHA, OARSH),
- select('=', ORSHA, ORSH)),
- OGRT));
+ select('=', OARSHA, OARSH),
+ select('=', ORSHA, ORSH)),
+ OGRT));
goto joinop;
case '&':