Cleanup, There is no decl anymore, Ast_Named

This commit is contained in:
Krzosa Karol
2022-05-26 20:21:24 +02:00
parent ec773c08be
commit 0e398c84b6
7 changed files with 98 additions and 80 deletions

View File

@@ -177,7 +177,22 @@ parse_optional_type(){
return result;
}
function Ast_Decl *parse_decl(B32);
function Ast_Init *
parse_init_stmt(Ast_Expr *expr){
Token *token = token_get();
if(token_match(TK_Colon)){
if(expr->kind != AST_IDENT) parsing_error(token, "Failed to parse init stmt, expected left of [:] to be a token of kind [Identifier]");
if(token_match(TK_Assign)){
Ast_Expr *value = parse_expr();
Ast_Init *result = ast_init(token, TK_Comma, (Ast_Atom *)expr, value);
return result;
}
else not_implemented;
}
return 0;
}
function Ast_Named *parse_named(B32);
function Ast_Block *
parse_block(){
Ast_Block *block = 0;
@@ -195,24 +210,11 @@ parse_block(){
}
else if(token_match_keyword(keyword_if)){
Array<Ast_If_Node *> if_nodes = {scratch};
Ast_Expr *expr = parse_expr();
Ast_Init *init_val = 0;
{
Token *token = token_get();
if(token_match(TK_Colon)){
if(expr->kind != AST_IDENT) parsing_error(token, "Failed to parse init stmt, expected left of [:] to be a token of kind [Identifier]");
if(token_match(TK_Assign)){
Ast_Expr *value = parse_expr();
assert(expr->kind == AST_IDENT);
init_val = ast_init(token, TK_Comma, (Ast_Atom *)expr, value);
}
else not_implemented;
if(token_match(TK_Comma)){
expr = parse_expr();
}
}
Ast_Expr *expr = parse_expr();
Ast_Init *init_val = parse_init_stmt(expr);
if(init_val){
if(token_match(TK_Comma)) expr = parse_expr();
else expr = 0;
}
Ast_Block *block = parse_block();
@@ -240,7 +242,7 @@ parse_block(){
}
else{
Ast_Decl *result = parse_decl(false);
Ast_Named *result = parse_named(false);
if(result) stmts.add(result);
else parsing_error(token, "Unexpected token [%s] while parsing statement", token_kind_string(token->kind).str);
}
@@ -416,16 +418,16 @@ parse_assign_expr(){
return result;
}
function Ast_Decl *
parse_decl(B32 is_global){
Ast_Decl *result = 0;
function Ast_Named *
parse_named(B32 is_global){
Ast_Named *result = 0;
if(is_global) token_match(SAME_SCOPE);
if(token_is(TK_Identifier)){
if(is_global && pctx->indent != 0) parsing_error(token_get(), "Top level declarations shouldn't be indented");
Token *name = token_next();
if(token_match(TK_DoubleColon)){ // Constant
Ast_Expr *expr = parse_expr();
result = ast_decl_const(name, name->intern_val, expr);
result = ast_const(name, name->intern_val, expr);
}
else if(token_match(TK_Colon)){
Ast_Typespec *typespec = 0;
@@ -434,7 +436,7 @@ parse_decl(B32 is_global){
if(token_match(TK_Assign)) expr = parse_expr();
if(!expr && !typespec) parsing_error(name, "invalid declaration, no type or value");
result = ast_decl_var(name, typespec, name->intern_val, expr);
result = ast_var(name, typespec, name->intern_val, expr);
}
else{
Token *token = token_get();
@@ -455,16 +457,19 @@ parse_file(){
Scratch scratch;
//
// @note: pop the first token which which always should be an indentation token,
// it updates the indent info on the parser, making sure that indentation on
// @note: pop the first token with token_next() / token_expect()
// which always should be an indentation token,
// it updates the indent info on the parser,
// making sure that indentation on
// the first line is properly updated
//
Token *token = token_get();
Array<Ast_Decl *>decls = {scratch};
Array<Ast_Named *>decls = {scratch};
while(!token_is(TK_End)){
token_expect(SAME_SCOPE);
Ast_Decl *decl = parse_decl(true);
Ast_Named *decl = parse_named(true);
if(!decl) break;
decls.add(decl);
}
Ast_Package *result = ast_package(token, token->file, decls);