Aria

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

commit 084b3befcf98c7e9ff9642197025075f4d4b4538
parent f430fbd893a75b32519980ef7adb25a7a10cc37b
Author: m21c <ho*******@gmail.com>
Date:   Fri, 27 Jun 2025 19:19:18 +0200

worked on hlprint

Diffstat:
Mcompiler.c | 548++++++++++++++++++++++++++++++++++++++++++-------------------------------------
1 file changed, 293 insertions(+), 255 deletions(-)

diff --git a/compiler.c b/compiler.c @@ -5052,8 +5052,9 @@ enum Highlight { HLPARAMDECL = 10, HLDECL = 11, #endif - HLINFO = 12, - HLPROMPT = 13 + HLSPECIAL = 12, + HLINFO = 13, + HLPROMPT = 14 } Highlight; #define HLFUNCTIONDECL HLFUNCTION @@ -5088,6 +5089,7 @@ highlight(FILE *out, Highlight kind) lasthighlight == HLPARAMDECL || kind == HLPARAMDECL || lasthighlight == HLDECL || kind == HLDECL || #endif + lasthighlight == HLSPECIAL || lasthighlight == HLUNKNOWN || kind == HLUNKNOWN) n += fprintf(out, "\x1b[0m"); @@ -5138,6 +5140,10 @@ highlight(FILE *out, Highlight kind) break; #endif + case HLSPECIAL: + n += fprintf(out, "\x1b[3;36m"); + break; + case HLINFO: n += fprintf(out, "\x1b[33m"); break; @@ -5248,7 +5254,7 @@ printtype(FILE *out, Type *type, int indent) } static int -printtypesuffix(FILE *out, Type *type, int indent) +printtypesuffix(FILE *out, Type *type) { int n = 0; @@ -5386,7 +5392,7 @@ printoperant(FILE *out, Node *expr, int opprec, bool braceequalprec, int indent) static int printsubexpr(FILE *out, Node *expr, bool islhs, int indent) { - int prec, n = 0; + int n = 0; if (!expr) return 0; @@ -5400,6 +5406,52 @@ printsubexpr(FILE *out, Node *expr, bool islhs, int indent) } static int +printdeclmodule(FILE *out, Decl *module) +{ + int n = 0; + + if (module->module) + printdeclmodule(out, module->module); + + if (module->kind == DTYPE) { + n += highlight(out, HLTYPE); + fprintf(out, "%s", getstring(idents, module->key)); + n += highlight(out, HLDELIM); + } else { + n += highlight(out, HLDELIM); + fprintf(out, "%s", getstring(idents, module->key)); + } + n += fprintf(out, "."); + + return n; +} + +static int +printdeclname(FILE *out, Decl *decl, bool isdecl) +{ + int n = 0; + + if (decl->module) + printdeclmodule(out, decl->module); + + if (isdecl) { + n += highlight(out, + decl->kind == DFUNCTION ? HLFUNCTIONDECL : + decl->kind == DPARAM ? ( + decl->flags & MSPECIAL ? HLSPECIAL : HLPARAMDECL + ) : HLDECL); + } else { + n += highlight(out, decl->kind == DFUNCTION ? + HLFUNCTION : decl->kind == DPARAM ? ( + decl->flags & MSPECIAL ? HLSPECIAL : HLPARAM + ) : HLIDENT); + } + n += fprintf(out, "%s", getstring(idents, decl->key)); + + return n; +} + +static int printdeclaration(FILE *out, Decl *decl, int indent) { int n = 0; @@ -5413,20 +5465,8 @@ printdeclaration(FILE *out, Decl *decl, int indent) n += printtype(out, decl->type, indent); } - if (decl->typemodule) { - n += fprintf(out, " "); - n += printtype(out, decl->typemodule, indent); - n += highlight(out, HLDELIM); - n += fprintf(out, "."); - } else { - n += fprintf(out, " "); - } - - n += highlight(out, - decl->kind == DFUNCTION ? HLFUNCTIONDECL : - decl->kind == DPARAM ? HLPARAMDECL : - HLDECL); - n += fprintf(out, "%s", getstring(idents, decl->key)); + n += fprintf(out, " "); + printdeclname(out, decl, true); if (decl->kind == DFUNCTION) { Decl *param, *head = NULL; @@ -5490,8 +5530,10 @@ printexpr(FILE *out, Node *expr, int indent) getprec(expr->kind), !israssoc(expr->kind), indent); - - } else if (getnumops(expr->kind) == 1) { + goto finish; + } + + if (getnumops(expr->kind) == 1) { if (getprec(expr->kind) == PUNSUF) { printoperant(out, expr->lhs, PUNSUF, false, indent); @@ -5545,301 +5587,297 @@ printexpr(FILE *out, Node *expr, int indent) n += printoperant(out, expr->lhs, PUNARY, false, indent); } - } else { - switch (expr->kind) - { - case IDENT: - n += highlight(out, HLUNKNOWN); - n += fprintf(out, "%s?", getstring(idents, expr->u.key)); - n += highlight(out, HLNONE); - break; - - case NUMBER: - n += highlight(out, HLNUMBER); - - switch (expr->type->kind) { - case TF32: case TF64: - /* case TLDOUBLE: */ - n += fprintf(out, "%f", expr->u.d); - n += printtypesuffix(out, expr->type, indent); - break; - - case TINFER: - case TS8: case TS16: case TS32: case TS64: - 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, "%llu", expr->u.s); - n += printtypesuffix(out, expr->type, indent); - break; - - case TBOOL: - if (expr->u.u == 0) - n += fprintf(out, "false"); - else if (expr->u.u == 1) - n += fprintf(out, "true"); - else - n += fprintf(out, "0x%016llx", expr->u.u); - - break; - - case TPTR: - if (expr->u.u == 0) - n += fprintf(out, "null"); - else - n += fprintf(out, "0x%016llx", expr->u.u); - break; + goto finish; + } + + switch (expr->kind) { + case IDENT: + n += highlight(out, HLUNKNOWN); + n += fprintf(out, "%s?", getstring(idents, expr->u.key)); + n += highlight(out, HLNONE); + break; - case TVOID: - default: - n += fprintf(out, "---"); - break; + case NUMBER: + n += highlight(out, HLNUMBER); - } + switch (expr->type->kind) { + case TF32: case TF64: + /* case TLDOUBLE: */ + n += fprintf(out, "%f", expr->u.d); + n += printtypesuffix(out, expr->type); + break; + case TINFER: + case TS8: case TS16: case TS32: case TS64: + n += fprintf(out, "%lli", expr->u.s); + n += printtypesuffix(out, expr->type); break; - case STRING: - n += highlight(out, HLSTRING); - n += printstring(out, expr); + case TUINFER: + case TU8: case TU16: case TU32: case TU64: + n += fprintf(out, "%llu", expr->u.s); + n += printtypesuffix(out, expr->type); break; - case ADECLREF: - n += highlight(out, expr->u.declref->kind == DFUNCTION ? - HLFUNCTION : expr->u.declref->kind == DPARAM ? - HLPARAM : HLIDENT); + case TBOOL: + if (expr->u.u == 0) + n += fprintf(out, "false"); + else if (expr->u.u == 1) + n += fprintf(out, "true"); + else + n += fprintf(out, "0x%016llx", expr->u.u); - n += fprintf(out, "%s", - getstring(idents, expr->u.declref->key)); break; - case ADECL: - n += printdeclaration(out, expr->u.declref, indent); + case TPTR: + if (expr->u.u == 0) + n += fprintf(out, "null"); + else + n += fprintf(out, "0x%016llx", expr->u.u); break; - case ACOMMA: - n += printsubexpr(out, expr->lhs, true, indent); - n += highlight(out, HLDELIM); - n += printf(", "); - n += printsubexpr(out, expr->rhs, false, indent); + case TVOID: + default: + n += fprintf(out, "---"); break; - case KSIZEOF: - case KALIGNOF: - case KLENGTHOF: - 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); - n += fprintf(out, ")"); - break; + break; - case KBITCAST: - n += highlight(out, HLKEYWORD); - n += fprintf(out, "bitcast"); - n += highlight(out, HLDELIM); - n += fprintf(out, "("); + case STRING: + n += highlight(out, HLSTRING); + n += printstring(out, expr); + break; - n += highlight(out, HLTYPE); - n += printtype(out, expr->rhs->type, indent); - n += highlight(out, HLDELIM); + case ADECLREF: + printdeclname(out, expr->u.declref, false); + break; + + case ADECL: + n += printdeclaration(out, expr->u.declref, indent); + break; - n += fprintf(out, ") ("); - n += printexpr(out, expr->lhs, indent); - n += highlight(out, HLDELIM); - n += fprintf(out, ")"); - break; + case ACOMMA: + n += printsubexpr(out, expr->lhs, true, indent); + n += highlight(out, HLDELIM); + n += printf(", "); + n += printsubexpr(out, expr->rhs, false, indent); + break; - case KRETURN: - n += highlight(out, HLKEYWORD); - if (expr->rhs) { - n += fprintf(out, "return "); - n += printexpr(out, expr->rhs, indent); - } else { - n += fprintf(out, "return"); - } - break; + case KSIZEOF: + case KALIGNOF: + case KLENGTHOF: + n += highlight(out, HLKEYWORD); + n += fprintf(out, "%s", nodestrings[expr->kind]); + n += highlight(out, HLDELIM); - case KBREAK: - n += highlight(out, HLKEYWORD); - n += fprintf(out, "break"); - break; + n += fprintf(out, "("); + n += printexpr(out, expr->lhs, indent); + n += highlight(out, HLDELIM); + n += fprintf(out, ")"); + break; - case KCONTINUE: - n += highlight(out, HLKEYWORD); - n += fprintf(out, "continue"); - break; - case KWHILE: - n += highlight(out, HLKEYWORD); - n += fprintf(out, "while "); - goto joinifelse; + case KBITCAST: + n += highlight(out, HLKEYWORD); + n += fprintf(out, "bitcast"); + n += highlight(out, HLDELIM); + n += fprintf(out, "("); - case KIF: - n += highlight(out, HLKEYWORD); - n += fprintf(out, "if "); - /* FALLTHROUGH */ - joinifelse: - n += printexpr(out, expr->u.payload, indent); - n += printclause(out, expr->lhs, indent); + n += highlight(out, HLTYPE); + n += printtype(out, expr->rhs->type, indent); + n += highlight(out, HLDELIM); - if (expr->rhs) { - int i; + n += fprintf(out, ") ("); + n += printexpr(out, expr->lhs, indent); + n += highlight(out, HLDELIM); + n += fprintf(out, ")"); + break; - n += fprintf(out, "\n"); + case KRETURN: + n += highlight(out, HLKEYWORD); + if (expr->rhs) { + n += fprintf(out, "return "); + n += printexpr(out, expr->rhs, indent); + } else { + n += fprintf(out, "return"); + } + break; - for (i = 0; i < indent; ++i) - n += fprintf(out, "\t"); + case KBREAK: + n += highlight(out, HLKEYWORD); + n += fprintf(out, "break"); + break; - n += highlight(out, HLKEYWORD); - n += fprintf(out, "else"); - n += printclause(out, expr->rhs, indent); - } - break; + case KCONTINUE: + n += highlight(out, HLKEYWORD); + n += fprintf(out, "continue"); + break; + case KWHILE: + n += highlight(out, HLKEYWORD); + n += fprintf(out, "while "); + goto joinifbody; - case KLOOP: - case ALOOPUNTIL: - n += highlight(out, HLKEYWORD); - n += fprintf(out, "loop"); - n += printclause(out, expr->lhs, indent); - if (expr->kind == KLOOP) - break; + case KIF: + n += highlight(out, HLKEYWORD); + n += fprintf(out, "if "); + /* FALLTHROUGH */ + joinifbody: + n += printexpr(out, expr->u.payload, indent); + n += printclause(out, expr->lhs, indent); - do { - int i; - n += fprintf(out, "\n"); + if (expr->rhs) { + int i; - for (i = 0; i < indent; ++i) - n += fprintf(out, "\t"); + n += fprintf(out, "\n"); - } while (0); + for (i = 0; i < indent; ++i) + n += fprintf(out, "\t"); n += highlight(out, HLKEYWORD); - n += fprintf(out, "until "); - n += printexpr(out, expr->u.payload, indent); + n += fprintf(out, "else"); + n += printclause(out, expr->rhs, indent); + } + break; - if (expr->rhs) { - n += highlight(out, HLKEYWORD); - n += fprintf(out, " else"); - n += printclause(out, expr->rhs, indent); - } + case KLOOP: + case ALOOPUNTIL: + n += highlight(out, HLKEYWORD); + n += fprintf(out, "loop"); + n += printclause(out, expr->lhs, indent); + if (expr->kind == KLOOP) break; + do { + int i; + n += fprintf(out, "\n"); - case KDO: - n += highlight(out, HLKEYWORD); - n += fprintf(out, "do"); - n += printclause(out, expr->lhs, indent); - break; + for (i = 0; i < indent; ++i) + n += fprintf(out, "\t"); + + } while (0); + + n += highlight(out, HLKEYWORD); + n += fprintf(out, "until "); + n += printexpr(out, expr->u.payload, indent); - case KUNION: - case KSTRUCT: + if (expr->rhs) { n += highlight(out, HLKEYWORD); + n += fprintf(out, " else"); + n += printclause(out, expr->rhs, indent); + } + break; - 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)); - } + case KDO: + n += highlight(out, HLKEYWORD); + n += fprintf(out, "do"); + n += printclause(out, expr->lhs, indent); + break; - if (expr->rhs) - n += printclause(out, expr->rhs, indent); + case KUNION: + case KSTRUCT: + n += highlight(out, HLKEYWORD); - break; + 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)); + } - case ASTMT: - advancestmt: - do { - int i; + if (expr->rhs) + n += printclause(out, expr->rhs, indent); - for (i = 0; i < indent; ++i) - n += fprintf(out, "\t"); - } while (0); + break; - n += printexpr(out, expr->lhs, indent); + case ASTMT: + advancestmt: + do { + int i; - if (expr->rhs) { - assert(expr->rhs->kind == ASTMT); - n += fprintf(out, "\n"); - expr = expr->rhs; - goto advancestmt; - } + for (i = 0; i < indent; ++i) + n += fprintf(out, "\t"); + } while (0); - break; + n += printexpr(out, expr->lhs, indent); - case ASCOPE: - /* TODO(m21c): improve this piece of code */ - if (expr->lhs && - expr->lhs->kind == ASTMT && - expr->u.env && - expr->u.env->kind != SFUNCTION && - expr->u.env->kind != SSTRUCT) - { - Node *stmt = expr->lhs; + if (expr->rhs) { + assert(expr->rhs->kind == ASTMT); + n += fprintf(out, "\n"); + expr = expr->rhs; + goto advancestmt; + } - if (!stmt->rhs && isclauseorempty(stmt)) { - n += printexpr(out, stmt->lhs, indent); - break; - } - } - n += printexpr(out, expr->lhs, indent); - n += fprintf(out, "\n"); /* blank line */ - break; + break; - case ACONV: - n += highlight(out, HLNUMBER); - n += fprintf(out, "conv("); + case ASCOPE: + /* TODO(m21c): improve this piece of code */ + if (expr->lhs && + expr->lhs->kind == ASTMT && + expr->u.env && + expr->u.env->kind != SFUNCTION && + expr->u.env->kind != SSTRUCT) + { + Node *stmt = expr->lhs; - n += highlight(out, HLTYPE); - n += printtype(out, expr->type, indent); - n += highlight(out, HLDELIM); + if (!stmt->rhs && isclauseorempty(stmt)) { + n += printexpr(out, stmt->lhs, indent); + break; + } + } + n += printexpr(out, expr->lhs, indent); + n += fprintf(out, "\n"); /* blank line */ + break; - n += fprintf(out, ") "); - n += printoperant(out, expr->lhs, PUNARY, false, indent); - break; + case ACONV: + n += highlight(out, HLNUMBER); + n += fprintf(out, "conv("); - case AADDR: - n += highlight(out, HLDELIM); - n += fputs("&{", out); - n += printoperant(out, expr->lhs, PUNARY, false, indent); - n += highlight(out, HLDELIM); - n += fputs("}", out); - break; + n += highlight(out, HLTYPE); + n += printtype(out, expr->type, indent); + n += highlight(out, HLDELIM); - case ADEREF: - n += highlight(out, HLDELIM); - n += fputs("*{", out); - n += printoperant(out, expr->lhs, PUNARY, false, indent); - n += highlight(out, HLDELIM); - n += fputs("}", out); - break; + n += fprintf(out, ") "); + n += printoperant(out, expr->lhs, PUNARY, false, indent); + break; - default: - n += highlight(out, HLINFO); - n += fprintf(out, "node(%u)", expr->kind); + case AADDR: + n += highlight(out, HLDELIM); + n += fputs("&{", out); + n += printoperant(out, expr->lhs, PUNARY, false, indent); + n += highlight(out, HLDELIM); + n += fputs("}", out); + break; - if (expr->lhs) { - n += fprintf(out, " -> "); - n += printsubexpr(out, expr->lhs, true, indent); - } - if (expr->rhs) { - n += highlight(out, HLINFO); - n += fprintf(out, " => "); - n += printsubexpr(out, expr->rhs, false, indent); - } + case ADEREF: + n += highlight(out, HLDELIM); + n += fputs("*{", out); + n += printoperant(out, expr->lhs, PUNARY, false, indent); + n += highlight(out, HLDELIM); + n += fputs("}", out); + break; - break; + default: + n += highlight(out, HLINFO); + n += fprintf(out, "node(%u)", expr->kind); + + if (expr->lhs) { + n += fprintf(out, " -> "); + n += printsubexpr(out, expr->lhs, true, indent); } + if (expr->rhs) { + n += highlight(out, HLINFO); + n += fprintf(out, " => "); + n += printsubexpr(out, expr->rhs, false, indent); + } + + break; } +finish: #if 0 if (expr->kind == ASTMT && expr->next) { n += fprintf(out, "\n");