Almost works
This commit is contained in:
123
new_parse.c
123
new_parse.c
@@ -555,7 +555,7 @@ name::const = 4254;
|
||||
// }
|
||||
*/
|
||||
function void
|
||||
parse_note_list(Parser *p, Note *parent) {
|
||||
parse_note_list(Parser *p, AST_Parent *parent) {
|
||||
if(token_match(p, TK_OpenParen)) {
|
||||
if(token_match(p, TK_CloseParen)){
|
||||
return;
|
||||
@@ -563,7 +563,7 @@ parse_note_list(Parser *p, Note *parent) {
|
||||
do {
|
||||
Token *name = token_expect(p, TK_Identifier);
|
||||
Note *current = note_push_new(p->arena, parent, name, name->intern_val, 0);
|
||||
parse_note_list(p, current);
|
||||
parse_note_list(p, (AST_Parent *)current);
|
||||
if(token_match(p, TK_Assign)) {
|
||||
current->expr = parse_expr(p);
|
||||
}
|
||||
@@ -573,11 +573,11 @@ parse_note_list(Parser *p, Note *parent) {
|
||||
}
|
||||
|
||||
function void
|
||||
parse__notes(Parser *p, Note *result) {
|
||||
parse__notes(Parser *p, AST_Parent *result) {
|
||||
while(token_match(p, TK_At)) {
|
||||
Token *name = token_expect(p, TK_Identifier);
|
||||
Note *current = note_push_new(p->arena, result, name, name->intern_val, 0);
|
||||
parse_note_list(p, current);
|
||||
parse_note_list(p, (AST_Parent *)current);
|
||||
if(token_match(p, TK_Assign)) {
|
||||
current->expr = parse_expr(p);
|
||||
}
|
||||
@@ -585,10 +585,10 @@ parse__notes(Parser *p, Note *result) {
|
||||
}
|
||||
}
|
||||
|
||||
function Note
|
||||
function Note_List *
|
||||
parse_notes(Parser *p){
|
||||
Note result = {0};
|
||||
parse__notes(p, &result);
|
||||
Note_List *result = note_list(p->arena, token_get(p));
|
||||
parse__notes(p, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -611,16 +611,16 @@ parse_optional_type(Parser *p, Intern_String type){
|
||||
return result;
|
||||
}
|
||||
|
||||
function Decl *
|
||||
function Decl_Enum *
|
||||
parse_enum(Parser *p, Token *name){
|
||||
Typespec *typespec = parse_optional_type(p, intern_int);
|
||||
Decl *result = decl_enum(p->arena, name, name->intern_val, typespec);
|
||||
Decl_Enum *result = decl_enum(p->arena, name, name->intern_val, typespec);
|
||||
token_expect(p, TK_OpenBrace);
|
||||
do {
|
||||
Note notes = parse_notes(p);
|
||||
Note_List *notes = parse_notes(p);
|
||||
Token *val = token_expect(p, TK_Identifier);
|
||||
Expr *expr = parse_assign_expr(p);
|
||||
decl_enum_push(p->arena, result, val, val->intern_val, expr, ¬es);
|
||||
decl_enum_push(p->arena, result, val, val->intern_val, expr, notes);
|
||||
if(!token_match(p, TK_Comma)){
|
||||
break;
|
||||
}
|
||||
@@ -630,7 +630,7 @@ parse_enum(Parser *p, Token *name){
|
||||
}
|
||||
|
||||
function void
|
||||
parse_var_decl(Parser *p, Decl *parent, Token *name, Note *notes){
|
||||
parse_var_decl(Parser *p, Decl_Struct *parent, Token *name, Note_List *notes){
|
||||
Token *name_stack[64];
|
||||
S64 name_stack_len = 0;
|
||||
name_stack[name_stack_len++] = name;
|
||||
@@ -640,51 +640,50 @@ parse_var_decl(Parser *p, Decl *parent, Token *name, Note *notes){
|
||||
Typespec *typespec = parse_typespec(p);
|
||||
token_expect(p, TK_Semicolon);
|
||||
for(S64 i = 0; i < name_stack_len; i++){
|
||||
Decl *decl = decl_variable(p->arena, name_stack[i], name_stack[i]->intern_val, typespec, 0);
|
||||
decl_pass_notes(decl, notes);
|
||||
decl_struct_push(parent, decl);
|
||||
Decl_Var *decl = decl_variable(p->arena, name_stack[i], name_stack[i]->intern_val, 0, typespec, notes);
|
||||
decl_struct_push(parent, (AST *)decl);
|
||||
}
|
||||
}
|
||||
|
||||
function Decl *
|
||||
parse_struct(Parser *p, Token *name, Decl_Kind struct_kind){
|
||||
function Decl_Struct *
|
||||
parse_struct(Parser *p, Token *name, AST_Kind struct_kind){
|
||||
Intern_String intern_name = name ? name->intern_val : (Intern_String){0};
|
||||
Token *token = name ? name : token_get(p);
|
||||
Decl *result = decl_struct(p->arena, struct_kind, token, intern_name);
|
||||
Decl_Struct *result = decl_struct(p->arena, struct_kind, token, intern_name);
|
||||
token_expect(p, TK_OpenBrace);
|
||||
do {
|
||||
Note notes = parse_notes(p);
|
||||
Note_List *notes = parse_notes(p);
|
||||
Token *token = token_get(p);
|
||||
if(token_match_keyword(p, keyword_union)){
|
||||
Decl *decl = parse_struct(p, 0, DECL_SubUnion);
|
||||
decl_pass_notes(decl, ¬es);
|
||||
decl_struct_push(result, decl);
|
||||
Decl_Struct *decl = parse_struct(p, 0, AST_Decl_SubUnion);
|
||||
decl->notes = notes;
|
||||
decl_struct_push(result, (AST *)decl);
|
||||
}
|
||||
else if(token_match_keyword(p, keyword_struct)){
|
||||
Decl *decl = parse_struct(p, 0, DECL_SubStruct);
|
||||
decl_pass_notes(decl, ¬es);
|
||||
decl_struct_push(result, decl);
|
||||
Decl_Struct *decl = parse_struct(p, 0, AST_Decl_SubStruct);
|
||||
decl->notes = notes;
|
||||
decl_struct_push(result, (AST *)decl);
|
||||
}
|
||||
else if(token_match(p, TK_Identifier)){
|
||||
if(token_is(p, TK_Colon)){
|
||||
if(token_peek_is_keyword(p, keyword_union, 1)){
|
||||
token_next(p); token_next(p);
|
||||
Decl *decl = parse_struct(p, token, DECL_SubUnion);
|
||||
decl_pass_notes(decl, ¬es);
|
||||
decl_struct_push(result, decl);
|
||||
Decl_Struct *decl = parse_struct(p, token, AST_Decl_SubUnion);
|
||||
decl->notes = notes;
|
||||
decl_struct_push(result, (AST *)decl);
|
||||
}
|
||||
else if(token_peek_is_keyword(p, keyword_struct, 1)){
|
||||
token_next(p); token_next(p);
|
||||
Decl *decl = parse_struct(p, token, DECL_SubStruct);
|
||||
decl_pass_notes(decl, ¬es);
|
||||
decl_struct_push(result, decl);
|
||||
Decl_Struct *decl = parse_struct(p, token, AST_Decl_SubStruct);
|
||||
decl->notes = notes;
|
||||
decl_struct_push(result, (AST *)decl);
|
||||
}
|
||||
else{
|
||||
parse_var_decl(p, result, token, ¬es);
|
||||
parse_var_decl(p, result, token, notes);
|
||||
}
|
||||
}
|
||||
else{
|
||||
parse_var_decl(p, result, token, ¬es);
|
||||
parse_var_decl(p, result, token, notes);
|
||||
}
|
||||
}
|
||||
else{
|
||||
@@ -694,16 +693,16 @@ parse_struct(Parser *p, Token *name, Decl_Kind struct_kind){
|
||||
return result;
|
||||
}
|
||||
|
||||
function Decl *
|
||||
function Decl_Typedef *
|
||||
parse_typedef(Parser *p, Token *name){
|
||||
token_expect(p, TK_Assign);
|
||||
Typespec *typespec = parse_typespec(p);
|
||||
token_expect(p, TK_Semicolon);
|
||||
Decl *result = decl_typedef(p->arena, name, name->intern_val, typespec);
|
||||
Decl_Typedef *result = decl_typedef(p->arena, name, name->intern_val, typespec);
|
||||
return result;
|
||||
}
|
||||
|
||||
function Decl *
|
||||
function Decl_Const *
|
||||
parse_const(Parser *p, Token *name){
|
||||
Typespec *typespec = 0;
|
||||
if(token_match(p, TK_Colon))
|
||||
@@ -711,13 +710,13 @@ parse_const(Parser *p, Token *name){
|
||||
token_expect(p, TK_Assign);
|
||||
Expr *expr = parse_expr(p);
|
||||
token_expect(p, TK_Semicolon);
|
||||
Decl *result = decl_const(p->arena, name, name->intern_val, expr, typespec);
|
||||
Decl_Const *result = decl_const(p->arena, name, name->intern_val, expr, typespec);
|
||||
return result;
|
||||
}
|
||||
|
||||
function Decl *
|
||||
function Decl_Func *
|
||||
parse_function(Parser *p, Token *name){
|
||||
Decl *result = decl_function(p->arena, name, name->intern_val, 0);
|
||||
Decl_Func *result = decl_function(p->arena, name, name->intern_val, 0);
|
||||
if(!token_is(p, TK_CloseParen)){
|
||||
do{
|
||||
name = token_expect(p, TK_Identifier);
|
||||
@@ -727,36 +726,36 @@ parse_function(Parser *p, Token *name){
|
||||
} while(token_match(p, TK_Comma));
|
||||
}
|
||||
token_expect(p, TK_CloseParen);
|
||||
result->func_decl.ret = parse_optional_type(p, intern_void);
|
||||
result->ret = parse_optional_type(p, intern_void);
|
||||
token_expect(p, TK_Semicolon);
|
||||
return result;
|
||||
}
|
||||
|
||||
function B32
|
||||
parse_decl(Parser *p, Decl *parent){
|
||||
parse_decl(Parser *p, AST_Parent *parent){
|
||||
Token *name = 0;
|
||||
Decl *result = 0;
|
||||
Note note = parse_notes(p);
|
||||
AST *result = 0;
|
||||
Note_List *note = parse_notes(p);
|
||||
if((name = token_match(p, TK_Identifier))){
|
||||
if(token_match(p, TK_DoubleColon)){
|
||||
Token *token = 0;
|
||||
if((token = token_match_keyword(p, keyword_enum))){
|
||||
result = parse_enum(p, name);
|
||||
result = (AST *)parse_enum(p, name);
|
||||
}
|
||||
else if((token = token_match_keyword(p, keyword_union))){
|
||||
result = parse_struct(p, name, DECL_Union);
|
||||
result = (AST *)parse_struct(p, name, AST_Decl_Union);
|
||||
}
|
||||
else if((token = token_match_keyword(p, keyword_struct))){
|
||||
result = parse_struct(p, name, DECL_Struct);
|
||||
result = (AST *)parse_struct(p, name, AST_Decl_Struct);
|
||||
}
|
||||
else if((token = token_match_keyword(p, keyword_typedef))){
|
||||
result = parse_typedef(p, name);
|
||||
result = (AST *)parse_typedef(p, name);
|
||||
}
|
||||
else if((token = token_match_keyword(p, keyword_const))){
|
||||
result = parse_const(p, name);
|
||||
result = (AST *)parse_const(p, name);
|
||||
}
|
||||
else if((token = token_match(p, TK_OpenParen))){
|
||||
result = parse_function(p, name);
|
||||
result = (AST *)parse_function(p, name);
|
||||
}
|
||||
else{
|
||||
token = token_get(p);
|
||||
@@ -771,18 +770,18 @@ parse_decl(Parser *p, Decl *parent){
|
||||
}
|
||||
|
||||
if(result){
|
||||
decl_pass_notes(result, ¬e);
|
||||
decl_list_push(parent, result);
|
||||
result->notes = note;
|
||||
ast_push_last(parent, result);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function Decl
|
||||
function Program *
|
||||
parse_decls(Parser *p){
|
||||
Decl decl_list = {.kind=DECL_List};
|
||||
Program *decl_list = ast_program(p->arena, token_get(p));
|
||||
while(!token_is(p, TK_End)){
|
||||
B32 success = parse_decl(p, &decl_list);
|
||||
B32 success = parse_decl(p, decl_list);
|
||||
if(!success){
|
||||
parser_push_error(p, token_get(p), "Failed to parse decls, unexpected token!");
|
||||
}
|
||||
@@ -806,10 +805,10 @@ stmt =
|
||||
|
||||
*/
|
||||
|
||||
function Stmt *
|
||||
function AST_Parent *
|
||||
parse_stmt(Parser *p){
|
||||
Note notes = parse_notes(p);
|
||||
Stmt *result = 0;
|
||||
Note_List *notes = parse_notes(p);
|
||||
AST_Parent *result = 0;
|
||||
if(token_match_keyword(p, keyword_if)){
|
||||
|
||||
}
|
||||
@@ -978,9 +977,9 @@ parse_test_decls(){
|
||||
};
|
||||
for(SizeU i = 0; i < buff_cap(decls); i++){
|
||||
parser_restream(&p, decls[i], lit("Decl_Test"));
|
||||
Decl decl = parse_decls(&p);
|
||||
assert(decl.list_decl.first);
|
||||
decl_print(decl.list_decl.first);
|
||||
Program *decl = parse_decls(&p);
|
||||
assert(decl->first);
|
||||
ast_print((AST *)decl);
|
||||
}
|
||||
arena_end_scratch();
|
||||
}
|
||||
@@ -991,8 +990,8 @@ parse_test_from_file(){
|
||||
Arena *scratch = arena_begin_scratch();
|
||||
String file = os_read_file(scratch, FILENAME);
|
||||
Parser p = parser_make_stream(scratch, file, FILENAME);
|
||||
Decl d = parse_decls(&p);
|
||||
decl_print(&d);
|
||||
Program *d = parse_decls(&p);
|
||||
ast_print((AST *)d);
|
||||
arena_end_scratch();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user