Add Var Unpack

This commit is contained in:
Krzosa Karol
2022-06-16 08:56:31 +02:00
parent 185fd8975c
commit 4f0d16e632
4 changed files with 33 additions and 2 deletions

14
ast.cpp
View File

@@ -26,6 +26,7 @@ enum Ast_Kind: U32{
AST_LENGTH_OF,
AST_ALIGN_OF,
AST_VAR_UNPACK,
AST_BREAK,
AST_COMPOUND,
AST_TYPE,
@@ -102,6 +103,11 @@ struct Ast_Call: Ast_Expr{
Array<Ast_Call_Item *> exprs;
};
struct Ast_Var_Unpack: Ast_Expr{
Array<Ast_Decl *> vars;
Ast_Expr *expr;
};
struct Ast_Unary: Ast_Expr{
Token_Kind op;
Ast_Expr *expr;
@@ -554,6 +560,14 @@ ast_alignof(Token *pos, Ast_Expr *expr){
return result;
}
function Ast_Var_Unpack *
ast_var_unpack(Token *pos, Array<Ast_Decl *> vars, Ast_Expr *expr){
AST_NEW(Var_Unpack, VAR_UNPACK, pos, AST_STMT);
result->vars = vars.tight_copy(pctx->perm);
result->expr = expr;
return result;
}
//-----------------------------------------------------------------------------
// Value
//-----------------------------------------------------------------------------

View File

@@ -43,12 +43,12 @@ want to export all the symbols, we can namespace them optionally.
@todo
[ ] - Fix field access, cant cast, cant index
[ ] - Add parent_scope to Ast_Type
[ ] - Should compound resolution use an algorithm to reorder compounds to initialize all fields in order
[ ] - Switch
[ ] - Some way to take slice of data
[ ] - Optional function renaming in codegen
[ ] - Multiple return values
[ ] - Using in structs to embed members, then casting offsets to that embedded member
[ ] - #assert that handles constants at compile time and vars at runtime
[ ] - Comma notation when declaring variables thing1, thing2: S32
[ ] - Array of inferred size
[ ] - Add single line lambda expressions
@@ -72,9 +72,11 @@ want to export all the symbols, we can namespace them optionally.
@donzo
[x] - Add c string
[-] - Should compound resolution use an algorithm to reorder compounds to initialize all fields in order
[x] - slices should be properly displayed in debugger
[x] - Imports inside of import shouldn't spill outside
[x] - Scope
[x] - #assert that handles constants at compile time and vars at runtime
[x] - Hex 0x42
[x] - Rewrite where # happen,
[x] - elif

View File

@@ -315,6 +315,19 @@ parse_stmt_scope(Ast_Scope *scope_defined_outside = 0){
scope->stmts.add(result_if);
}
else if(token_is(TK_Identifier) && token_is(TK_Comma, 1)){
Array<Ast_Decl *> decls = {scratch};
do{
Token *name = token_match(TK_Identifier);
Ast_Decl *decl = ast_var(name, 0, name->intern_val, 0);
decls.add(decl);
}while(token_match(TK_Comma));
token_expect(TK_Assign);
Ast_Expr *expr = parse_expr();
Ast_Var_Unpack *vars = ast_var_unpack(token, decls, expr);
scope->stmts.add(vars);
}
else{
Ast *result = parse_decl(false);
if(!result){
@@ -757,6 +770,7 @@ parse_decl(B32 is_global){
Ast_Expr *expr = parse_expr();
result = ast_var(tname, 0, tname->intern_val, expr);
}
else if(is_global && tname->kind != TK_End){
compiler_error(tname, "Unexpected token: [%s] when parsing a declaration", name(tname->kind));
}

View File

@@ -228,6 +228,7 @@ force_inline B32 is_struct(Ast_Type *a){return a->kind == TYPE_STRUCT;}
force_inline B32 is_lambda(Ast_Type *a){return a->kind == TYPE_LAMBDA;}
force_inline B32 is_array(Ast_Type *a){return a->kind == TYPE_ARRAY;}
force_inline B32 is_slice(Ast_Type *a){return a->kind == TYPE_SLICE;}
force_inline B32 is_tuple(Ast_Type *a){return a->kind == TYPE_TUPLE;}
force_inline B32 is_enum(Ast_Type *a){return a->kind == TYPE_ENUM;}
force_inline B32 is_pointer(Ast_Type *a){return a->kind == TYPE_POINTER;}
force_inline B32 is_string(Ast_Type *a){return a->kind == TYPE_STRING || a->kind == TYPE_UNTYPED_STRING || a == type_pointer_to_char;}