More cleanup

This commit is contained in:
Krzosa Karol
2022-05-31 23:01:55 +02:00
parent 98d2389c9f
commit 86aec0b1eb
2 changed files with 104 additions and 94 deletions

View File

@@ -145,7 +145,7 @@ parse_init_stmt(Ast_Expr *expr){
result->flags = set_flag(result->flags, AST_STMT); result->flags = set_flag(result->flags, AST_STMT);
return result; return result;
} }
return 0; return expr;
} }
function Ast_Call * function Ast_Call *
@@ -215,7 +215,6 @@ parse_block(){
else if(token_match_keyword(keyword_for)){ else if(token_match_keyword(keyword_for)){
Ast_Expr *expr_first = parse_expr(); Ast_Expr *expr_first = parse_expr();
Ast_Expr *init = parse_init_stmt(expr_first); Ast_Expr *init = parse_init_stmt(expr_first);
Ast_Expr *actual_init = init ? init : expr_first;
Ast_Expr *cond = 0; Ast_Expr *cond = 0;
Ast_Expr *iter = 0; Ast_Expr *iter = 0;
@@ -223,18 +222,19 @@ parse_block(){
cond = parse_expr(); cond = parse_expr();
if(token_match(TK_Comma)){ if(token_match(TK_Comma)){
iter = parse_expr(); iter = parse_expr();
iter = parse_init_stmt(iter);
} }
} }
Ast_Block *for_block = parse_block(); Ast_Block *for_block = parse_block();
stmts.add(ast_for(token, actual_init, cond, iter, for_block)); stmts.add(ast_for(token, init, cond, iter, for_block));
} }
else if(token_match_keyword(keyword_if)){ else if(token_match_keyword(keyword_if)){
Array<Ast_If_Node *> if_nodes = {scratch}; Array<Ast_If_Node *> if_nodes = {scratch};
Ast_Expr *expr = parse_expr(); Ast_Expr *expr = parse_expr();
Ast_Expr *init_val = parse_init_stmt(expr); Ast_Expr *init_val = parse_init_stmt(expr);
if(init_val){ if(init_val != expr){
if(token_match(TK_Comma)) expr = parse_expr(); if(token_match(TK_Comma)) expr = parse_expr();
else expr = 0; else expr = 0;
} }

View File

@@ -85,10 +85,10 @@ resolve_stmt(Ast *ast, Ast_Resolved_Type *ret){
CASE(FOR, For){ CASE(FOR, For){
// @todo: I think we need to bring back the Ast_Init, it was not an expression // @todo: I think we need to bring back the Ast_Init, it was not an expression
resolve_stmt(node->init, ret); resolve_expr(node->init, ret);
Operand cond = resolve_expr(node->cond); // @todo: typechecking Operand cond = resolve_expr(node->cond); // @todo: typechecking
Operand iter = resolve_expr(node->iter); resolve_expr(node->iter, ret);
unused(cond); unused(iter); unused(cond);
resolve_stmt_block(node->block, ret); resolve_stmt_block(node->block, ret);
BREAK(); BREAK();
} }
@@ -430,100 +430,110 @@ resolve_expr(Ast_Expr *ast, Ast_Resolved_Type *expected_type, Sym *lambda_to_res
CASE(BINARY, Binary){ CASE(BINARY, Binary){
Operand result = {}; Operand result = {};
if(node->op == TK_Dot){ switch(node->op){
B32 required_to_be_const = false; case TK_ColonAssign:{
// @note: resolve first chunk which involves querying global map assert(is_flag_set(node->flags, AST_STMT));
// second part requires searching through a struct // Operand left = resolve_expr(node->left); // needs to be lvalue
// resolve.x.y Operand right = resolve_expr(node->right);
Operand resolved_ident = resolve_expr(node->left); assert(node->left->kind == AST_IDENT);
Ast_Resolved_Type *type = resolved_ident.type; Ast_Atom *atom = (Ast_Atom *)node->left; // @todo use left operand
if(type == type_type){ Sym *sym = sym_new_resolved(SYM_VAR, atom->intern_val, right.type, right.value, node);
type = resolved_ident.type_val; sym_insert(sym);
required_to_be_const = true; }break;
} case TK_Dot: {
// @copy_paste B32 required_to_be_const = false;
if(is_pointer(type)) type = type->base; // @note: resolve first chunk which involves querying global map
type_complete(type); // second part requires searching through a struct
if(!(is_struct(type) || is_enum(type))) parsing_error(node->pos, "Trying to access inside a value that is not a struct or enum"); // resolve.x.y
sym_new_resolved(SYM_VAR, {}, resolved_ident.type, {}, node->left); Operand resolved_ident = resolve_expr(node->left);
Ast_Resolved_Type *type = resolved_ident.type;
// This happens only on binary nodes which further chain with dots and require lookups if(type == type_type){
// This part cant happen on enums type = resolved_ident.type_val;
// x.resolve.y required_to_be_const = true;
Ast_Binary *binary = (Ast_Binary *)node->right; }
for(;!is_ident(binary); binary=(Ast_Binary *)binary->right){
assert(is_ident(binary->left));
Ast_Atom *ident = (Ast_Atom *)binary->left;
assert(is_binary(binary));
Ast_Struct *agg = (Ast_Struct *)type->ast;
Ast *query = query_struct(agg, ident->intern_val);
if(query){
Sym *sym = resolved_get(query);
if(required_to_be_const && sym->kind != SYM_CONST) parsing_error(ident->pos, "Required to be constant");
type = sym->type;
// @copy_paste // @copy_paste
if(type == type_type){ if(is_pointer(type)) type = type->base;
required_to_be_const = true; type_complete(type);
type = sym->type_val; if(!(is_struct(type) || is_enum(type))) parsing_error(node->pos, "Trying to access inside a value that is not a struct or enum");
} sym_new_resolved(SYM_VAR, {}, resolved_ident.type, {}, node->left);
if(is_pointer(type)) type = type->base;
type_complete(type);
if(!(is_struct(type) || is_enum(type))) parsing_error(node->pos, "Trying to access inside a value that is not a struct or enum");
sym_associate(ident, sym);
} else parsing_error(ident->pos, "No such member in struct"); // This happens only on binary nodes which further chain with dots and require lookups
} // This part cant happen on enums
// x.resolve.y
Ast_Binary *binary = (Ast_Binary *)node->right;
for(;!is_ident(binary); binary=(Ast_Binary *)binary->right){
assert(is_ident(binary->left));
Ast_Atom *ident = (Ast_Atom *)binary->left;
assert(is_binary(binary));
// Here we can resolve the last part, this doesnt need to be a struct Ast_Struct *agg = (Ast_Struct *)type->ast;
// x.y.resolve Ast *query = query_struct(agg, ident->intern_val);
// @copy_paste if(query){
Ast_Atom *ident = (Ast_Atom *)binary; Sym *sym = resolved_get(query);
if(is_enum(type)){ if(required_to_be_const && sym->kind != SYM_CONST) parsing_error(ident->pos, "Required to be constant");
Ast_Enum *enu = (Ast_Enum *)type->ast; type = sym->type;
Ast_Enum_Member *query = query_enum(enu, ident->intern_val); // @copy_paste
if(query){ if(type == type_type){
Sym *resolved = resolved_get(query); required_to_be_const = true;
assert(resolved); type = sym->type_val;
rewrite_into_const(node, Ast_Binary, resolved); }
result = operand(resolved); if(is_pointer(type)) type = type->base;
type_complete(type);
if(!(is_struct(type) || is_enum(type))) parsing_error(node->pos, "Trying to access inside a value that is not a struct or enum");
sym_associate(ident, sym);
} else parsing_error(ident->pos, "No such member in struct");
} }
}
else if(is_struct(type)){
Ast_Struct *agg = (Ast_Struct *)type->ast;
Ast *query = query_struct(agg, ident->intern_val);
if(query){
Sym *sym = resolved_get(query);
result = operand(sym);
assert(sym);
if(sym->kind == SYM_CONST) rewrite_into_const(node, Ast_Binary, sym);
else sym_associate(ident, sym);
} else parsing_error(ident->pos, "No such member in struct"); // Here we can resolve the last part, this doesnt need to be a struct
} // x.y.resolve
else parsing_error(ident->pos, "Trying to [.] access a value that is not [Enum] or [Struct]"); // @copy_paste
Ast_Atom *ident = (Ast_Atom *)binary;
if(result.is_const == false && required_to_be_const){ if(is_enum(type)){
invalid_codepath; Ast_Enum *enu = (Ast_Enum *)type->ast;
} Ast_Enum_Member *query = query_enum(enu, ident->intern_val);
if(query){
} Sym *resolved = resolved_get(query);
else{ assert(resolved);
Operand left = resolve_expr(node->left); rewrite_into_const(node, Ast_Binary, resolved);
Operand right = resolve_expr(node->right); result = operand(resolved);
result.type = resolve_type_pair(node->pos, left.type, right.type);
if(left.is_const && right.is_const){
result.is_const = true;
if(result.type == type_int){
switch(node->op){
case TK_Add: result.int_val = left.int_val + right.int_val; break;
case TK_Sub: result.int_val = left.int_val - right.int_val; break;
case TK_Mul: result.int_val = left.int_val * right.int_val; break;
case TK_Div: result.int_val = left.int_val / right.int_val; break;
invalid_default_case;
} }
} }
else parsing_error(node->pos, "Arithmetic on type [%s] is not supported", type_names[result.type->kind]); else if(is_struct(type)){
Ast_Struct *agg = (Ast_Struct *)type->ast;
Ast *query = query_struct(agg, ident->intern_val);
if(query){
Sym *sym = resolved_get(query);
result = operand(sym);
assert(sym);
if(sym->kind == SYM_CONST) rewrite_into_const(node, Ast_Binary, sym);
else sym_associate(ident, sym);
} else parsing_error(ident->pos, "No such member in struct");
}
else parsing_error(ident->pos, "Trying to [.] access a value that is not [Enum] or [Struct]");
if(result.is_const == false && required_to_be_const){
invalid_codepath;
}
} break;
default: {
Operand left = resolve_expr(node->left);
Operand right = resolve_expr(node->right);
result.type = resolve_type_pair(node->pos, left.type, right.type);
if(left.is_const && right.is_const){
result.is_const = true;
if(result.type == type_int){
switch(node->op){
case TK_Add: result.int_val = left.int_val + right.int_val; break;
case TK_Sub: result.int_val = left.int_val - right.int_val; break;
case TK_Mul: result.int_val = left.int_val * right.int_val; break;
case TK_Div: result.int_val = left.int_val / right.int_val; break;
invalid_default_case;
}
}
else parsing_error(node->pos, "Arithmetic on type [%s] is not supported", type_names[result.type->kind]);
}break;
} }
} }