src/util.c
// Show an error message with its location information.
Show an error message with its location information.
Show an error message with its location information .
void error_at(char *loc, char *fmt, ...) {
    char *line = loc;
    while (user_input < line && line[-1] != '\n') {
        line--;
    }
    char *end = loc;
    while (*end != '\n') {
        end++;
    }
    int line_num = 1;
    for (char *p = user_input; p < line; p++) {
        if (*p == '\n') {
            line_num++;
        }
    }
    int indent = fprintf(stderr, "%s:%d: ", file_name, line_num);
    fprintf(stderr, "%.*s\n", (int)(end - line), line);
    int pos = loc - line + indent;
    fprintf(stderr, "%*s", pos, "");
    fprintf(stderr, "^ ");
    va_list ap;
    va_start(ap, fmt);
    fprintf(stderr, "\033[31merror\033[m: ");
    vfprintf(stderr, fmt, ap);
    fprintf(stderr, "\n");
    exit(1);
}
void error_at ( char * loc , char * fmt , ... ) { char * line = loc ; while ( user_input < line && line [ -1 ] != ' \n ' ) { line -- ; } char * end = loc ; while ( * end != ' \n ' ) { end ++ ; } int line_num = 1 ; for ( char * p = user_input ; p < line ; p ++ ) { if ( * p == ' \n ' ) { line_num ++ ; } } int indent = fprintf ( stderr , " " , file_name , line_num ) ; fprintf ( stderr , " \n " , ( int ) ( end - line ) , line ) ; int pos = loc - line + indent ; fprintf ( stderr , " " , pos , " " ) ; fprintf ( stderr , " " ) ; va_list ap ; va_start ( ap , fmt ) ; fprintf ( stderr , " \033 \033 " ) ; vfprintf ( stderr , fmt , ap ) ; fprintf ( stderr , " \n " ) ; exit ( 1 ) ; }
Show an error message with its location information.
Show an error message with its location information .
// Find the start/end positions of the line. // Investigate the line number. // Report the line number with the file name. // Display a pointer to the error location. // Display an error message.