From 5fc7577b5a3bc2c445522dfd5b287e1c6eddc3e9 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sun, 31 Aug 2025 23:33:22 -0400 Subject: Switch to using optional return values for list indexing. --- src/compile/assignments.c | 18 +++++++----------- src/compile/indexing.c | 10 ++-------- 2 files changed, 9 insertions(+), 19 deletions(-) (limited to 'src/compile') diff --git a/src/compile/assignments.c b/src/compile/assignments.c index ab28b972..5fedad9a 100644 --- a/src/compile/assignments.c +++ b/src/compile/assignments.c @@ -92,7 +92,8 @@ Text_t compile_assignment_statement(env_t *env, ast_t *ast) { if (assign->targets && !assign->targets->next) { type_t *lhs_t = get_type(env, assign->targets->ast); if (assign->targets->ast->tag == Index && lhs_t->tag == OptionalType - && value_type(get_type(env, Match(assign->targets->ast, Index)->indexed))->tag == TableType) + && (value_type(get_type(env, Match(assign->targets->ast, Index)->indexed))->tag == TableType + || value_type(get_type(env, Match(assign->targets->ast, Index)->indexed))->tag == ListType)) lhs_t = Match(lhs_t, OptionalType)->type; if (has_stack_memory(lhs_t)) code_err(ast, "Stack references cannot be assigned to " @@ -110,7 +111,8 @@ Text_t compile_assignment_statement(env_t *env, ast_t *ast) { value = value->next, target = target->next) { type_t *lhs_t = get_type(env, target->ast); if (target->ast->tag == Index && lhs_t->tag == OptionalType - && value_type(get_type(env, Match(target->ast, Index)->indexed))->tag == TableType) + && (value_type(get_type(env, Match(target->ast, Index)->indexed))->tag == TableType + || value_type(get_type(env, Match(target->ast, Index)->indexed))->tag == ListType)) lhs_t = Match(lhs_t, OptionalType)->type; if (has_stack_memory(lhs_t)) code_err(ast, "Stack references cannot be assigned to " @@ -166,14 +168,9 @@ Text_t compile_lvalue(env_t *env, ast_t *ast) { ? compile_int_to_type(env, index->index, Type(IntType, .bits = TYPE_IBITS64)) : (index_t->tag == BigIntType ? Texts("Int64$from_int(", compile(env, index->index), ", no)") : Texts("(Int64_t)(", compile(env, index->index), ")")); - if (index->unchecked) { - return Texts("List_lvalue_unchecked(", compile_type(item_type), ", ", target_code, ", ", index_code, - ")"); - } else { - return Texts("List_lvalue(", compile_type(item_type), ", ", target_code, ", ", index_code, ", ", - String((int)(ast->start - ast->file->text)), ", ", - String((int)(ast->end - ast->file->text)), ")"); - } + return Texts("List_lvalue(", compile_type(item_type), ", ", target_code, ", ", index_code, ", ", + String((int)(ast->start - ast->file->text)), ", ", String((int)(ast->end - ast->file->text)), + ")"); } else if (container_t->tag == TableType) { DeclareMatch(table_type, container_t, TableType); if (table_type->default_value) { @@ -184,7 +181,6 @@ Text_t compile_lvalue(env_t *env, ast_t *ast) { compile_to_type(env, table_type->default_value, table_type->value_type), ", ", compile_type_info(container_t), ")"); } - if (index->unchecked) code_err(ast, "Table indexes cannot be unchecked"); return Texts("*(", compile_type(Type(PointerType, table_type->value_type)), ")Table$reserve(", compile_to_pointer_depth(env, index->indexed, 1, false), ", ", compile_to_type(env, index->index, Type(PointerType, table_type->key_type, .is_stack = true)), diff --git a/src/compile/indexing.c b/src/compile/indexing.c index e99feeb2..d2e98388 100644 --- a/src/compile/indexing.c +++ b/src/compile/indexing.c @@ -34,21 +34,15 @@ Text_t compile_indexing(env_t *env, ast_t *ast) { code_err(indexing->index, "Lists can only be indexed by integers, not ", type_to_str(index_t)); type_t *item_type = Match(container_t, ListType)->item_type; Text_t list = compile_to_pointer_depth(env, indexing->indexed, 0, false); - file_t *f = indexing->index->file; Text_t index_code = indexing->index->tag == Int ? compile_int_to_type(env, indexing->index, Type(IntType, .bits = TYPE_IBITS64)) : (index_t->tag == BigIntType ? Texts("Int64$from_int(", compile(env, indexing->index), ", no)") : Texts("(Int64_t)(", compile(env, indexing->index), ")")); - if (indexing->unchecked) - return Texts("List_get_unchecked(", compile_type(item_type), ", ", list, ", ", index_code, ")"); - else - return Texts("List_get(", compile_type(item_type), ", ", list, ", ", index_code, ", ", - String((int64_t)(indexing->index->start - f->text)), ", ", - String((int64_t)(indexing->index->end - f->text)), ")"); + return Texts("List_get(", list, ", ", index_code, ", ", compile_type(item_type), ", value, ", + promote_to_optional(item_type, Text("value")), ", ", compile_none(item_type), ")"); } else if (container_t->tag == TableType) { DeclareMatch(table_type, container_t, TableType); - if (indexing->unchecked) code_err(ast, "Table indexes cannot be unchecked"); if (table_type->default_value) { return Texts("Table$get_or_default(", compile_to_pointer_depth(env, indexing->indexed, 0, false), ", ", compile_type(table_type->key_type), ", ", compile_type(table_type->value_type), ", ", -- cgit v1.2.3