From c62c3200c7078f58b218def14c54bbfd26677069 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sun, 24 Aug 2025 15:32:17 -0400 Subject: Pluralize filenames --- src/compile/enums.c | 2 +- src/compile/list.c | 61 -------------------------------------------------- src/compile/list.h | 5 ----- src/compile/lists.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/compile/lists.h | 5 +++++ src/compile/pointer.c | 49 ---------------------------------------- src/compile/pointer.h | 6 ----- src/compile/pointers.c | 48 +++++++++++++++++++++++++++++++++++++++ src/compile/pointers.h | 6 +++++ src/compile/structs.c | 2 +- 10 files changed, 122 insertions(+), 123 deletions(-) delete mode 100644 src/compile/list.c delete mode 100644 src/compile/list.h create mode 100644 src/compile/lists.c create mode 100644 src/compile/lists.h delete mode 100644 src/compile/pointer.c delete mode 100644 src/compile/pointer.h create mode 100644 src/compile/pointers.c create mode 100644 src/compile/pointers.h (limited to 'src/compile') diff --git a/src/compile/enums.c b/src/compile/enums.c index c28aafae..f09d41ba 100644 --- a/src/compile/enums.c +++ b/src/compile/enums.c @@ -9,7 +9,7 @@ #include "../stdlib/tables.h" #include "../stdlib/text.h" #include "../typecheck.h" -#include "pointer.h" +#include "pointers.h" #include "structs.h" Text_t compile_enum_typeinfo(env_t *env, ast_t *ast) { diff --git a/src/compile/list.c b/src/compile/list.c deleted file mode 100644 index 01036cda..00000000 --- a/src/compile/list.c +++ /dev/null @@ -1,61 +0,0 @@ -// Compilation logic for lists - -#include -#include -#include -#include - -#include "../ast.h" -#include "../compile.h" -#include "../config.h" -#include "../environment.h" -#include "../stdlib/text.h" -#include "../stdlib/util.h" - -static ast_t *add_to_list_comprehension(ast_t *item, ast_t *subject) { - return WrapAST(item, MethodCall, .name = "insert", .self = subject, .args = new (arg_ast_t, .value = item)); -} - -public -Text_t compile_typed_list(env_t *env, ast_t *ast, type_t *list_type) { - DeclareMatch(list, ast, List); - if (!list->items) return Text("(List_t){.length=0}"); - - type_t *item_type = Match(list_type, ListType)->item_type; - - int64_t n = 0; - for (ast_list_t *item = list->items; item; item = item->next) { - ++n; - if (item->ast->tag == Comprehension) goto list_comprehension; - } - - { - env_t *scope = item_type->tag == EnumType ? with_enum_scope(env, item_type) : env; - if (is_incomplete_type(item_type)) code_err(ast, "This list's type can't be inferred!"); - Text_t code = Texts("TypedListN(", compile_type(item_type), ", ", String(n)); - for (ast_list_t *item = list->items; item; item = item->next) { - code = Texts(code, ", ", compile_to_type(scope, item->ast, item_type)); - } - return Texts(code, ")"); - } - -list_comprehension: { - env_t *scope = item_type->tag == EnumType ? with_enum_scope(env, item_type) : fresh_scope(env); - static int64_t comp_num = 1; - const char *comprehension_name = String("list$", comp_num++); - ast_t *comprehension_var = - LiteralCode(Texts("&", comprehension_name), .type = Type(PointerType, .pointed = list_type, .is_stack = true)); - Closure_t comp_action = {.fn = add_to_list_comprehension, .userdata = comprehension_var}; - scope->comprehension_action = &comp_action; - Text_t code = Texts("({ List_t ", comprehension_name, " = {};"); - // set_binding(scope, comprehension_name, list_type, comprehension_name); - for (ast_list_t *item = list->items; item; item = item->next) { - if (item->ast->tag == Comprehension) code = Texts(code, "\n", compile_statement(scope, item->ast)); - else code = Texts(code, compile_statement(env, add_to_list_comprehension(item->ast, comprehension_var))); - } - code = Texts(code, " ", comprehension_name, "; })"); - return code; -} -} - -// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/src/compile/list.h b/src/compile/list.h deleted file mode 100644 index b9bf74d6..00000000 --- a/src/compile/list.h +++ /dev/null @@ -1,5 +0,0 @@ -#include "../ast.h" -#include "../environment.h" -#include "../stdlib/datatypes.h" - -Text_t compile_typed_list(env_t *env, ast_t *ast, type_t *list_type); diff --git a/src/compile/lists.c b/src/compile/lists.c new file mode 100644 index 00000000..01036cda --- /dev/null +++ b/src/compile/lists.c @@ -0,0 +1,61 @@ +// Compilation logic for lists + +#include +#include +#include +#include + +#include "../ast.h" +#include "../compile.h" +#include "../config.h" +#include "../environment.h" +#include "../stdlib/text.h" +#include "../stdlib/util.h" + +static ast_t *add_to_list_comprehension(ast_t *item, ast_t *subject) { + return WrapAST(item, MethodCall, .name = "insert", .self = subject, .args = new (arg_ast_t, .value = item)); +} + +public +Text_t compile_typed_list(env_t *env, ast_t *ast, type_t *list_type) { + DeclareMatch(list, ast, List); + if (!list->items) return Text("(List_t){.length=0}"); + + type_t *item_type = Match(list_type, ListType)->item_type; + + int64_t n = 0; + for (ast_list_t *item = list->items; item; item = item->next) { + ++n; + if (item->ast->tag == Comprehension) goto list_comprehension; + } + + { + env_t *scope = item_type->tag == EnumType ? with_enum_scope(env, item_type) : env; + if (is_incomplete_type(item_type)) code_err(ast, "This list's type can't be inferred!"); + Text_t code = Texts("TypedListN(", compile_type(item_type), ", ", String(n)); + for (ast_list_t *item = list->items; item; item = item->next) { + code = Texts(code, ", ", compile_to_type(scope, item->ast, item_type)); + } + return Texts(code, ")"); + } + +list_comprehension: { + env_t *scope = item_type->tag == EnumType ? with_enum_scope(env, item_type) : fresh_scope(env); + static int64_t comp_num = 1; + const char *comprehension_name = String("list$", comp_num++); + ast_t *comprehension_var = + LiteralCode(Texts("&", comprehension_name), .type = Type(PointerType, .pointed = list_type, .is_stack = true)); + Closure_t comp_action = {.fn = add_to_list_comprehension, .userdata = comprehension_var}; + scope->comprehension_action = &comp_action; + Text_t code = Texts("({ List_t ", comprehension_name, " = {};"); + // set_binding(scope, comprehension_name, list_type, comprehension_name); + for (ast_list_t *item = list->items; item; item = item->next) { + if (item->ast->tag == Comprehension) code = Texts(code, "\n", compile_statement(scope, item->ast)); + else code = Texts(code, compile_statement(env, add_to_list_comprehension(item->ast, comprehension_var))); + } + code = Texts(code, " ", comprehension_name, "; })"); + return code; +} +} + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/src/compile/lists.h b/src/compile/lists.h new file mode 100644 index 00000000..b9bf74d6 --- /dev/null +++ b/src/compile/lists.h @@ -0,0 +1,5 @@ +#include "../ast.h" +#include "../environment.h" +#include "../stdlib/datatypes.h" + +Text_t compile_typed_list(env_t *env, ast_t *ast, type_t *list_type); diff --git a/src/compile/pointer.c b/src/compile/pointer.c deleted file mode 100644 index 50c267b6..00000000 --- a/src/compile/pointer.c +++ /dev/null @@ -1,49 +0,0 @@ -// Compilation logic -#include -#include -#include -#include - -#include "../ast.h" -#include "../compile.h" -#include "../config.h" -#include "../environment.h" -#include "../stdlib/text.h" -#include "../typecheck.h" -#include "list.h" - -Text_t compile_to_pointer_depth(env_t *env, ast_t *ast, int64_t target_depth, bool needs_incref) { - Text_t val = compile(env, ast); - type_t *t = get_type(env, ast); - int64_t depth = 0; - for (type_t *tt = t; tt->tag == PointerType; tt = Match(tt, PointerType)->pointed) - ++depth; - - // Passing a literal value won't trigger an incref, because it's ephemeral, - // e.g. [10, 20].reversed() - if (t->tag != PointerType && needs_incref && !can_be_mutated(env, ast)) needs_incref = false; - - while (depth != target_depth) { - if (depth < target_depth) { - if (ast->tag == Var && target_depth == 1) val = Texts("(&", val, ")"); - else code_err(ast, "This should be a pointer, not ", type_to_str(get_type(env, ast))); - t = Type(PointerType, .pointed = t, .is_stack = true); - ++depth; - } else { - DeclareMatch(ptr, t, PointerType); - val = Texts("*(", val, ")"); - t = ptr->pointed; - --depth; - } - } - - while (t->tag == PointerType) { - DeclareMatch(ptr, t, PointerType); - t = ptr->pointed; - } - - if (needs_incref && t->tag == ListType) val = Texts("LIST_COPY(", val, ")"); - else if (needs_incref && (t->tag == TableType || t->tag == SetType)) val = Texts("TABLE_COPY(", val, ")"); - - return val; -} diff --git a/src/compile/pointer.h b/src/compile/pointer.h deleted file mode 100644 index 306c3019..00000000 --- a/src/compile/pointer.h +++ /dev/null @@ -1,6 +0,0 @@ -#include - -#include "../environment.h" -#include "../stdlib/datatypes.h" - -Text_t compile_to_pointer_depth(env_t *env, ast_t *ast, int64_t target_depth, bool needs_incref); diff --git a/src/compile/pointers.c b/src/compile/pointers.c new file mode 100644 index 00000000..cb11844d --- /dev/null +++ b/src/compile/pointers.c @@ -0,0 +1,48 @@ +// Compilation logic +#include +#include +#include +#include + +#include "../ast.h" +#include "../compile.h" +#include "../config.h" +#include "../environment.h" +#include "../stdlib/text.h" +#include "../typecheck.h" + +Text_t compile_to_pointer_depth(env_t *env, ast_t *ast, int64_t target_depth, bool needs_incref) { + Text_t val = compile(env, ast); + type_t *t = get_type(env, ast); + int64_t depth = 0; + for (type_t *tt = t; tt->tag == PointerType; tt = Match(tt, PointerType)->pointed) + ++depth; + + // Passing a literal value won't trigger an incref, because it's ephemeral, + // e.g. [10, 20].reversed() + if (t->tag != PointerType && needs_incref && !can_be_mutated(env, ast)) needs_incref = false; + + while (depth != target_depth) { + if (depth < target_depth) { + if (ast->tag == Var && target_depth == 1) val = Texts("(&", val, ")"); + else code_err(ast, "This should be a pointer, not ", type_to_str(get_type(env, ast))); + t = Type(PointerType, .pointed = t, .is_stack = true); + ++depth; + } else { + DeclareMatch(ptr, t, PointerType); + val = Texts("*(", val, ")"); + t = ptr->pointed; + --depth; + } + } + + while (t->tag == PointerType) { + DeclareMatch(ptr, t, PointerType); + t = ptr->pointed; + } + + if (needs_incref && t->tag == ListType) val = Texts("LIST_COPY(", val, ")"); + else if (needs_incref && (t->tag == TableType || t->tag == SetType)) val = Texts("TABLE_COPY(", val, ")"); + + return val; +} diff --git a/src/compile/pointers.h b/src/compile/pointers.h new file mode 100644 index 00000000..306c3019 --- /dev/null +++ b/src/compile/pointers.h @@ -0,0 +1,6 @@ +#include + +#include "../environment.h" +#include "../stdlib/datatypes.h" + +Text_t compile_to_pointer_depth(env_t *env, ast_t *ast, int64_t target_depth, bool needs_incref); diff --git a/src/compile/structs.c b/src/compile/structs.c index d7cbcddf..a5889ff8 100644 --- a/src/compile/structs.c +++ b/src/compile/structs.c @@ -8,7 +8,7 @@ #include "../stdlib/tables.h" #include "../stdlib/text.h" #include "../typecheck.h" -#include "pointer.h" +#include "pointers.h" public Text_t compile_struct_typeinfo(env_t *env, type_t *t, const char *name, arg_ast_t *fields, bool is_secret, -- cgit v1.2.3