From 0ab980734959f11edb616dbcca3463563c6f1214 Mon Sep 17 00:00:00 2001 From: icst Date: Fri, 21 Jun 2024 20:03:59 -0400 Subject: [PATCH] make error messages a little less idoitic --- lapis.c | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/lapis.c b/lapis.c index e3f174e..8fec0c5 100644 --- a/lapis.c +++ b/lapis.c @@ -4,6 +4,7 @@ #include #include #include +#include struct lapis_node_s; @@ -363,7 +364,7 @@ lapis_node_t *lapis_parse_expr(size_t *nnodes, FILE *f) { switch (prod) { default: - fputs("ERROR: unknown priority for operator '", stderr); + fprintf(stderr, "ERROR[%d]: unknown priority for operator '", __LINE__); fwrite(tokens[n].str, 1, tokens[n].len, stderr); fputs("'\n", stderr); goto ERROR; @@ -530,14 +531,23 @@ lapis_node_t *lapis_parse_expr(size_t *nnodes, FILE *f) { } if ( n == max_prior_idx || tokens[n].type == LAPIS_TTYPE_NONE ) { - printf("ERROR: bad logic line #%d\n", __LINE__); + + fprintf(stderr, "ERROR[%d]: Found no valid LHS operand for operator: ", __LINE__); + fwrite(tokens[max_prior_idx].str, 1, tokens[max_prior_idx].len, stderr); + fputc('\n', stderr); goto ERROR; } if ( tokens[n].type == LAPIS_TTYPE_OPER ) { if ( oper_priorities[n] >= -1 ) { - printf("ERROR: bad logic line #%d\n", __LINE__); + + fprintf(stderr, "ERROR[%d]: Attempted to take LHS as operand before evaluation!", __LINE__); + fputs(" lhs=", stderr); + fwrite(tokens[n].str, 1, tokens[n].len, stderr); + fputs(" operator=", stderr); + fwrite(tokens[max_prior_idx].str, 1, tokens[max_prior_idx].len, stderr); + fputc('\n', stderr); goto ERROR; } @@ -548,10 +558,7 @@ lapis_node_t *lapis_parse_expr(size_t *nnodes, FILE *f) { // later lower priority dependent operators can find its node. int64_t node_idx = -(oper_priorities[n] + 2); - if ( node_idx < 0 ) { - printf("ERROR: bad logic line #%d\n", __LINE__); - goto ERROR; - } + assert( node_idx > 0 ); node.lhs_idx = node_idx; @@ -583,14 +590,23 @@ lapis_node_t *lapis_parse_expr(size_t *nnodes, FILE *f) { } if ( n == max_prior_idx || tokens[n].type == LAPIS_TTYPE_NONE ) { - printf("ERROR: bad logic line #%d\n", __LINE__); + + fprintf(stderr, "ERROR[%d]: Found no valid RHS operand for operator: ", __LINE__); + fwrite(tokens[max_prior_idx].str, 1, tokens[max_prior_idx].len, stderr); + fputc('\n', stderr); goto ERROR; } if ( tokens[n].type == LAPIS_TTYPE_OPER ) { if ( oper_priorities[n] >= -1 ) { - printf("ERROR: bad logic line #%d\n", __LINE__); + + fprintf(stderr, "ERROR[%d]: Attempted to take RHS as operand before evaluation!", __LINE__); + fputs(" rhs=", stderr); + fwrite(tokens[n].str, 1, tokens[n].len, stderr); + fputs(" operator=", stderr); + fwrite(tokens[max_prior_idx].str, 1, tokens[max_prior_idx].len, stderr); + fputc('\n', stderr); goto ERROR; } @@ -601,10 +617,7 @@ lapis_node_t *lapis_parse_expr(size_t *nnodes, FILE *f) { // later lower priority dependent operators can find its node. int64_t node_idx = -(oper_priorities[n] + 2); - if ( node_idx < 0 ) { - printf("ERROR: bad logic line #%d\n", __LINE__); - goto ERROR; - } + assert( node_idx > 0 ); node.rhs_idx = node_idx;