diff --git a/ast.cpp b/ast.cpp index 2a1982b..8882256 100644 --- a/ast.cpp +++ b/ast.cpp @@ -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 exprs; }; +struct Ast_Var_Unpack: Ast_Expr{ + Array 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 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 //----------------------------------------------------------------------------- diff --git a/main.cpp b/main.cpp index 8f278e6..ec47dc4 100644 --- a/main.cpp +++ b/main.cpp @@ -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 diff --git a/parsing.cpp b/parsing.cpp index b5caee9..866ff53 100644 --- a/parsing.cpp +++ b/parsing.cpp @@ -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 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)); } diff --git a/types.h b/types.h index e501738..2bc50d6 100644 --- a/types.h +++ b/types.h @@ -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;}