feat: 列表推导式 [for x in arr: expr] — parser+sema+codegen

AST(29): +AST_LIST_COMP, parser 解析 [for var in expr: body]
sema: 创建子作用域注册循环变量, codegen: for 循环绑定+填充结果数组
已知限制: 仅支持 2 元素及以下数组 (大数组 alloca 对齐问题待修)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-06-07 13:58:54 +08:00
parent 0088347576
commit 443b22bdf1
6 changed files with 128 additions and 0 deletions
+4
View File
@@ -16,6 +16,7 @@ typedef enum {
AST_RETURN_STMT,
AST_EXPR_STMT,
AST_DEFER_STMT,
AST_LIST_COMP, // [for x in expr: body]
AST_BINARY_EXPR,
AST_UNARY_EXPR,
AST_CALL_EXPR,
@@ -91,6 +92,8 @@ struct AstNode {
struct { struct AstNode* expr; } expr_stmt;
// AST_DEFER_STMT
struct { struct AstNode* body; } defer_stmt;
// AST_LIST_COMP
struct { const char* var_name; struct AstNode* array; struct AstNode* map_expr; } list_comp;
// AST_BINARY_EXPR
struct { BinaryOp op; struct AstNode* left; struct AstNode* right; } binary;
// AST_UNARY_EXPR
@@ -157,6 +160,7 @@ AstNode* ast_make_while(void* alloc, AstNode* cond, AstNode* body, SourceLoc loc
AstNode* ast_make_return(void* alloc, AstNode* expr, SourceLoc loc);
AstNode* ast_make_expr_stmt(void* alloc, AstNode* expr, SourceLoc loc);
AstNode* ast_make_defer_stmt(void* alloc, AstNode* expr, SourceLoc loc);
AstNode* ast_make_list_comp(void* alloc, const char* var, AstNode* arr, AstNode* map, SourceLoc loc);
AstNode* ast_make_binary(void* alloc, BinaryOp op, AstNode* left, AstNode* right, SourceLoc loc);
AstNode* ast_make_unary(void* alloc, BinaryOp op, AstNode* operand, SourceLoc loc);
AstNode* ast_make_call(void* alloc, const char* name, AstNode** args, const char** arg_names, size_t count, SourceLoc loc);