parser improved
This commit is contained in:
27
src/parser.h
27
src/parser.h
@@ -197,25 +197,37 @@ void print_token(Token *tk){
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void skip_space(Token *inp, size_t *idx){
|
||||||
|
while (inp->type[*idx] == TOKEN_SPACE || inp->type[*idx] == TOKEN_NEWLINE) (*idx)++;
|
||||||
|
}
|
||||||
|
|
||||||
Token parse_func_def(Token *inp, size_t *idx, SymbolTable *sym){
|
Token parse_func_def(Token *inp, size_t *idx, SymbolTable *sym){
|
||||||
|
skip_space(inp, idx);
|
||||||
if (inp->type[*idx] != TOKEN_FN){
|
if (inp->type[*idx] != TOKEN_FN){
|
||||||
fprintf(stderr, "Expected 'fn'\n");
|
fprintf(stderr, "Expected 'fn'\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
(*idx)++;
|
(*idx)++;
|
||||||
|
skip_space(inp, idx);
|
||||||
|
|
||||||
if (inp->type[*idx] != TOKEN_IDENTIFIER){
|
if (inp->type[*idx] != TOKEN_IDENTIFIER){
|
||||||
fprintf(stderr, "Expected function name after 'fn'\n");
|
fprintf(stderr, "Expected function name after 'fn'\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
skip_space(inp, idx);
|
||||||
|
|
||||||
const char* fname = inp->text[*idx];
|
const char* fname = inp->text[*idx];
|
||||||
(*idx)++;
|
(*idx)++;
|
||||||
|
|
||||||
|
skip_space(inp, idx);
|
||||||
|
|
||||||
if (inp->type[*idx] != TOKEN_LPAREN){
|
if (inp->type[*idx] != TOKEN_LPAREN){
|
||||||
fprintf(stderr, "Expected '('\n");
|
fprintf(stderr, "Expected '('\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
(*idx)++;
|
(*idx)++;
|
||||||
|
|
||||||
|
skip_space(inp, idx);
|
||||||
|
|
||||||
Symbol func = {0};
|
Symbol func = {0};
|
||||||
func.name = strdup(fname);
|
func.name = strdup(fname);
|
||||||
@@ -226,11 +238,13 @@ Token parse_func_def(Token *inp, size_t *idx, SymbolTable *sym){
|
|||||||
|
|
||||||
SymbolTable args = {0};
|
SymbolTable args = {0};
|
||||||
while (inp->type[*idx] != TOKEN_RPAREN){
|
while (inp->type[*idx] != TOKEN_RPAREN){
|
||||||
|
if (inp->type[*idx] == TOKEN_SPACE || inp->type[*idx] == TOKEN_NEWLINE) (*idx)++;
|
||||||
if (inp->type[*idx] != TOKEN_IDENTIFIER){
|
if (inp->type[*idx] != TOKEN_IDENTIFIER){
|
||||||
fprintf(stderr, "Expected Arg Name\n");
|
fprintf(stderr, "Expected Arg Name\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
skip_space(inp, idx);
|
||||||
|
|
||||||
Symbol arg = {0};
|
Symbol arg = {0};
|
||||||
arg.name = strdup(inp->text[*idx]);
|
arg.name = strdup(inp->text[*idx]);
|
||||||
arg.arg_types[func.arg_count] = inp->type[*idx];
|
arg.arg_types[func.arg_count] = inp->type[*idx];
|
||||||
@@ -238,13 +252,15 @@ Token parse_func_def(Token *inp, size_t *idx, SymbolTable *sym){
|
|||||||
arg.symbol_kind = SYM_VAR;
|
arg.symbol_kind = SYM_VAR;
|
||||||
// symbol_table_add(&args, arg); // TODO: do this after parsing arg type
|
// symbol_table_add(&args, arg); // TODO: do this after parsing arg type
|
||||||
(*idx)++;
|
(*idx)++;
|
||||||
|
skip_space(inp, idx);
|
||||||
|
|
||||||
if (inp->type[*idx] != TOKEN_COLON){
|
if (inp->type[*idx] != TOKEN_COLON){
|
||||||
fprintf(stderr, "Expected ':' after arg name\n");
|
fprintf(stderr, "Expected ':' after arg name\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
// func.arg_count++;
|
// func.arg_count++;
|
||||||
(*idx)++;
|
(*idx)++;
|
||||||
|
skip_space(inp, idx);
|
||||||
|
|
||||||
if (inp->type[*idx] != TOKEN_IDENT_INT){ // TODO: unharcode should be easy just keep it as TOKEN_IDENTIFIER
|
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 ^
|
fprintf(stderr, "Expected Type after ':'\n"); // BUT NEED TO CHECK TABLE IF WE DO ^
|
||||||
@@ -252,13 +268,16 @@ Token parse_func_def(Token *inp, size_t *idx, SymbolTable *sym){
|
|||||||
}
|
}
|
||||||
arg.arg_types[func.arg_count] = inp->type[*idx];
|
arg.arg_types[func.arg_count] = inp->type[*idx];
|
||||||
(*idx)++;
|
(*idx)++;
|
||||||
|
skip_space(inp, idx);
|
||||||
|
|
||||||
if (inp->type[*idx] != TOKEN_COMMA){
|
if (inp->type[*idx] != TOKEN_COMMA){
|
||||||
fprintf(stderr, "Expected Comma after type\n");
|
fprintf(stderr, "Expected Comma after type\n");
|
||||||
|
fprintf(stderr, "At Token %zu\n", *idx);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
func.arg_count++; // PROBABLY THE RIGHT PLACE TO DO THIS
|
func.arg_count++; // PROBABLY THE RIGHT PLACE TO DO THIS
|
||||||
(*idx)++;
|
(*idx)++;
|
||||||
|
skip_space(inp, idx);
|
||||||
|
|
||||||
}
|
}
|
||||||
if (inp->type[*idx] != TOKEN_IDENTIFIER){
|
if (inp->type[*idx] != TOKEN_IDENTIFIER){
|
||||||
@@ -267,12 +286,14 @@ Token parse_func_def(Token *inp, size_t *idx, SymbolTable *sym){
|
|||||||
}
|
}
|
||||||
func.ret_type = inp->type[*idx]; // probably wont work for serious typing.
|
func.ret_type = inp->type[*idx]; // probably wont work for serious typing.
|
||||||
(*idx)++;
|
(*idx)++;
|
||||||
|
skip_space(inp, idx);
|
||||||
|
|
||||||
if (inp->type[*idx] != TOKEN_LCURLY){
|
if (inp->type[*idx] != TOKEN_LCURLY){
|
||||||
fprintf(stderr, "Expected Left Curly Bracket '{'\n");
|
fprintf(stderr, "Expected Left Curly Bracket '{'\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
(*idx)++;
|
(*idx)++;
|
||||||
|
skip_space(inp, idx);
|
||||||
|
|
||||||
while (inp->type[*idx] != TOKEN_RCURLY){
|
while (inp->type[*idx] != TOKEN_RCURLY){
|
||||||
// FULL PARSING LOGIC SHOULD BE HERE
|
// FULL PARSING LOGIC SHOULD BE HERE
|
||||||
|
|||||||
Reference in New Issue
Block a user