Struct field access. Damn, I coded for like 30 minutes and then it just worked on the first try

This commit is contained in:
Krzosa Karol
2022-05-30 18:27:06 +02:00
parent 0e0b95ab52
commit 980a3b68b9
6 changed files with 186 additions and 41 deletions

View File

@@ -217,18 +217,20 @@ struct Ast_Named:Ast{
Intern_String name;
};
struct Ast_Resolved_Type;
struct Ast_Struct: Ast_Expr{
// Required to be Ast_Struct or Ast_Var or Ast_Const
Array<Ast_Named *> members;
Ast_Resolved_Type *type;
};
struct Ast_Var: Ast_Named{
Ast_Expr *typespec;
Ast_Expr *expr;
};
struct Ast_Const;
struct Ast_Resolved_Type;
struct Ast_Struct: Ast_Expr{
// Required to be Ast_Struct or Ast_Var or Ast_Const
Array<Ast_Var *> members;
Array<Ast_Const *> const_members;
Ast_Resolved_Type *type;
};
struct Ast_Const: Ast_Named{
union{
Ast *ast;
@@ -281,7 +283,7 @@ ast_expr_binary(Ast_Expr *left, Ast_Expr *right, Token *op){
result->left = left;
result->right = right;
result->left->parent = result;
result->right->parent = result;
if(result->right) result->right->parent = result;
return result;
}
@@ -426,12 +428,18 @@ ast_array(Token *pos, Ast_Expr *expr){
}
function Ast_Struct *
ast_struct(Token *pos, Array<Ast_Named *> members){
ast_struct(Token *pos, Array<Ast_Var *> members, Array<Ast_Const *> const_members){
AST_NEW(Struct, STRUCT, pos, AST_AGGREGATE);
result->members = members.tight_copy(pctx->perm);
result->const_members = const_members.tight_copy(pctx->perm);
For(result->members) {
assert(is_flag_set(it->flags, AST_BINDING));
assert(it->kind == AST_VAR || it->kind == AST_CONST);
assert(it->kind == AST_VAR);
it->parent = result;
}
For(result->const_members) {
assert(is_flag_set(it->flags, AST_BINDING));
assert(it->kind == AST_CONST);
it->parent = result;
}
return result;
@@ -525,3 +533,40 @@ ast_is_struct(Ast *ast){
}
return false;
}
function B32
is_ident(Ast *ast){
B32 result = ast->kind == AST_IDENT;
return result;
}
function B32
is_binary(Ast *ast){
B32 result = ast->kind == AST_BINARY;
return result;
}
function B32
is_atom(Ast *ast){
B32 result = is_flag_set(ast->flags, AST_ATOM);
return result;
}
function Ast *
query_struct(Ast_Struct *agg, Intern_String string){
For(agg->members){
if(it->name == string){
return it;
}
}
For(agg->const_members){
if(it->name == string){
return it;
}
}
return 0;
}