Aria

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

commit 0521d59eb791de93d2d95650d229f28d324c15e7
parent 1fb33ec5cf61759e18ec0f3629fabbe6df8652d8
Author: m21c  <ho*******@gmail.com>
Date:   Sun, 11 Jul 2021 01:28:38 +0200

worked on output highlighting + improvised REPL

Diffstat:
Maria.c | 132++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------
1 file changed, 117 insertions(+), 15 deletions(-)

diff --git a/aria.c b/aria.c @@ -168,9 +168,23 @@ char line[4096]; int currline, lastline; long linepos; +bool handlereplprompt; + +void +tryprompt(FILE *in, const char ch) { + if (handlereplprompt) { + fprintf(stdout, "\e[35m%c \e[0m", ch); + } else if (in == stdin) { + handlereplprompt = true; + } +} + bool mygetline(FILE *in) { - int i, l, c = getc(in); + int i, l, c; + + tryprompt(in, '.'); + c = getc(in); linepos = ftell(in); @@ -179,6 +193,7 @@ advance: i = 0, l = 0; while (c == '\r' || c == '\n') { + tryprompt(in, '.'); l = c, c = getc(in); if (l == '\r' && c == '\n') @@ -196,10 +211,16 @@ advance: if (c == '\\') { int x = getc(in); if (x == '\n') { + tryprompt(in, '\\'); c = getc(in); + ++currline; } else if (x == '\r') { - int y = getc(in); + int y; + + tryprompt(in, '\\'); + y = getc(in); + c = (y == '\n') ? getc(in) : y; ++currline; } else if (x == EOF) { @@ -211,12 +232,16 @@ advance: } if (c == '\r') { - int x = getc(in); + int x; + + tryprompt(in, '.'); + x = getc(in); if (x != '\n') ungetc(x, in); } - if (c != EOF && i == 0) goto advance; + if (c != EOF && i == 0) + goto advance; line[i] = 0; return c != EOF || i; @@ -2188,17 +2213,17 @@ autoref(Type *ty, Node *node) assert(node != NULL); - printf("numderefs[1]: %i\n", numderefs); + /* printf("numderefs[1]: %i\n", numderefs); */ for (t = node->type; t && t->kind == TPTR; t = t->target) ++numderefs; - printf("numderefs[2]: %i\n", numderefs); + /* printf("numderefs[2]: %i\n", numderefs); */ for (t = ty; t && t->kind == TPTR; t = t->target) --numderefs; - printf("numderefs[3]: %i\n", numderefs); + /* printf("numderefs[3]: %i\n", numderefs); */ #if 0 if (numderefs) @@ -2679,9 +2704,19 @@ typedef enum Highlight { HLTYPE = 6, HLFUNCTION = 7, HLPARAM = 8, - HLINFO = 9 + #if 0 + HLFUNCTIONDECL = 9, + HLPARAMDECL = 10, + HLDECL = 11, + #endif + HLINFO = 12, + HLPROMPT = 13 } Highlight; +#define HLFUNCTIONDECL HLFUNCTION +#define HLPARAMDECL HLPARAM +#define HLDECL HLIDENT + #define HLOP HLDELIM #define HLCHAR HLSTRING #define HLIDENT HLNONE @@ -2704,6 +2739,11 @@ highlight(FILE *out, Highlight kind) { if (lasthighlight == HLDELIM || kind == HLDELIM || lasthighlight == HLFUNCTION || kind == HLFUNCTION || lasthighlight == HLPARAM || kind == HLPARAM || + #if 0 + lasthighlight == HLFUNCTIONDECL || kind == HLFUNCTIONDECL || + lasthighlight == HLPARAMDECL || kind == HLPARAMDECL || + lasthighlight == HLDECL || kind == HLDECL || + #endif lasthighlight == HLUNKNOWN || kind == HLUNKNOWN) n += fprintf(out, "\e[0m"); @@ -2732,9 +2772,23 @@ highlight(FILE *out, Highlight kind) { case HLPARAM: n += fprintf(out, "\e[3m"); break; + #if 0 + case HLFUNCTIONDECL: + n += fprintf(out, "\e[1;4;3m"); + break; + case HLPARAMDECL: + n += fprintf(out, "\e[4;3m"); + break; + case HLDECL: + n += fprintf(out, "\e[4m"); + break; + #endif case HLINFO: n += fprintf(out, "\e[33m"); break; + case HLPROMPT: + n += fprintf(out, "\e[35m"); + break; } lasthighlight = kind; @@ -2744,7 +2798,8 @@ highlight(FILE *out, Highlight kind) { int printexpr(FILE *out, Node *expr, int indent); -int printtype(FILE *out, Type *type, int indent) { +int +printtype(FILE *out, Type *type, int indent) { int n = 0; if (!type) @@ -2764,6 +2819,29 @@ int printtype(FILE *out, Type *type, int indent) { typecase(TUNDEFINED, "<undefined-type>"); typecase(TPTR, "*"); typecase(TVOID, "void" ); typecase(TBOOL, "bool" ); + typecase(TINFER, "infer"); typecase(TUINFER, "uinfer"); + typecase(TS8, "char" ); typecase(TU8, "uchar" ); + typecase(TS16, "s16" ); typecase(TU16, "u16" ); + typecase(TS32, "int" ); typecase(TU32, "uint" ); + typecase(TS64, "s64" ); typecase(TU64, "u64" ); + typecase(TF32, "float"); typecase(TF64, "double"); + #undef typecase + default:; + } + + return n; +} + +int +printtypesuffix(FILE *out, Type *type, int indent) { + int n = 0; + + if (!type) + return 0; + + switch (type->kind) { + #define typecase(type, str) \ + case type: n += fprintf(out, str); break typecase(TINFER, "i" ); typecase(TUINFER, "u" ); typecase(TS8, "s8" ); typecase(TU8, "u8" ); typecase(TS16, "s16" ); typecase(TU16, "u16" ); @@ -2884,9 +2962,9 @@ printdeclaration(FILE *out, Decl *decl, int indent) { n += printtype(out, decl->type, indent); n += highlight(out, - decl->kind == DFUNCTION ? HLFUNCTION : - decl->kind == DPARAM ? HLPARAM : - HLIDENT); + decl->kind == DFUNCTION ? HLFUNCTIONDECL : + decl->kind == DPARAM ? HLPARAMDECL : + HLDECL); n += fprintf(out, " %s", getstring(idents, decl->key)); if (decl->kind == DFUNCTION) { @@ -2961,6 +3039,7 @@ printexpr(FILE *out, Node *expr, int indent) { n += printexpr(out, c->rhs, indent); break; default: + n += highlight(out, HLDELIM); n += fprintf(out, "%s", nodestrings[c->kind]); } } else { @@ -2974,6 +3053,7 @@ printexpr(FILE *out, Node *expr, int indent) { putc(')', out), ++n; break; default: + n += highlight(out, HLDELIM); n += fprintf(out, "%s", nodestrings[c->kind]); if (getprec(c->lhs->kind) == PUNARY && c->kind != OLPTR) @@ -3001,7 +3081,7 @@ printexpr(FILE *out, Node *expr, int indent) { n += fprintf(out, "%li", c->u.s); else n += fprintf(out, "%lu", c->u.u); - n += printtype(out, c->type, indent); + n += printtypesuffix(out, c->type, indent); break; case 'S': n += highlight(out, HLSTRING); @@ -3114,7 +3194,11 @@ main(int argc, char **argv) { } else { filein = stdin; filename = "<stdin>"; + + highlight(stdout, HLPROMPT); printf("> "); + highlight(stdout, HLNONE); + handlereplprompt = false; } gettok(false); @@ -3133,8 +3217,16 @@ main(int argc, char **argv) { */ if (ast->kind != ADECL || !ast->u.payload || ast->u.payload->kind != ASCOPE) ast = foldexpr(typecheck(ast)); + + if (filein == stdin) { + highlight(stdout, HLINFO); + fputs("= ", stdout); + highlight(stdout, HLNONE); + } + printexpr(stdout, ast, 0); highlight(stdout, HLNONE); + if (filein == stdin) { highlight(stdout, HLINFO); fputs(" : ", stdout); @@ -3145,8 +3237,12 @@ main(int argc, char **argv) { if (tok.kind == '\n') { - if (filein == stdin) + if (filein == stdin) { + highlight(stdout, HLPROMPT); printf("> "); + highlight(stdout, HLNONE); + handlereplprompt = false; + } gettok(false); } else if (tok.kind == ';') { gettok(false); @@ -3156,8 +3252,14 @@ main(int argc, char **argv) { error("expected new line"); while (tok.kind != ';' && tok.kind != '\n' && tok.kind != 0) gettok(false); - if (filein == stdin) + + if (filein == stdin) { + highlight(stdout, HLPROMPT); printf("> "); + highlight(stdout, HLNONE); + handlereplprompt = false; + } + if (tok.kind != 0) gettok(false); }