From 6ad5f4e706beab11d4b2ef2c20d0cde80283b560 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Fri, 10 Jun 2022 16:26:26 +0200 Subject: [PATCH] Nicer error messages --- order1.kl | 2 +- parsing.cpp | 29 +++++++++++++++++------------ typechecking.cpp | 3 ++- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/order1.kl b/order1.kl index 0cd1560..179c512 100644 --- a/order1.kl +++ b/order1.kl @@ -15,5 +15,5 @@ val := CONSTANT_VAL DEPENDENCE :: CONSTANT_VAL CONSTANT_VAL :: 10 -//thing: a_type = 10 +thing: a_type = 10 diff --git a/parsing.cpp b/parsing.cpp index 19c1c56..62a489c 100644 --- a/parsing.cpp +++ b/parsing.cpp @@ -1,5 +1,21 @@ function Ast_Decl *parse_decl(B32 is_global); +function void +print_token_context(Token *token){ + printf(" :: %s:%d\n", token->file.str, (S32)token->line + 1); + // @Note(Krzosa): Print error line + { + int i = 0; + while(token->line_begin[i]!='\n' && token->line_begin[i]!=0) i++; + printf("%.*s\n", i, token->line_begin); + + // @Note(Krzosa): Print error marker + int token_i = token->str - token->line_begin; + for(int i = 0; i < token_i-2; i++) printf(" "); + printf("^^^^^^\n"); + } +} + function void compiler_error(Token *token, const char *str, ...){ Scratch scratch; @@ -11,19 +27,8 @@ compiler_error(Token *token, const char *str, ...){ if(token->kind == TK_Error){ printf("Token Error: %.*s", (int)token->error_val.len, token->error_val.str); } - printf(" :: %s:%d\n", token->file.str, (S32)token->line + 1); - // @Note(Krzosa): Print error line - { - int i = 0; - while(token->line_begin[i]!='\n' && token->line_begin[i]!=0) i++; - printf("%.*s\n", i, token->line_begin); - - // @Note(Krzosa): Print error marker - int token_i = token->str - token->line_begin; - for(int i = 0; i < token_i-2; i++) printf(" "); - printf("^^^^^^\n"); - } + print_token_context(token); } __debugbreak(); diff --git a/typechecking.cpp b/typechecking.cpp index f7a84c7..2501b51 100644 --- a/typechecking.cpp +++ b/typechecking.cpp @@ -528,7 +528,8 @@ function void insert_into_scope(Ast_Scope *scope, Ast_Decl *decl){ Ast_Decl *find = search_for_decl(scope, decl->name); if(find){ - compiler_error(decl->pos, "[%s] is already defined in this scope", decl->name.str); + print_token_context(find->pos); + compiler_error(decl->pos, "[%s] is already defined", decl->name.str); } scope->decls.add(decl);