diff --git a/src/error.c b/src/error.c index d64c938..7b02d3b 100644 --- a/src/error.c +++ b/src/error.c @@ -8,7 +8,7 @@ noom_uint_t noom_format_error(const noomP_Parser* parser, char* buffer, noom_uin }; static const struct noom_error parser_errors[] = { - [NOOMP_ERROR_NONE] = {0, 0 }, + [NOOMP_ERROR_NONE] = {0, 0}, [NOOMP_ERROR_OOM] = {"Whoops! Out of memory :(\n", 0}, [NOOMP_ERROR_EXPECTED_LCURLY] = {"expected '{'", 1}, @@ -41,12 +41,13 @@ noom_uint_t noom_format_error(const noomP_Parser* parser, char* buffer, noom_uin [NOOMP_ERROR_EXPECTED_UNTIL] = {"expected 'until' to close repeat expression", 1}, [NOOMP_ERROR_EXPECTED_IDENTIFIER_AFTER_GOTO] = {"expected identifier after goto\n", 0}, [NOOMP_ERROR_EXPECTED_COLONCOLON] = {"expected :: to end label identifier", 1}, - [NOOMP_ERROR_UNEXPECTED_VALUE] = {"unexpected value", 1}, + [NOOMP_ERROR_INVALID_STATEMENT] = {"expected statement, got", 2}, // I want someone smarter than me [tema5002] to give these a proper description - [NOOMP_ERROR_UNEXPECTED_SOMETHING1] = {"", 1}, - [NOOMP_ERROR_UNEXPECTED_SOMETHING2] = {"", 1}, - [NOOMP_ERROR_UNEXPECTED_SOMETHING3] = {"", 1}, - [NOOMP_ERROR_UNEXPECTED_SOMETHING4] = {"", 1}, + // ^ alrighty then + [NOOMP_ERROR_EXPECTED_ASSIGNABLE] = {"expected assignable expression after comma in assignment", 1}, + [NOOMP_ERROR_NOT_ASSIGNABLE] = {"expression in assignment is not assignable", 1}, + [NOOMP_ERROR_EXPECTED_COMMA_OR_EQUAL_IN_ASSIGNMENT] = {"expected a comma or equals after assignable in assignment", 1}, + [NOOMP_ERROR_EXPRESSION_NOT_STATEMENT] = {"loose expression is not a valid statement", 1}, [NOOMP_ERROR_FAKEATTRIBUTE] = {"invalid attribute", 2}, [NOOMP_ERROR_RETURN_NOT_END] = {"'return' must be the last statement in a block\n", 0}, [NOOMP_ERROR_FOR_WRONG_AMOUNT] = {"'for' initializer must have 2 or 3 expressions\n", 0} @@ -91,22 +92,29 @@ noom_uint_t noom_format_error(const noomP_Parser* parser, char* buffer, noom_uin if (buffer == 0) { noom_uint_t linedig = 0; - for (noom_uint_t eh = row; eh; eh /= 10, linedig++); + for (noom_uint_t n = row; n; n /= 10, linedig++); - return + noom_uint_t space = 0; + + space = sizeof("noom: ") - 1 + noom_strlen(parser->filename) + sizeof(":") - 1 + linedig + sizeof(":") - 1 + noom_strlen(err.s) + - + 1 + // \0 - (err.near ? ( + + 1; // \0; + + if (err.near) { + space += ( (err.near == 1 ? sizeof(" near") - 1 : 0) + sizeof(" '") - 1 + parser->last_token_length + sizeof("'\n") - 1 - ) : 0); + ); + } + + return space; } noom_safe_strcpy(buffer, &pos, buffer_size, "noom: "); @@ -115,10 +123,10 @@ noom_uint_t noom_format_error(const noomP_Parser* parser, char* buffer, noom_uin char num_buf[20]; noom_uint_t num_len = 0; + if (row == 0) { num_buf[num_len++] = '0'; - } - else { + } else { noom_uint_t temp = row; noom_uint_t divisor = 1; while (temp / divisor >= 10) divisor *= 10; @@ -145,6 +153,7 @@ noom_uint_t noom_format_error(const noomP_Parser* parser, char* buffer, noom_uin } noom_safe_strcpy(buffer, &pos, buffer_size, "'\n"); } + if (pos < buffer_size) buffer[pos] = '\0'; return pos; } diff --git a/src/error.h b/src/error.h index 81eef5e..5f0d3a8 100644 --- a/src/error.h +++ b/src/error.h @@ -1,4 +1,5 @@ // js let me use include guards 🥀🥀🥀🥀🥀🥀🥀 +// ^ header guards r 4 noobz! #include "types.h" #include "parser.h" diff --git a/src/main.c b/src/main.c index 8f0fb45..1840753 100644 --- a/src/main.c +++ b/src/main.c @@ -33,7 +33,7 @@ void print_node(noomP_Node *node, noom_uint_t depth) { int main(int argc, char **argv) { // uhh uhhh uhhhhh - const char *code = "local t = {'a'; 2; 6 \"\\xgg\""; + const char *code = "a,(\"hi\") = 2"; noom_uint_t pos = 0; printf("LEX OUTPUT:\n"); @@ -41,7 +41,7 @@ int main(int argc, char **argv) { fputs("\x1b[48;2;10;10;10m", stdout); while (1) { noomL_Token token; - //noomL_lex(code, pos, &token, NOOM_VERSION_54); + noomL_ErrorType err = noomL_lex(code, pos, &token, NOOM_VERSION_54); if (err) break; diff --git a/src/parser.c b/src/parser.c index a83f041..5aa0999 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1685,7 +1685,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) { if (token.type != NOOML_TOKEN_IDENTIFIER && (token.type != NOOML_TOKEN_SYMBOL || !noom_streql(parser->code + token.offset, token.length, "(", 1))) { // unexpected - parser->error_state = NOOMP_ERROR_UNEXPECTED_SOMETHING1; + parser->error_state = NOOMP_ERROR_EXPECTED_ASSIGNABLE; return 0; } @@ -1694,7 +1694,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) { if (item == 0) return 0; if (item->type != NOOMP_NODE_INDEX && item->type != NOOMP_NODE_GETFIELD && item->type != NOOMP_NODE_VARIABLE) { - parser->error_state = NOOMP_ERROR_UNEXPECTED_SOMETHING2; + parser->error_state = NOOMP_ERROR_NOT_ASSIGNABLE; return 0; // unexpected } @@ -1713,7 +1713,7 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) { break; } } else { - parser->error_state = NOOMP_ERROR_UNEXPECTED_SOMETHING3; + parser->error_state = NOOMP_ERROR_EXPECTED_COMMA_OR_EQUAL_IN_ASSIGNMENT; return 0; // unexpected } } @@ -1742,12 +1742,12 @@ noomP_Node* noomP_parseRawStatement(noomP_Parser* parser) { // this expression is now a statement. return base; // no need to eat any more. } else { - parser->error_state = NOOMP_ERROR_UNEXPECTED_SOMETHING4; + parser->error_state = NOOMP_ERROR_EXPRESSION_NOT_STATEMENT; return 0; // unexpected. e.g. random string or whatever } } - parser->error_state = NOOMP_ERROR_UNEXPECTED_VALUE; + parser->error_state = NOOMP_ERROR_INVALID_STATEMENT; return 0; } diff --git a/src/parser.h b/src/parser.h index 0d6f2a7..ed3a963 100644 --- a/src/parser.h +++ b/src/parser.h @@ -111,13 +111,12 @@ typedef enum noomP_Error { NOOMP_ERROR_EXPECTED_IDENTIFIER_AFTER_GOTO, NOOMP_ERROR_EXPECTED_COLONCOLON, - NOOMP_ERROR_UNEXPECTED_VALUE, + NOOMP_ERROR_INVALID_STATEMENT, - // i am sorry - NOOMP_ERROR_UNEXPECTED_SOMETHING1, - NOOMP_ERROR_UNEXPECTED_SOMETHING2, - NOOMP_ERROR_UNEXPECTED_SOMETHING3, - NOOMP_ERROR_UNEXPECTED_SOMETHING4, + NOOMP_ERROR_EXPECTED_ASSIGNABLE, + NOOMP_ERROR_NOT_ASSIGNABLE, + NOOMP_ERROR_EXPECTED_COMMA_OR_EQUAL_IN_ASSIGNMENT, + NOOMP_ERROR_EXPRESSION_NOT_STATEMENT, NOOMP_ERROR_FAKEATTRIBUTE, NOOMP_ERROR_RETURN_NOT_END,