func parser base ready

This commit is contained in:
2025-11-06 19:06:43 +03:00
parent 5b476f08a3
commit 1b5b4e53fb
2 changed files with 92 additions and 5 deletions

View File

@@ -26,7 +26,10 @@ typedef enum {
TOKEN_LCURLY, TOKEN_LCURLY,
TOKEN_RCURLY, TOKEN_RCURLY,
TOKEN_COLON, TOKEN_COLON,
TOKEN_SEMI TOKEN_SEMI,
TOKEN_FN,
TOKEN_LET,
TOKEN_IDENT_INT //TODO: unhardcode
} symbols; } symbols;
typedef enum { typedef enum {
@@ -61,6 +64,9 @@ char *token_type_to_string(symbols type) {
case TOKEN_SEMI: return "TOKEN_SEMI"; case TOKEN_SEMI: return "TOKEN_SEMI";
case TOKEN_COLON: return "TOKEN_COLON"; case TOKEN_COLON: return "TOKEN_COLON";
case TOKEN_UNKNOWN: return "TOKEN_UNKNOWN"; 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"; 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; if (i >= sizeof(buf) - 1) break;
} }
buf[i] = '\0'; 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; return cursor - start;
} }

View File

@@ -79,9 +79,9 @@ Symbol *symbol_lookup(SymbolTable *table, const char *n){
void symbol_table_init(SymbolTable *table, size_t initial_capacity) { 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) { if (!table->symbols) {
fprintf(stderr, "symbol_table_init: malloc failed\n"); fprintf(stderr, "symbol_table_init: malloc failed\n"); // should not happen
exit(1); exit(1);
} }
table->size = 0; 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) { void symbol_table_add(SymbolTable *table, Symbol sym) {
if (table->size >= table->capacity) { if (table->size >= table->capacity) {
table->capacity = (table->capacity == 0) ? 8 : table->capacity * 2; 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) { if (!table->symbols) {
fprintf(stderr, "symbol_table_add: realloc failed\n"); fprintf(stderr, "symbol_table_add: realloc failed\n");
exit(1); 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){ // int main(int argc, char **argv){