From: ahmedsamyh Date: Wed, 26 Feb 2025 17:27:27 +0000 (+0500) Subject: [main.c] Use String_view for Lexer.src. X-Git-Url: https://www.git.momoyon.org/?a=commitdiff_plain;h=ee6643405351e236c5b203db9f9e08018d50c705;p=lang.git [main.c] Use String_view for Lexer.src. --- diff --git a/include/commonlib.h b/include/commonlib.h index b147ffb..7af1d7b 100644 --- a/include/commonlib.h +++ b/include/commonlib.h @@ -46,6 +46,38 @@ #define shift_args c_shift_args +#define SV_FMT c_SV_FMT +#define SV_ARG c_SV_ARG + +#define SV c_SV + +#define sv_print_dumb c_sv_print_dumb +#define sv_from_cstr c_sv_from_cstr +#define sv_lpop c_sv_lpop +#define sv_lpop_until_predicate c_sv_lpop_until_predicate +#define sv_rpop_until_predicate c_sv_rpop_until_predicate +#define sv_lpop_until_char c_sv_lpop_until_char +#define sv_rpop_until_char c_sv_rpop_until_char +#define sv_lremove c_sv_lremove +#define sv_rremove c_sv_rremove +#define sv_lremove_until_char c_sv_lremove_until_char +#define sv_rremove_until_char c_sv_rremove_until_char +#define sv_lremove_until_char_after c_sv_lremove_until_char_after +#define sv_rremove_until_char_after c_sv_rremove_until_char_after +#define sv_ltrim c_sv_ltrim +#define sv_rtrim c_sv_rtrim +#define sv_trim c_sv_trim +#define sv_to_cstr c_sv_to_cstr +#define sv_to_int c_sv_to_int +#define sv_to_uint64 c_sv_to_uint64 +#define sv_to_uint8_hex c_sv_to_uint8_hex +#define sv_to_ptr c_sv_to_ptr +#define sv_to_float c_sv_to_float +#define sv_contains_char c_sv_contains_char +#define sv_is_hex_numbers c_sv_is_hex_numbers +#define sv_equals c_sv_equals + + #endif // COMMONLIB_REMOVE_PREFIX // typedefs diff --git a/main.c b/main.c index 6de8c60..eecc433 100644 --- a/main.c +++ b/main.c @@ -1,4 +1,5 @@ #include +#include #define COMMONLIB_IMPLEMENTATION #define COMMONLIB_REMOVE_PREFIX @@ -12,8 +13,19 @@ void usage(const char *program) { } typedef struct { - const char *src; - size_t src_len; + const char *filename; + int line; + int col; +} Location; + +void print_loc(FILE *f, Location *loc) { + ASSERT(loc != NULL, "Bro you passed a NULL..."); + fprintf(f, "%s:%d:%d", loc->filename, loc->line, loc->col); +} + +typedef struct { + // NOTE: src gets data from a heap allocated string!!! + String_view src; size_t cur; size_t bol; // Beginning of Line size_t line; @@ -38,8 +50,7 @@ Lexer make_lexer(const char *filename) { exit(1); } Lexer l = { - .src = buf, - .src_len = strlen(buf), + .src = sv_from_cstr(buf), .cur = 0, .bol = 0, .line = 1, @@ -50,12 +61,12 @@ Lexer make_lexer(const char *filename) { } bool eof(Lexer *l) { - return l->cur >= l->src_len; + return l->cur >= l->src.count; } char current_char(Lexer *l) { ASSERT(!eof(l), "Trying to get char after EOF"); - return l->src[l->cur]; + return l->src.data[l->cur]; } char consume_char(Lexer *l) { @@ -64,6 +75,11 @@ char consume_char(Lexer *l) { return ch; } +void consume_ident(Lexer *l, String_view *ident_sv_out, Location *loc_out) { + // Identifiers can start with [a-z][A-Z]_ and contain [0-9] after the first char + ASSERT(isalpha(current_char(l)) || current_char(l) == '_', "Called consume_identifier() at the wrong character!"); +} + void left_trim(Lexer *l) { while (!eof(l) && isspace(current_char(l))) { // TODO: Care about window's \r\n.... @@ -76,13 +92,26 @@ void left_trim(Lexer *l) { } bool next_token(Lexer *l, Token *t_out) { + (void)t_out; left_trim(l); if (eof(l)) return false; char ch = current_char(l); + if (isalpha(ch) || ch == '_') { + String_view ident_sv = {0}; + Location ident_loc = {0}; + consume_ident(l, &ident_sv, &ident_loc); + } + switch (ch) { + case '"': { + } break; + default: { + error("Unhandled char '%c'", ch); + ASSERT(false, "UNREACHABLE!"); + } } /*info("ch: '%c'", ch);*/