Backup, Decl,Type parsing enum,structs etc.
This commit is contained in:
87
parser.c
87
parser.c
@@ -15,11 +15,9 @@ function void
|
||||
parser_init(Parser *p){
|
||||
p->interns_count = 4096;
|
||||
p->interns = arena_push_array(&p->intern_table_arena, Intern_String, p->interns_count);
|
||||
memory_zero(p->interns, sizeof(Intern_String)*p->interns_count);
|
||||
|
||||
p->symbols_count = 4096;
|
||||
p->symbols = arena_push_array(&p->intern_table_arena, Intern_String, p->symbols_count);
|
||||
memory_zero(p->symbols, sizeof(Intern_String)*p->symbols_count);
|
||||
|
||||
keyword_s64 = intern_string(p, lit("S64"));
|
||||
keyword_u64 = intern_string(p, lit("U64"));
|
||||
@@ -50,6 +48,7 @@ intern_is_keyword(Parser *p, Intern_String intern){
|
||||
|
||||
function void
|
||||
parser_push_error(Parser *p, Token *token, char *str, ...){
|
||||
|
||||
String string;
|
||||
{
|
||||
va_list args1, args2;
|
||||
@@ -63,6 +62,7 @@ parser_push_error(Parser *p, Token *token, char *str, ...){
|
||||
va_end(args1);
|
||||
}
|
||||
|
||||
lex_print("Error: %s\n", string.str);// @Todo(Krzosa):
|
||||
Parser_Error *error = arena_push_struct(&p->main_arena, Parser_Error);
|
||||
error->message = string;
|
||||
error->next = 0;
|
||||
@@ -112,6 +112,8 @@ intern_string(Parser *p, String string){
|
||||
Intern_String *intern = p->interns + index.iter;
|
||||
if(intern->s.str == 0){
|
||||
result.s = arena_push_string_copy(&p->main_arena, string);
|
||||
p->interns_in_bytes += string.len;
|
||||
p->interns_inserted += 1;
|
||||
*intern = result;
|
||||
break;
|
||||
}
|
||||
@@ -141,26 +143,29 @@ intern_tokens(Parser *p){
|
||||
}
|
||||
}
|
||||
|
||||
function void
|
||||
symbol_insert(Parser *p, Symbol symbol){
|
||||
String string = symbol.string.s;
|
||||
function Symbol *
|
||||
symbol_get_slot(Parser *p, Intern_String intern){
|
||||
String string = intern.s;
|
||||
Table_Index index = table_index_from_string(string, p->symbols_count);
|
||||
|
||||
for(;;){
|
||||
Symbol *slot = p->symbols + index.iter;
|
||||
if(slot->string.s.str == 0){
|
||||
*slot = symbol;
|
||||
break;
|
||||
slot->string = intern;
|
||||
p->symbols_inserted++;
|
||||
return slot;
|
||||
}
|
||||
else if(slot->string.s.str == string.str){
|
||||
invalid_codepath;
|
||||
break;
|
||||
return slot;
|
||||
}
|
||||
|
||||
if (table_index_advance(&index))
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
parser_push_error(p, token_get(p), "Failed to find a spot for symbol");
|
||||
return 0;
|
||||
}
|
||||
|
||||
function Symbol *
|
||||
@@ -179,31 +184,63 @@ symbol_get(Parser *p, Intern_String string){
|
||||
return 0;
|
||||
}
|
||||
|
||||
function B32
|
||||
symbol_require_empty(Parser *p, Symbol *symbol){
|
||||
assert(symbol);
|
||||
B32 result = symbol->kind == SK_None;
|
||||
if(!result){
|
||||
// @Todo(Krzosa): Should send symbol name not token
|
||||
parser_push_error(p, token_get(p), "This symbol name is already registered");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function void
|
||||
const_val_insert(Parser *p, Token *token, Type *type, Intern_String string, Expr *expr){
|
||||
Symbol *symbol = symbol_get_slot(p, string);
|
||||
if(symbol_require_empty(p, symbol)){
|
||||
symbol->kind = SK_Const;
|
||||
symbol->token = token;
|
||||
symbol->const_val.type = type;
|
||||
symbol->const_val.expr = expr;
|
||||
}
|
||||
}
|
||||
|
||||
function void
|
||||
variable_insert(Parser *p, Decl *decl){
|
||||
Symbol *symbol = symbol_get_slot(p, decl->name);
|
||||
if(symbol_require_empty(p, symbol)){
|
||||
symbol->kind = SK_Decl;
|
||||
symbol->decl = decl;
|
||||
symbol->string = decl->name;
|
||||
symbol->token = decl->token;
|
||||
}
|
||||
}
|
||||
|
||||
function void
|
||||
type_insert(Parser *p, Type *type, Intern_String string){
|
||||
Symbol symbol = {.kind=SK_Type, .string=string, .type=type};
|
||||
symbol_insert(p, symbol);
|
||||
Symbol *symbol = symbol_get_slot(p, string);
|
||||
if(symbol_require_empty(p, symbol)){
|
||||
symbol->kind = SK_Type;
|
||||
symbol->type = type;
|
||||
symbol->string = string;
|
||||
}
|
||||
}
|
||||
|
||||
function Type *
|
||||
type_get(Parser *p, Token *token){
|
||||
type_get(Parser *p, Intern_String string){
|
||||
Type *result = 0;
|
||||
if(token->kind == TK_Identifier || token->kind == TK_Keyword){
|
||||
Symbol *symbol = symbol_get(p, token->intern_val);
|
||||
if(symbol){
|
||||
if(symbol->kind == SK_Type){
|
||||
result = symbol->type;
|
||||
}
|
||||
else {
|
||||
parser_push_error(p, token, "Symbol is not a type");
|
||||
}
|
||||
Symbol *symbol = symbol_get(p, string);
|
||||
if(symbol){
|
||||
if(symbol->kind == SK_Type){
|
||||
result = symbol->type;
|
||||
}
|
||||
else{
|
||||
parser_push_error(p, token, "Undefined type");
|
||||
else {
|
||||
parser_push_error(p, token_get(p), "Symbol is not a type");
|
||||
}
|
||||
}
|
||||
else {
|
||||
parser_push_error(p, token, "Trying to lookup a type with token of wrong kind");
|
||||
else{
|
||||
parser_push_error(p, token_get(p), "Undefined type");
|
||||
}
|
||||
|
||||
if(!result){
|
||||
|
||||
Reference in New Issue
Block a user