From 1b5b4e53fb4d266100a51c21de2271763fd4ed63 Mon Sep 17 00:00:00 2001 From: shabani005 Date: Thu, 6 Nov 2025 19:06:43 +0300 Subject: [PATCH] func parser base ready --- src/lexer.h | 15 ++++++++-- src/parser.h | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 92 insertions(+), 5 deletions(-) diff --git a/src/lexer.h b/src/lexer.h index 81dc4fb..7cbb5ca 100644 --- a/src/lexer.h +++ b/src/lexer.h @@ -26,7 +26,10 @@ typedef enum { TOKEN_LCURLY, TOKEN_RCURLY, TOKEN_COLON, - TOKEN_SEMI + TOKEN_SEMI, + TOKEN_FN, + TOKEN_LET, + TOKEN_IDENT_INT //TODO: unhardcode } symbols; typedef enum { @@ -61,6 +64,9 @@ char *token_type_to_string(symbols type) { case TOKEN_SEMI: return "TOKEN_SEMI"; case TOKEN_COLON: return "TOKEN_COLON"; case TOKEN_UNKNOWN: return "TOKEN_UNKNOWN"; + case TOKEN_FN: return "TOKEN_FN"; + case TOKEN_LET: return "TOKEN_LET"; + case TOKEN_IDENT_INT: return "TOKEN_IDENT_INT"; default: return "UNKNOWN_SYMBOL"; } } @@ -187,7 +193,12 @@ size_t read_from_tok(Token *tok, const char *input, size_t cursor) { if (i >= sizeof(buf) - 1) break; } buf[i] = '\0'; - token_push(tok, TOKEN_IDENTIFIER, buf, BHV_IDENT, cursor - start); + + if (strcmp(buf, "let") == 0) token_push(tok, TOKEN_LET, buf, BHV_UNDEFINED, cursor - start); + else if (strcmp(buf, "fn") == 0) token_push(tok, TOKEN_FN, buf, BHV_UNDEFINED, cursor - start); + else if (strcmp(buf, "int") == 0) token_push(tok, TOKEN_IDENT_INT, buf, BHV_UNDEFINED, cursor - start); // TODO: unhardcode + else token_push(tok, TOKEN_IDENTIFIER, buf, BHV_IDENT, cursor - start); + return cursor - start; } diff --git a/src/parser.h b/src/parser.h index af44b3e..3e7e32a 100644 --- a/src/parser.h +++ b/src/parser.h @@ -79,9 +79,9 @@ Symbol *symbol_lookup(SymbolTable *table, const char *n){ void symbol_table_init(SymbolTable *table, size_t initial_capacity) { - table->symbols = malloc(sizeof(Symbol) * initial_capacity); + table->symbols = (Symbol*)malloc(sizeof(Symbol) * initial_capacity); if (!table->symbols) { - fprintf(stderr, "symbol_table_init: malloc failed\n"); + fprintf(stderr, "symbol_table_init: malloc failed\n"); // should not happen exit(1); } table->size = 0; @@ -91,7 +91,7 @@ void symbol_table_init(SymbolTable *table, size_t initial_capacity) { void symbol_table_add(SymbolTable *table, Symbol sym) { if (table->size >= table->capacity) { table->capacity = (table->capacity == 0) ? 8 : table->capacity * 2; - table->symbols = realloc(table->symbols, sizeof(Symbol) * table->capacity); + table->symbols = (Symbol*)realloc(table->symbols, sizeof(Symbol) * table->capacity); if (!table->symbols) { fprintf(stderr, "symbol_table_add: realloc failed\n"); exit(1); @@ -197,6 +197,82 @@ void print_token(Token *tk){ } +Token parse_func_def(Token *inp, size_t *idx, SymbolTable *sym){ + if (inp->type[*idx] != TOKEN_FN){ + fprintf(stderr, "Expected 'fn'\n"); + exit(1); + } + (*idx)++; + + if (inp->type[*idx] != TOKEN_IDENTIFIER){ + fprintf(stderr, "Expected function name after 'fn'\n"); + exit(1); + } + const char* fname = inp->text[*idx]; + (*idx)++; + + if (inp->type[*idx] != TOKEN_LPAREN){ + fprintf(stderr, "Expected '('\n"); + exit(1); + } + (*idx)++; + + Symbol func = {0}; + func.name = strdup(fname); + func.symbol_kind = SYM_FUNC; + func.ret_type = TOKEN_UNKNOWN; + func.arg_count = 0; + func.builtin = false; + + while (inp->type[*idx] != TOKEN_RPAREN){ + + if (inp->type[*idx] != TOKEN_IDENTIFIER){ + fprintf(stderr, "Expected Arg Name\n"); + exit(1); + } + // save THIS TOKEN NAME AS VARIABLE PROBABLY. + (*idx)++; + + if (inp->type[*idx] != TOKEN_COLON){ + fprintf(stderr, "Expected ':' after arg name\n"); + exit(1); + } + func.arg_count++; + (*idx)++; + + if (inp->type[*idx] != TOKEN_IDENT_INT){ // TODO: unharcode should be easy just keep it as TOKEN_IDENTIFIER + fprintf(stderr, "Expected Type after ':'\n"); // BUT NEED TO CHECK TABLE IF WE DO ^ + exit(1); + } + (*idx)++; + + if (inp->type[*idx] != TOKEN_COMMA){ + fprintf(stderr, "Expected Comma after type\n"); + exit(1); + } + (*idx)++; + + } + if (inp->type[*idx != TOKEN_IDENTIFIER]){ + fprintf(stderr, "Expected return type after ')'\n"); + exit(1); + } + func.ret_type = inp->type[*idx]; // probably wont work for serious typing. + (*idx)++; + + if (inp->type[*idx] != TOKEN_LCURLY){ + fprintf(stderr, "Expected Left Curly Bracket '{'\n"); + exit(1); + } + (*idx)++; + + while (inp->type[*idx] != TOKEN_RCURLY){ + // FULL PARSING LOGIC SHOULD BE HERE + // let (definiton parser should be alone) + } +} + + // int main(int argc, char **argv){