nooc

nooc programming language compiler
git clone git://git.nihaljere.xyz/nooc
Log | Files | Refs | LICENSE

commit e69da648bad3180ae7cf05fa079f27e5534c673d
parent 95f3020c50946b9db46e80891cb0be194459d8fc
Author: Nihal Jere <nihal@nihaljere.xyz>
Date:   Sun, 26 Dec 2021 20:47:19 -0600

move debugging facilities into util

Diffstat:
Mmain.c | 113++-----------------------------------------------------------------------------
Mutil.c | 112+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mutil.h | 4++++
3 files changed, 118 insertions(+), 111 deletions(-)

diff --git a/main.c b/main.c @@ -20,30 +20,11 @@ #include "map.h" #include "blockstack.h" +extern const char *const tokenstr[]; + extern struct block *blockstack[BLOCKSTACKSIZE]; extern size_t blocki; -const char *const tokenstr[] = { - [TOK_NONE] = "TOK_NONE", - [TOK_NAME] = "TOK_NAME", - [TOK_LPAREN] = "TOK_LPAREN", - [TOK_RPAREN] = "TOK_RPAREN", - [TOK_LCURLY] = "TOK_LCURLY", - [TOK_RCURLY] = "TOK_RCURLY", - [TOK_PLUS] = "TOK_PLUS", - [TOK_MINUS] = "TOK_MINUS", - [TOK_GREATER] = "TOK_GREATER", - [TOK_COMMA] = "TOK_COMMA", - [TOK_EQUAL] = "TOK_EQUAL", - [TOK_NUM] = "TOK_NUM", - [TOK_STRING] = "TOK_STRING", - [TOK_LET] = "TOK_LET", - [TOK_IF] = "TOK_IF", - [TOK_ELSE] = "TOK_ELSE", - [TOK_LOOP] = "TOK_LOOP", - [TOK_RETURN] = "TOK_RETURN", -}; - struct map *typesmap; struct assgns assgns; struct exprs exprs; @@ -276,96 +257,6 @@ place_move(char *buf, struct place *dest, struct place *src) return total; } -char *exprkind_str(enum exprkind kind) -{ - switch (kind) { - case EXPR_LIT: - return "EXPR_LIT"; - case EXPR_BINARY: - return "EXPR_BINARY"; - case EXPR_IDENT: - return "EXPR_IDENT"; - case EXPR_FCALL: - return "EXPR_FCALL"; - case EXPR_COND: - return "EXPR_COND"; - default: - die("invalid exprkind"); - } - - return NULL; -} - -struct exprs exprs; - -void -dumpval(struct expr *e) -{ - switch (e->class) { - case C_INT: - fprintf(stderr, "%ld", e->d.v.v.i64); - break; - case C_STR: - fprintf(stderr, "\"%.*s\"", (int)e->d.v.v.s.len, e->d.v.v.s.data); - break; - case C_PROC: - fprintf(stderr, "proc with %lu params", e->d.proc.in.len); - break; - } -} - -void -dumpbinop(enum binop op) -{ - switch (op) { - case OP_PLUS: - fprintf(stderr, "OP_PLUS"); - break; - case OP_MINUS: - fprintf(stderr, "OP_MINUS"); - break; - case OP_GREATER: - fprintf(stderr, "OP_GREATER"); - break; - case OP_EQUAL: - fprintf(stderr, "OP_EQUAL"); - break; - default: - die("invalid binop"); - } -} - -void -dumpexpr(int indent, struct expr *expr) -{ - for (int i = 0; i < indent; i++) - fputc(' ', stderr); - fprintf(stderr, "%s: ", exprkind_str(expr->kind)); - switch (expr->kind) { - case EXPR_IDENT: - fprintf(stderr, "%.*s\n", (int)expr->d.s.len, expr->d.s.data); - break; - case EXPR_LIT: - dumpval(expr); - fputc('\n', stderr); - break; - case EXPR_BINARY: - dumpbinop(expr->d.op); - fputc('\n', stderr); - dumpexpr(indent + 8, &exprs.data[expr->left]); - dumpexpr(indent + 8, &exprs.data[expr->right]); - break; - case EXPR_COND: - dumpexpr(indent + 8, &exprs.data[expr->d.cond.cond]); - break; - case EXPR_FCALL: - fprintf(stderr, "%.*s\n", (int)expr->d.call.name.len, expr->d.call.name.data); - break; - default: - die("dumpexpr: bad expression"); - } -} - void typecheck(struct block *block) { diff --git a/util.c b/util.c @@ -10,6 +10,118 @@ #include "util.h" extern char *infile; +extern struct exprs exprs; + +const char *const tokenstr[] = { + [TOK_NONE] = "TOK_NONE", + [TOK_NAME] = "TOK_NAME", + [TOK_LPAREN] = "TOK_LPAREN", + [TOK_RPAREN] = "TOK_RPAREN", + [TOK_LCURLY] = "TOK_LCURLY", + [TOK_RCURLY] = "TOK_RCURLY", + [TOK_PLUS] = "TOK_PLUS", + [TOK_MINUS] = "TOK_MINUS", + [TOK_GREATER] = "TOK_GREATER", + [TOK_COMMA] = "TOK_COMMA", + [TOK_EQUAL] = "TOK_EQUAL", + [TOK_NUM] = "TOK_NUM", + [TOK_STRING] = "TOK_STRING", + [TOK_LET] = "TOK_LET", + [TOK_IF] = "TOK_IF", + [TOK_ELSE] = "TOK_ELSE", + [TOK_LOOP] = "TOK_LOOP", + [TOK_RETURN] = "TOK_RETURN", +}; + +char * +exprkind_str(enum exprkind kind) +{ + switch (kind) { + case EXPR_LIT: + return "EXPR_LIT"; + case EXPR_BINARY: + return "EXPR_BINARY"; + case EXPR_IDENT: + return "EXPR_IDENT"; + case EXPR_FCALL: + return "EXPR_FCALL"; + case EXPR_COND: + return "EXPR_COND"; + default: + die("invalid exprkind"); + } + + return NULL; +} + +void +dumpval(struct expr *e) +{ + switch (e->class) { + case C_INT: + fprintf(stderr, "%ld", e->d.v.v.i64); + break; + case C_STR: + fprintf(stderr, "\"%.*s\"", (int)e->d.v.v.s.len, e->d.v.v.s.data); + break; + case C_PROC: + fprintf(stderr, "proc with %lu params", e->d.proc.in.len); + break; + } +} + +void +dumpbinop(enum binop op) +{ + switch (op) { + case OP_PLUS: + fprintf(stderr, "OP_PLUS"); + break; + case OP_MINUS: + fprintf(stderr, "OP_MINUS"); + break; + case OP_GREATER: + fprintf(stderr, "OP_GREATER"); + break; + case OP_EQUAL: + fprintf(stderr, "OP_EQUAL"); + break; + default: + die("invalid binop"); + } +} + +void +dumpexpr(int indent, struct expr *expr) +{ + for (int i = 0; i < indent; i++) + fputc(' ', stderr); + fprintf(stderr, "%s: ", exprkind_str(expr->kind)); + switch (expr->kind) { + case EXPR_IDENT: + fprintf(stderr, "%.*s\n", (int)expr->d.s.len, expr->d.s.data); + break; + case EXPR_LIT: + dumpval(expr); + fputc('\n', stderr); + break; + case EXPR_BINARY: + dumpbinop(expr->d.op); + fputc('\n', stderr); + dumpexpr(indent + 8, &exprs.data[expr->left]); + dumpexpr(indent + 8, &exprs.data[expr->right]); + break; + case EXPR_COND: + dumpexpr(indent + 8, &exprs.data[expr->d.cond.cond]); + break; + case EXPR_FCALL: + fprintf(stderr, "%.*s\n", (int)expr->d.call.name.len, expr->d.call.name.data); + break; + default: + die("dumpexpr: bad expression"); + } +} + int slice_cmp(struct slice *s1, struct slice *s2) diff --git a/util.h b/util.h @@ -1,3 +1,7 @@ +char *exprkind_str(enum exprkind kind); +void dumpval(struct expr *e); +void dumpbinop(enum binop op); +void dumpexpr(int indent, struct expr *expr); int slice_cmp(struct slice *s1, struct slice *s2); int slice_cmplit(struct slice *s1, char *s2); void error(size_t line, size_t col, const char *error, ...);