commit e6fbea4aa2822078e52e9e5b99a1b6441f848b60
parent e2273e5e06a0711763181533fc20568c3b2aeb9a
Author: m21c <ho*******@gmail.com>
Date: Sat, 18 Sep 2021 21:29:46 +0200
changed coding style: open curly-braces of functions are on a new line
Diffstat:
| M | compiler.c | | | 207 | ++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------- |
1 file changed, 137 insertions(+), 70 deletions(-)
diff --git a/compiler.c b/compiler.c
@@ -110,8 +110,9 @@ enum Kind {
#define isoperator(kind) ((kind) >= OSTART && (kind) < ASTART)
#define isastnode(kind) ((kind) >= ASTART && (kind) < MAXKINDS)
-bool
-isatomnode(Kind kind) {
+static bool
+isatomnode(Kind kind)
+{
return kind == 'I' || kind == ADECLREF || kind == 'N' || kind == 'S' ||
kind == 'C';
}
@@ -599,7 +600,8 @@ const uint8_t opinfo[] = {
#define lengthof(array) ((int) sizeof(array) / (int) sizeof(*(array)))
static int
-mystrncasecmp(const char *str1, const char *str2, size_t max_len) {
+mystrncasecmp(const char *str1, const char *str2, size_t max_len)
+{
char tmp1[] = {'\0', '\0'};
char tmp2[] = {'\0', '\0'};
char c1, c2;
@@ -627,7 +629,8 @@ mystrncasecmp(const char *str1, const char *str2, size_t max_len) {
}
static int
-mystrcasecmp(const char *str1, const char *str2) {
+mystrcasecmp(const char *str1, const char *str2)
+{
char tmp1[] = {'\0', '\0'};
char tmp2[] = {'\0', '\0'};
char c1, c2;
@@ -653,7 +656,8 @@ mystrcasecmp(const char *str1, const char *str2) {
/* - pre-lexer - */
static void
-tryprompt(Source *source, const char ch) {
+tryprompt(Source *source, const char ch)
+{
if (source->handlereplprompt) {
fprintf(stdout, "\e[35m%c \e[0m", ch);
} else if (source->filein == stdin) {
@@ -662,7 +666,8 @@ tryprompt(Source *source, const char ch) {
}
static bool
-mygetline(Source *source) {
+mygetline(Source *source)
+{
int i, l, c;
FILE *in = source->filein;
@@ -739,7 +744,8 @@ const char *keywordkeys[KEYWORD_MAP_SIZE];
int keywordvals[KEYWORD_MAP_SIZE];
static int
-strnhash(const char *str, int n) {
+strnhash(const char *str, int n)
+{
int hash = 5381, i;
for (i = 0; i < n && str[i]; ++i)
hash = (hash << 5) + hash + str[i];
@@ -747,7 +753,8 @@ strnhash(const char *str, int n) {
}
static void
-initkeywords(void) {
+initkeywords(void)
+{
int i, j, h;
for (i = 0; i < lengthof(keywordlengths); ++i) {
int n = keywordlengths[i] = strlen(nodestrings[i + KSTART]);
@@ -775,8 +782,8 @@ initkeywords(void) {
}
static int
-getkeyword(const char *str, int n) {
-
+getkeyword(const char *str, int n)
+{
int i, h = strnhash(str, n) & (lengthof(keywordkeys) - 8);
for (i = 0; i < 8; ++i, ++h) {
int len;
@@ -812,7 +819,8 @@ StringMap idents;
StringMap strings;
static void
-initstrmap(StringMap *map) {
+initstrmap(StringMap *map)
+{
map->keys = calloc(32, sizeof(int));
map->keyscap = 32;
assert(map->keys);
@@ -824,7 +832,8 @@ initstrmap(StringMap *map) {
}
static void
-disposestrmap(StringMap *map) {
+disposestrmap(StringMap *map)
+{
int i;
for (i = map->valslen - 1; i >= 0; --i) {
free((char *) map->vals[i].str);
@@ -835,7 +844,8 @@ disposestrmap(StringMap *map) {
}
static void
-putstringkey(StringMap *map, int key, int hash) {
+putstringkey(StringMap *map, int key, int hash)
+{
int *keys = map->keys;
StringEntry *vals = map->vals;
@@ -864,7 +874,8 @@ redo:
int auxthen;
static int
-getstringkey(StringMap *map, const char *str, int n) {
+getstringkey(StringMap *map, const char *str, int n)
+{
int *keys = map->keys;
StringEntry *vals = map->vals;
@@ -915,7 +926,8 @@ getstringkey(StringMap *map, const char *str, int n) {
/* - error reporting - */
static int
-warn(SrcLoc *loc, const char *fmt, ...) {
+warn(SrcLoc *loc, const char *fmt, ...)
+{
va_list ap;
int n;
@@ -934,7 +946,8 @@ warn(SrcLoc *loc, const char *fmt, ...) {
}
static int
-error(SrcLoc *loc, const char *fmt, ...) {
+error(SrcLoc *loc, const char *fmt, ...)
+{
va_list ap;
int n;
@@ -960,7 +973,8 @@ error(SrcLoc *loc, const char *fmt, ...) {
((indent) + (source)->tabwidth - ((indent) % (source)->tabwidth))
static int
-gettok(Source *source) {
+gettok(Source *source)
+{
register int c0 = (uchar) source->line[source->currloc.column];
static bool hasnewline = false;
@@ -1406,7 +1420,8 @@ skipwhite:
((source)->tok.kind == '\n' ? (void) gettok(source) : (void) 0)
static bool
-isbasicdelimiter(Kind kind) {
+isbasicdelimiter(Kind kind)
+{
switch ((int) kind) {
case 0:
case '\n': case ',': case ';':
@@ -1421,7 +1436,8 @@ isbasicdelimiter(Kind kind) {
}
static Kind
-getunary(Source *source) {
+getunary(Source *source)
+{
Kind kind = source->tok.kind;
if (getprec(kind) == PUNARY)
@@ -1439,7 +1455,8 @@ getunary(Source *source) {
}
static bool
-isdelimiter(Kind kind) {
+isdelimiter(Kind kind)
+{
if (isbasicdelimiter(kind))
return true;
@@ -1453,7 +1470,8 @@ isdelimiter(Kind kind) {
}
static Kind
-getunarysuffix(Source *source) {
+getunarysuffix(Source *source)
+{
Kind kind = source->tok.kind;
if (getprec(kind) == PUNSUF)
@@ -1480,7 +1498,8 @@ int nodetop;
#define tokennode(source, lhs) makenode(&(source)->tok, (lhs))
static Node *
-makenode(Node *orig, Node *lhs) {
+makenode(Node *orig, Node *lhs)
+{
Node *node = nodebuf + nodetop++;
*node = *orig;
@@ -1498,7 +1517,8 @@ Type typebuf[4096];
int typetop;
static Type *
-maketype(SrcLoc *loc, Type *orig, Type *target) {
+maketype(SrcLoc *loc, Type *orig, Type *target)
+{
Type *ty = typebuf + typetop++;
*ty = *orig;
@@ -1516,7 +1536,8 @@ Env envbuf[4096];
int envtop;
static Decl *
-finddeclinenv(int key, Env *env) {
+finddeclinenv(int key, Env *env)
+{
const int cacheindex = (key >> 3) & 0x3f;
const int cachebit = 1 << (key & 0x03);
@@ -1534,7 +1555,8 @@ finddeclinenv(int key, Env *env) {
}
static Decl *
-finddeclaration(Source *source, Env *startenv, int key) {
+finddeclaration(Source *source, Env *startenv, int key)
+{
const int cacheindex = (key >> 3) & 0x3f;
const int cachebit = 1 << (key & 0x03);
@@ -1580,7 +1602,8 @@ finddeclaration(Source *source, Env *startenv, int key) {
}
static Env *
-setheadenv(Source *source, EnvKind kind) {
+setheadenv(Source *source, EnvKind kind)
+{
/* NOTE(m21c): this might only be useful for parameter => function env
* translation */
Env *env = envbuf + envtop++;
@@ -1602,7 +1625,8 @@ setheadenv(Source *source, EnvKind kind) {
}
static Env *
-pushenv(Source *source, EnvKind kind) {
+pushenv(Source *source, EnvKind kind)
+{
Env *env;
if (source->headenv) {
@@ -1630,7 +1654,8 @@ pushenv(Source *source, EnvKind kind) {
}
static Env *
-popenv(Source *source) {
+popenv(Source *source)
+{
Env *currenv = source->currenv;
Env *env = currenv;
@@ -1641,7 +1666,8 @@ popenv(Source *source) {
}
static void
-deferfuncenv(Source *source, int keydeclinfunc) {
+deferfuncenv(Source *source, int keydeclinfunc)
+{
Env *env, *funcenv = NULL;
for (env = source->currenv; env; env = env->below) {
@@ -1683,7 +1709,8 @@ Decl declbuf[4096];
int decltop;
static void
-appenddecltoenv(Decl *decl, Env *targetenv) {
+appenddecltoenv(Decl *decl, Env *targetenv)
+{
const int key = decl->key;
const int cacheindex = (key >> 3) & 0x3f;
const int cachebit = 1 << (key & 0x03);
@@ -1705,7 +1732,8 @@ appenddecltoenv(Decl *decl, Env *targetenv) {
}
static void
-removedeclfromenv(Decl *decl) {
+removedeclfromenv(Decl *decl)
+{
Env *sourceenv = decl->parentenv;
if (decl->prev)
@@ -1723,7 +1751,8 @@ removedeclfromenv(Decl *decl) {
}
static Decl *
-makedecl(Source *source, int key, DeclKind kind) {
+makedecl(Source *source, int key, DeclKind kind)
+{
Env *currenv = source->currenv;
Decl *decl;
@@ -1770,7 +1799,8 @@ makedecl(Source *source, int key, DeclKind kind) {
}
static Decl *
-defertypedeclaration(Source *source, int key) {
+defertypedeclaration(Source *source, int key)
+{
Env *savedcurrenv = source->currenv;
Decl *decl;
@@ -1792,7 +1822,8 @@ defertypedeclaration(Source *source, int key) {
(&(source)->tok.loc)
static bool
-expect(Source *source, int kind, const char *fmt, ...) {
+expect(Source *source, int kind, const char *fmt, ...)
+{
va_list ap;
int line = source->tok.loc.line;
@@ -1815,7 +1846,8 @@ expect(Source *source, int kind, const char *fmt, ...) {
}
static int
-qualifiers(Source *source, int allowmask) {
+qualifiers(Source *source, int allowmask)
+{
int flags = 0, mask = allowmask;
while (iskeyword(getkind(source))) {
@@ -1906,7 +1938,8 @@ static Node *
exprlist(Source *source, bool isparam, Type *paramtype);
static Node *
-stmtlist(Source *source, int indent, EnvKind envkind) {
+stmtlist(Source *source, int indent, EnvKind envkind)
+{
Node *head = NULL, *tail = NULL;
int needindent = nextindent(source, indent);
@@ -1972,7 +2005,8 @@ static Node *
readexpr(Source *source, int minprec);
static Type *
-gettype(Source *source, Type *basetype) {
+gettype(Source *source, Type *basetype)
+{
int flags;
if (!basetype)
@@ -2005,7 +2039,8 @@ advance:
}
static Node *
-declaration(Source *source, Type *ty) {
+declaration(Source *source, Type *ty)
+{
bool selfparam = false;
Decl *decl = NULL;
Node *result = NULL;
@@ -2149,7 +2184,8 @@ finish:
}
static Node *
-readident(Source *source, int flags) {
+readident(Source *source, int flags)
+{
Node *lhs = NULL;
Decl *decl = NULL;
SrcLoc loc = source->tok.loc;
@@ -2192,7 +2228,8 @@ readident(Source *source, int flags) {
}
static Node *
-readrecord(Source *source, bool isunion) {
+readrecord(Source *source, bool isunion)
+{
Node *recordnode;
Decl *module;
int indent = source->lastindent;
@@ -2223,7 +2260,8 @@ readrecord(Source *source, bool isunion) {
}
static Node *
-readatom(Source *source, int flags) {
+readatom(Source *source, int flags)
+{
Node *lhs = NULL, *savedis = source->lastis;
int indent;
@@ -2551,7 +2589,8 @@ readatom(Source *source, int flags) {
}
static Node *
-readexpr(Source *source, int minprec) {
+readexpr(Source *source, int minprec)
+{
Node *lhs = readatom(source, 0), *last = NULL;
/* only binary expr */
@@ -2592,7 +2631,8 @@ readexpr(Source *source, int minprec) {
}
static Node *
-todeclaration(Node *curr, Node **ty) {
+todeclaration(Node *curr, Node **ty)
+{
if (*ty) {
if (curr->kind == 'I') {
Node *decl = makenode(curr, *ty);
@@ -2620,7 +2660,8 @@ todeclaration(Node *curr, Node **ty) {
* comma-expressions (comma-operator, param-list, declaration-list,
* type-tuples and expression-tuples) */
static Node *
-exprlist(Source *source, bool isparam, Type *paramtype) {
+exprlist(Source *source, bool isparam, Type *paramtype)
+{
Node *lhs;
bool isdeclaration, typetuple;
@@ -2713,7 +2754,8 @@ exprlist(Source *source, bool isparam, Type *paramtype) {
/* - type-checking & folding - */
static bool
-isinttype(Type *ty) {
+isinttype(Type *ty)
+{
switch (ty->kind) {
case TINFER: case TUINFER:
case TS8: case TU8:
@@ -2728,7 +2770,8 @@ isinttype(Type *ty) {
}
static bool
-isfloattype(Type *ty) {
+isfloattype(Type *ty)
+{
switch (ty->kind) {
case TF32: case TF64:
return true;
@@ -2739,7 +2782,8 @@ isfloattype(Type *ty) {
}
static bool
-isarithtype(Type *ty) {
+isarithtype(Type *ty)
+{
switch (ty->kind) {
case TBOOL:
case TINFER: case TUINFER:
@@ -2756,7 +2800,8 @@ isarithtype(Type *ty) {
}
static bool
-isunsignedtype(Type *ty) {
+isunsignedtype(Type *ty)
+{
switch (ty->kind) {
case TBOOL: case TUINFER:
case TU8: case TU16:
@@ -2770,7 +2815,8 @@ isunsignedtype(Type *ty) {
/* TODO(m21c): also mask int/float values in the tokenizer */
static uint64_t
-maskint(int size, uint64_t value) {
+maskint(int size, uint64_t value)
+{
if (size == 1) return value & 0xfful;
if (size == 2) return value & 0xfffful;
if (size == 4) return value & 0xfffffffful;
@@ -2779,14 +2825,16 @@ maskint(int size, uint64_t value) {
}
static double
-maskfloat(int size, double value) {
+maskfloat(int size, double value)
+{
if (size == 4) return (double) (float) value;
return value;
}
static uint64_t
-convint(int srcsize, bool srcsigned, uint64_t value) {
+convint(int srcsize, bool srcsigned, uint64_t value)
+{
if (!srcsigned) return value;
if (srcsize == 1) return (uint64_t) (int8_t ) value;
if (srcsize == 2) return (uint64_t) (int16_t) value;
@@ -2799,7 +2847,8 @@ static Node *
conv(Node *node);
static Node *
-autoref(Type *ty, Node *node) {
+autoref(Type *ty, Node *node)
+{
int numderefs = 0, i;
Node *n;
Type *t;
@@ -2866,7 +2915,8 @@ autoref(Type *ty, Node *node) {
}
static Node *
-wrap(Type *ty, Node *node) {
+wrap(Type *ty, Node *node)
+{
assert(ty);
assert(node->type);
@@ -2924,7 +2974,8 @@ wrap(Type *ty, Node *node) {
}
static Node *
-conv(Node *node) {
+conv(Node *node)
+{
Type *ty = node->type;
assert(ty);
@@ -2942,7 +2993,8 @@ static Node *
typecheck(Env *env, Node *expr);
static bool
-arithtuplereorder(Env *env, Node *expr, int numops) {
+arithtuplereorder(Env *env, Node *expr, int numops)
+{
Node *tmp;
if (numops == 2) {
@@ -2984,7 +3036,8 @@ arithtuplereorder(Env *env, Node *expr, int numops) {
}
static Node *
-typecheck(Env *env, Node *expr) {
+typecheck(Env *env, Node *expr)
+{
Node *lhs = expr->lhs, *rhs = expr->rhs;
@@ -3285,7 +3338,8 @@ joinbinarywrap:
}
static Node *
-foldexpr(Env *env, Node *expr) {
+foldexpr(Env *env, Node *expr)
+{
Node *lhs = expr->lhs, *rhs = expr->rhs;
Type *ty = expr->type;
@@ -3520,7 +3574,8 @@ enum Highlight {
Highlight lasthighlight;
static int
-highlight(FILE *out, Highlight kind) {
+highlight(FILE *out, Highlight kind)
+{
int n = 0;
if (out != stdout)
@@ -3609,7 +3664,8 @@ static int
printexpr(FILE *out, Node *expr, int indent);
static int
-printtypetail(FILE *out, Type *type, int indent) {
+printtypetail(FILE *out, Type *type, int indent)
+{
int n = 0;
if (!type)
@@ -3666,7 +3722,8 @@ printtypetail(FILE *out, Type *type, int indent) {
}
static int
-printtype(FILE *out, Type *type, int indent) {
+printtype(FILE *out, Type *type, int indent)
+{
if (type && type->kind == TTUPLE) {
int n = 0;
@@ -3680,7 +3737,8 @@ printtype(FILE *out, Type *type, int indent) {
}
static int
-printtypesuffix(FILE *out, Type *type, int indent) {
+printtypesuffix(FILE *out, Type *type, int indent)
+{
int n = 0;
if (!type)
@@ -3705,7 +3763,8 @@ printtypesuffix(FILE *out, Type *type, int indent) {
}
static bool
-isclauseorempty(Node *expr) {
+isclauseorempty(Node *expr)
+{
Kind kind;
while (expr && (expr->kind == ASCOPE || expr->kind == ASTMT))
@@ -3720,7 +3779,8 @@ isclauseorempty(Node *expr) {
}
static int
-printclause(FILE *out, Node *expr, int indent) {
+printclause(FILE *out, Node *expr, int indent)
+{
int n = 0;
if (isclauseorempty(expr)) {
@@ -3735,7 +3795,8 @@ printclause(FILE *out, Node *expr, int indent) {
}
static int
-printstring(FILE *out, Node *string) {
+printstring(FILE *out, Node *string)
+{
const char *str = getstring(strings, string->u.key);
int len = getlength(strings, string->u.key);
int i, n = fprintf(out, "\"");
@@ -3780,7 +3841,8 @@ printstring(FILE *out, Node *string) {
}
static int
-printoperant(FILE *out, Node *expr, int opprec, bool braceequalprec, int indent) {
+printoperant(FILE *out, Node *expr, int opprec, bool braceequalprec, int indent)
+{
int prec, n = 0;
if (!expr)
@@ -3806,7 +3868,8 @@ printoperant(FILE *out, Node *expr, int opprec, bool braceequalprec, int indent)
}
static int
-printsubexpr(FILE *out, Node *expr, bool islhs, int indent) {
+printsubexpr(FILE *out, Node *expr, bool islhs, int indent)
+{
int prec, n = 0;
if (!expr)
@@ -3821,7 +3884,8 @@ printsubexpr(FILE *out, Node *expr, bool islhs, int indent) {
}
static int
-printdeclaration(FILE *out, Decl *decl, int indent) {
+printdeclaration(FILE *out, Decl *decl, int indent)
+{
int n = 0;
assert(decl);
@@ -3874,7 +3938,8 @@ printdeclaration(FILE *out, Decl *decl, int indent) {
}
static int
-printexpr(FILE *out, Node *expr, int indent) {
+printexpr(FILE *out, Node *expr, int indent)
+{
int n = 0;
if (getnumops(expr->kind) == 2) {
@@ -4143,7 +4208,8 @@ printexpr(FILE *out, Node *expr, int indent) {
/* - init source - */
static void
-initsource(Source *source, const char *filename, FILE *file) {
+initsource(Source *source, const char *filename, FILE *file)
+{
source->filein = file;
source->currloc.filename = filename;
source->tok.loc.filename = filename;
@@ -4161,7 +4227,8 @@ initsource(Source *source, const char *filename, FILE *file) {
/* - main-routine - */
int
-main(int argc, char **argv) {
+main(int argc, char **argv)
+{
Env *p;
Source *source = &testsource;