aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-12-26 17:31:33 -0500
committerBruce Hill <bruce@bruce-hill.com>2024-12-26 17:31:33 -0500
commitacd3f2d39edb2cc306632a19a1eb4b7b0ba02836 (patch)
tree60a05a85ee2ff987991da84666ba941edde3f3af
parent32cbf7b7c8d9634863af6be9defc8a02c780ff82 (diff)
Add text indexing
-rw-r--r--compile.c2
-rw-r--r--test/text.tm4
-rw-r--r--typecheck.c2
3 files changed, 8 insertions, 0 deletions
diff --git a/compile.c b/compile.c
index 95b9ba1b..d4f8d4ab 100644
--- a/compile.c
+++ b/compile.c
@@ -3700,6 +3700,8 @@ CORD compile(env_t *env, ast_t *ast)
} else {
code_err(indexing->index, "This table doesn't have a value type or a default value");
}
+ } else if (container_t->tag == TextType) {
+ return CORD_all("Text$cluster(", compile_to_pointer_depth(env, indexing->indexed, 0, false), ", ", compile_to_type(env, indexing->index, Type(BigIntType)), ")");
} else {
code_err(ast, "Indexing is not supported for type: %T", container_t);
}
diff --git a/test/text.tm b/test/text.tm
index a95764df..13ee2a3b 100644
--- a/test/text.tm
+++ b/test/text.tm
@@ -8,7 +8,11 @@ func main():
= "hello amélie!"
>> str:lower():title()
= "Hello Amélie!"
+ >> str[1]
+ = "H"
+ >> str[9]
+ = "é"
>> \UE9
= "é"
diff --git a/typecheck.c b/typecheck.c
index 2b5dd701..0bca1e3a 100644
--- a/typecheck.c
+++ b/typecheck.c
@@ -779,6 +779,8 @@ type_t *get_type(env_t *env, ast_t *ast)
return Type(OptionalType, table_type->value_type);
else
code_err(indexing->indexed, "This type doesn't have a value type or a default value");
+ } else if (value_t->tag == TextType) {
+ return value_t;
} else {
code_err(ast, "I don't know how to index %T values", indexed_t);
}