Aria

A low-level systems programming language
git clone git://git.m21c.me/Aria.git
Log | Files | Refs | LICENSE

commit aebdaed7fd4a6ee42805f01a347373319e57eefe
parent 00c4195d2cfaff27d6fbad0a368d688595d07063
Author: m21c <ho*******@gmail.com>
Date:   Mon, 29 Mar 2021 22:56:42 +0200

small coding-style fixes + implemented setheadenv

Diffstat:
Maria.c | 112+++++++++++++++++++++++++++++++++++++------------------------------------------
1 file changed, 52 insertions(+), 60 deletions(-)

diff --git a/aria.c b/aria.c @@ -9,8 +9,7 @@ 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; @@ -38,8 +37,7 @@ 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; @@ -488,8 +486,7 @@ StringMap idents; StringMap strings; void -initstrmap(StringMap *map) -{ +initstrmap(StringMap *map) { map->keys = calloc(32, sizeof(int)); map->keyscap = 32; assert(map->keys); @@ -501,8 +498,7 @@ initstrmap(StringMap *map) } void -disposestrmap(StringMap *map) -{ +disposestrmap(StringMap *map) { int i; for (i = map->valslen - 1; i >= 0; --i) { free((char *) map->vals[i].str); @@ -513,8 +509,7 @@ 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; @@ -543,8 +538,7 @@ redo: int auxthen; 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; @@ -1120,11 +1114,10 @@ int decltop; Env envbuf[4096]; int envtop; -Env *currenv; +Env *headenv, *currenv; Decl * -finddeclinenv(int key, Env *env) -{ +finddeclinenv(int key, Env *env) { const int cacheindex = (key >> 3) & 0x3f; const int cachebit = 1 << (key & 0x03); @@ -1142,8 +1135,7 @@ finddeclinenv(int key, Env *env) } Decl * -makedecl(int key, DeclKind kind) -{ +makedecl(int key, DeclKind kind) { const int cacheindex = (key >> 3) & 0x3f; const int cachebit = 1 << (key & 0x03); @@ -1178,8 +1170,7 @@ makedecl(int key, DeclKind kind) } Decl * -finddeclaration(int key) -{ +finddeclaration(int key) { const int cacheindex = (key >> 3) & 0x3f; const int cachebit = 1 << (key & 0x03); @@ -1201,21 +1192,42 @@ finddeclaration(int key) } Env * -pushenv(EnvKind kind) -{ +setheadenv(EnvKind kind) { + /* NOTE(m21c): this might only be useful for parameter=>function env translation */ Env *env = envbuf + envtop++; env->kind = kind; env->below = currenv; - currenv = env; + assert(headenv == NULL); + currenv = headenv = env; return env; } Env * -popenv(void) -{ +pushenv(EnvKind kind) { + if (headenv) { + headenv->kind = kind; + + assert(headenv == currenv); + headenv = NULL; + + return currenv; + } else { + Env *env = envbuf + envtop++; + + env->kind = kind; + env->below = currenv; + + currenv = env; + + return env; + } +} + +Env * +popenv(void) { Env *env = currenv; if (currenv) @@ -1225,8 +1237,7 @@ popenv(void) } Env * -getfuncenv(void) -{ +getfuncenv(void) { Env *env; for (env = currenv; env; env = env->below) { @@ -1243,8 +1254,7 @@ Node nodebuf[4096]; int nodetop; Node * -makenode(Node *lhs) -{ +makenode(Node *lhs) { Node *node = nodebuf + nodetop++; *node = tok; node->lhs = lhs; @@ -1396,8 +1406,7 @@ printast(Node *node, int indent) { /* - parser - */ bool -expect(int kind, bool nexthaslhs, const char *fmt, ...) -{ +expect(int kind, bool nexthaslhs, const char *fmt, ...) { va_list ap; if (tok.kind != kind) { @@ -1543,7 +1552,7 @@ Node * exprlist(bool isparam, Node *paramtype); Node * -stmtlist(int indent); +stmtlist(int indent, EnvKind kind); Node * declaration(Node *typenode) { @@ -1592,10 +1601,8 @@ declaration(Node *typenode) { if (tok.kind != 'O' || tok.u.id != OASS) { Node *stmts; - pushenv(SFUNCTION); - stmts = stmtlist(lastindent); + stmts = stmtlist(lastindent, SFUNCTION); result->u.decl.init = stmts; - popenv(); } } @@ -1609,13 +1616,6 @@ declaration(Node *typenode) { return result; } -Node * -tailof(Node *head) { - while (head->next) - head = head->next; - return head; -} - bool isatom(void) { switch (tok.kind) { @@ -1641,11 +1641,13 @@ isatom(void) { } Node * -stmtlist(int indent) { +stmtlist(int indent, EnvKind envkind) { Node *result = NULL, *lhs = NULL; int needindent = nextindent(indent); /* printf("needident: %d, currindent: %d, lastindent: %d\n", needindent, currindent, lastindent); */ + pushenv(envkind); + for (;;) { Node *stmt; @@ -1688,6 +1690,8 @@ stmtlist(int indent) { } } + popenv(); + return result; } @@ -1763,9 +1767,7 @@ atom(int flags) { #else gettok(false); if (tok.kind == '\n') { - pushenv(SSCOPE); - lhs = stmtlist(lastindent), lastis = savedis; - popenv(); + lhs = stmtlist(lastindent, SSCOPE), lastis = savedis; } else { lhs = exprlist(false, NULL), lastis = savedis; if (lhs->kind == 'T') { @@ -1861,9 +1863,7 @@ atom(int flags) { gettok(false); lhs->kind = 'A'; lhs->u.id = ADO; - pushenv(SSCOPE); - lhs->lhs = stmtlist(indent); - popenv(); + lhs->lhs = stmtlist(indent, SSCOPE); break; case KLOOP: indent = lastindent; @@ -1871,9 +1871,7 @@ atom(int flags) { gettok(false); lhs->kind = 'A'; lhs->u.id = ALOOP; - pushenv(SSCOPE); - lhs->lhs = stmtlist(indent); - popenv(); + lhs->lhs = stmtlist(indent, SSCOPE); if (tok.kind == 'K' && tok.u.id == KUNTIL && lastindent >= indent) @@ -1893,9 +1891,7 @@ atom(int flags) { lhs->kind = 'A'; lhs->u.cond.id = AWHILE; lhs->u.cond.cond = expr(POR); - pushenv(SSCOPE); - lhs->lhs = stmtlist(indent); - popenv(); + lhs->lhs = stmtlist(indent, SSCOPE); goto joinelse; case KIF: indent = lastindent; @@ -1907,17 +1903,13 @@ atom(int flags) { skipnewline(); if (tok.kind == 'I' && tok.u.id == auxthen) gettok(false); - pushenv(SSCOPE); - lhs->lhs = stmtlist(indent); - popenv(); + lhs->lhs = stmtlist(indent, SSCOPE); joinelse: if (tok.kind == 'K' && tok.u.id == KELSE && lastindent >= indent) { gettok(false); - pushenv(SSCOPE); - lhs->rhs = stmtlist(indent); - popenv(); + lhs->rhs = stmtlist(indent, SSCOPE); } break; default: