commit cf7cf45038e0ed96b310481014e19260d7d44605
parent ba49954d82f30aee8a68100f21cfa62f017e5d8a
Author: Brian Swetland <swetland@frotz.net>
Date: Fri, 13 Oct 2023 22:57:00 -0700
compiler0: tweaks to and test for str type
- allow str to be derefernced like an array
- test for str
Diffstat:
3 files changed, 53 insertions(+), 1 deletion(-)
diff --git a/compiler0.c b/compiler0.c
@@ -945,7 +945,7 @@ void parse_ident(void) {
} else if (ctx.tok == tOBRACK) { // array access
next();
// XXX handle slices
- if (sym->type->kind != TYPE_ARRAY) {
+ if ((sym->type->kind != TYPE_ARRAY) && (sym->type->kind != TYPE_STR)) {
error("cannot access '%s' as an array", name->text);
}
emit_impl("($%s[", name->text);
diff --git a/test/1043-str.log b/test/1043-str.log
@@ -0,0 +1,21 @@
+D 00000048
+D 00000065
+D 0000006c
+D 0000006c
+D 0000006f
+D 00000061
+D 00000061
+D 00000061
+D 00000062
+D 00000062
+D 00000062
+D 00000063
+D 00000063
+D 00000063
+D 00000064
+D 00000064
+D 00000064
+D 00000065
+D 00000065
+D 00000065
+X 00000005
diff --git a/test/1043-str.spl b/test/1043-str.spl
@@ -0,0 +1,31 @@
+
+var hello str = "Hello";
+
+func strlen(x str) i32 {
+ var n i32 = 0;
+ while x[n] != 0 {
+ n++;
+ }
+ return n;
+}
+
+func dump(x str, xlen i32) {
+ var n i32 = 0;
+ while n < xlen {
+ _hexout_(x[n]);
+ n++;
+ }
+}
+
+var strtab [5]str = { "aaa", "bbb", "xxx", "ddd", "eee" };
+
+func start() i32 {
+ dump(hello, strlen(hello));
+ var n i32 = 0;
+ strtab[2] = "ccc";
+ while n < 5 {
+ dump(strtab[n], strlen(strtab[n]));
+ n++;
+ }
+ return strlen(hello);
+}