From 69d41fa068a08fcb468bd50babc43178faf0e114 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sun, 18 Feb 2024 14:53:52 -0500 Subject: Add source information to runtime errors --- compile.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'compile.c') diff --git a/compile.c b/compile.c index 6b38a9ba..a0e613a4 100644 --- a/compile.c +++ b/compile.c @@ -771,8 +771,14 @@ CORD compile(env_t *env, ast_t *ast) type_t *item_type = Match(container_t, ArrayType)->item_type; CORD arr = compile_to_pointer_depth(env, indexing->indexed, 1); CORD index = compile(env, indexing->index); - return CORD_all(indexing->unchecked ? "$Array_get_unchecked" : "$Array_get(", - compile_type(item_type), ", ", arr, ", ", index, ")"); + file_t *f = indexing->index->file; + if (indexing->unchecked) + return CORD_all("$Array_get_unchecked", compile_type(item_type), ", ", arr, ", ", index, ")"); + else + return CORD_all("$Array_get(", compile_type(item_type), ", ", arr, ", ", index, ", ", + Str__quoted(f->filename, false), ", ", CORD_asprintf("%ld", (int64_t)(indexing->index->start - f->text)), ", ", + CORD_asprintf("%ld", (int64_t)(indexing->index->end - f->text)), + ")"); } case TableType: { type_t *key_t = Match(container_t, TableType)->key_type; @@ -781,8 +787,12 @@ CORD compile(env_t *env, ast_t *ast) code_err(indexing->index, "This value has type %T, but this table can only be index with keys of type %T", index_t, key_t); CORD table = compile_to_pointer_depth(env, indexing->indexed, 1); CORD key = compile(env, indexing->index); + file_t *f = indexing->index->file; return CORD_all("$Table_get(", table, ", ", compile_type(key_t), ", ", compile_type(value_t), ", ", - key, ", ", compile_type_info(env, container_t), ")"); + key, ", ", compile_type_info(env, container_t), ", ", + Str__quoted(f->filename, false), ", ", CORD_asprintf("%ld", (int64_t)(indexing->index->start - f->text)), ", ", + CORD_asprintf("%ld", (int64_t)(indexing->index->end - f->text)), + ")"); } default: code_err(ast, "Indexing is not supported for type: %T", container_t); } -- cgit v1.2.3