func parser kinda works, need to parse body and uhardcode int
This commit is contained in:
90
src/parser.h
90
src/parser.h
@@ -201,32 +201,29 @@ void skip_space(Token *inp, size_t *idx){
|
|||||||
while (inp->type[*idx] == TOKEN_SPACE || inp->type[*idx] == TOKEN_NEWLINE) (*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);
|
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);
|
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);
|
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);
|
skip_space(inp, idx);
|
||||||
|
|
||||||
Symbol func = {0};
|
Symbol func = {0};
|
||||||
@@ -236,83 +233,78 @@ Token parse_func_def(Token *inp, size_t *idx, SymbolTable *sym){
|
|||||||
func.arg_count = 0;
|
func.arg_count = 0;
|
||||||
func.builtin = false;
|
func.builtin = false;
|
||||||
|
|
||||||
SymbolTable args = {0};
|
while (inp->type[*idx] != TOKEN_RPAREN) {
|
||||||
while (inp->type[*idx] != TOKEN_RPAREN){
|
skip_space(inp, idx);
|
||||||
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 argument name\n");
|
||||||
fprintf(stderr, "Expected Arg Name\n");
|
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
skip_space(inp, idx);
|
|
||||||
|
|
||||||
Symbol arg = {0};
|
|
||||||
arg.name = strdup(inp->text[*idx]);
|
|
||||||
arg.arg_types[func.arg_count] = inp->type[*idx];
|
|
||||||
arg.builtin = false;
|
|
||||||
arg.symbol_kind = SYM_VAR;
|
|
||||||
// symbol_table_add(&args, arg); // TODO: do this after parsing arg type
|
|
||||||
(*idx)++;
|
(*idx)++;
|
||||||
skip_space(inp, 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 argument name\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
// func.arg_count++;
|
|
||||||
(*idx)++;
|
(*idx)++;
|
||||||
skip_space(inp, 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) {
|
||||||
fprintf(stderr, "Expected Type after ':'\n"); // BUT NEED TO CHECK TABLE IF WE DO ^
|
fprintf(stderr, "Expected type after ':'\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
arg.arg_types[func.arg_count] = inp->type[*idx];
|
|
||||||
|
func.arg_types[func.arg_count++] = inp->type[*idx];
|
||||||
(*idx)++;
|
(*idx)++;
|
||||||
skip_space(inp, idx);
|
skip_space(inp, idx);
|
||||||
|
|
||||||
if (inp->type[*idx] == TOKEN_COMMA){
|
if (inp->type[*idx] == TOKEN_COMMA) {
|
||||||
(*idx)++;
|
(*idx)++;
|
||||||
func.arg_count++; // PROBABLY THE RIGHT PLACE TO DO THIS
|
|
||||||
continue;
|
continue;
|
||||||
// fprintf(stderr, "Expected Comma after type\n");
|
} else if (inp->type[*idx] == TOKEN_RPAREN) {
|
||||||
// fprintf(stderr, "At Token %zu\n", *idx);
|
|
||||||
} else if (inp->type[*idx] == TOKEN_RPAREN){
|
|
||||||
skip_space(inp, idx);
|
|
||||||
break;
|
break;
|
||||||
// func.arg_count++; // PROBABLY THE RIGHT PLACE TO DO THIS
|
|
||||||
// (*idx)++;
|
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "Expected Comma or RPAREN after type\n");
|
fprintf(stderr, "Expected ',' or ')' after argument type\n");
|
||||||
fprintf(stderr, "At Token %zu\n", *idx);
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
|
||||||
// (*idx)++;
|
|
||||||
// skip_space(inp, idx);
|
|
||||||
//
|
|
||||||
}
|
|
||||||
(*idx)++;
|
(*idx)++;
|
||||||
skip_space(inp, idx);
|
skip_space(inp, idx);
|
||||||
if (inp->type[*idx] != TOKEN_IDENT_INT){
|
|
||||||
|
if (inp->type[*idx] != TOKEN_IDENT_INT) {
|
||||||
fprintf(stderr, "Expected return type after ')'\n");
|
fprintf(stderr, "Expected return type after ')'\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
func.ret_type = inp->type[*idx]; // probably wont work for serious typing.
|
|
||||||
|
func.ret_type = inp->type[*idx];
|
||||||
(*idx)++;
|
(*idx)++;
|
||||||
skip_space(inp, 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 '{'\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
(*idx)++;
|
(*idx)++;
|
||||||
skip_space(inp, idx);
|
skip_space(inp, idx);
|
||||||
|
|
||||||
while (inp->type[*idx] != TOKEN_RCURLY){
|
while (inp->type[*idx] != TOKEN_RCURLY && inp->type[*idx] != TOKEN_EOF) {
|
||||||
// FULL PARSING LOGIC SHOULD BE HERE
|
(*idx)++;
|
||||||
// let (definiton parser should be alone)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
if (inp->type[*idx] != TOKEN_RCURLY) {
|
||||||
|
fprintf(stderr, "Expected '}' at end of function\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
(*idx)++;
|
||||||
|
symbol_table_add(sym, func);
|
||||||
|
Token empty = {0};
|
||||||
|
return empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user