From b7e423adc2155b44f5a2c514bfaca2db3f2ad5dd Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Fri, 10 Sep 2021 19:44:27 +0200 Subject: [PATCH] Update .len for subarray to not require () --- resources/examples/base64.c3 | 10 +++++----- resources/examples/fasta.c3 | 6 +++--- resources/examples/nbodies.c3 | 4 ++-- resources/examples/notworking/levenshtein.c3 | 4 ++-- resources/testfragments/demo1.c3 | 2 +- resources/testfragments/toposort.c3 | 4 ++-- src/compiler/sema_expr.c | 15 ++------------- test/test_suite/functions/test_regression.c3t | 4 ++-- .../functions/test_regression_mingw.c3t | 4 ++-- test/test_suite/statements/foreach_custom.c3t | 2 +- .../statements/foreach_custom_macro.c3t | 2 +- 11 files changed, 23 insertions(+), 34 deletions(-) diff --git a/resources/examples/base64.c3 b/resources/examples/base64.c3 index aa8d8cc36..0788165ce 100644 --- a/resources/examples/base64.c3 +++ b/resources/examples/base64.c3 @@ -46,7 +46,7 @@ func void encode(char[] in, char *out) { int j = 0; char c = LUT_ENC[1]; - for (int i = 0; i < in.len(); i++) + for (int i = 0; i < in.len; i++) { switch (i % 3) { @@ -61,7 +61,7 @@ func void encode(char[] in, char *out) } // move back - usize last = in.len() - 1; + usize last = in.len - 1; // check the last and add padding switch (last % 3) { @@ -80,7 +80,7 @@ func int! decode(char[] in, char* out, int* invalid_char_index = null) { int j = 0; - for (int i = 0; i < in.len(); i++) + for (int i = 0; i < in.len; i++) { char value = in[i]; if (value == PAD) return j; @@ -100,13 +100,13 @@ func int! decode(char[] in, char* out, int* invalid_char_index = null) case 1: out[j++] += c >> 4 & 0x3; // if not last char with padding - if (i < (in.len() - 3) || in[(long)(in.len()) - 2] != PAD) + if (i < (in.len - 3) || in[(long)(in.len) - 2] != PAD) { out[j] = (c & 0xF) << 4; } case 2: out[j++] += c >> 2 & 0xF; - if (i < (in.len() - 2) || in[(long)(in.len()) - 1] != PAD) + if (i < (in.len - 2) || in[(long)(in.len) - 1] != PAD) { out[j] = (c & 0x3) << 6; } diff --git a/resources/examples/fasta.c3 b/resources/examples/fasta.c3 index 7cc25de49..b71c47f0b 100644 --- a/resources/examples/fasta.c3 +++ b/resources/examples/fasta.c3 @@ -57,7 +57,7 @@ const LINELEN = 60; // slowest character-at-a-time output func void repeat_fasta(char[] seq, int n) { - usize len = seq.len(); + usize len = seq.len; int i = void; for (i = 0; i < n; i++) { @@ -69,8 +69,8 @@ func void repeat_fasta(char[] seq, int n) func void random_fasta(char[] symb, double[] probability, int n) { - assert(symb.len() == probability.len()); - int len = (int)(probability.len()); + assert(symb.len == probability.len); + int len = probability.len; int i = void; for (i = 0; i < n; i++) { diff --git a/resources/examples/nbodies.c3 b/resources/examples/nbodies.c3 index 84b301141..72c892963 100644 --- a/resources/examples/nbodies.c3 +++ b/resources/examples/nbodies.c3 @@ -15,7 +15,7 @@ struct Planet func void advance(Planet[] bodies) @noinline { - usize nbodies = bodies.len(); + usize nbodies = bodies.len; foreach (i, Planet* &b : bodies) { for (usize j = i + 1; j < nbodies; j++) @@ -45,7 +45,7 @@ func void advance(Planet[] bodies) @noinline func double energy(Planet[] bodies) { double e; - usize nbodies = bodies.len(); + usize nbodies = bodies.len; foreach (i, Planet* &b : bodies) { diff --git a/resources/examples/notworking/levenshtein.c3 b/resources/examples/notworking/levenshtein.c3 index 970c86adb..14309c8ae 100644 --- a/resources/examples/notworking/levenshtein.c3 +++ b/resources/examples/notworking/levenshtein.c3 @@ -4,8 +4,8 @@ func int levenshtein(String s, String t) { // if either string is empty, difference is inserting all chars // from the other - if (!s.len()) return t.len(); - if (!t.len()) return s.len(); + if (!s.len) return t.len; + if (!t.len) return s.len; // if last letters are the same, the difference is whatever is // required to edit the rest of the strings diff --git a/resources/testfragments/demo1.c3 b/resources/testfragments/demo1.c3 index 71fb820d1..2ad90856c 100644 --- a/resources/testfragments/demo1.c3 +++ b/resources/testfragments/demo1.c3 @@ -101,7 +101,7 @@ func void testArrays() { printf("index[%d]: %d\n", i, a); } - printf("Length is a runtime value: %d\n", y.len()); + printf("Length is a runtime value: %d\n", y.len); puts("Getting a slice from the beginning to an offset of the end---"); y = x[..^2]; // Same as x[0..^2] foreach (i, a : y) diff --git a/resources/testfragments/toposort.c3 b/resources/testfragments/toposort.c3 index 76bcbc607..cdc22fb1e 100644 --- a/resources/testfragments/toposort.c3 +++ b/resources/testfragments/toposort.c3 @@ -26,9 +26,9 @@ struct TopoList public func void sort(InputPair[] pairs, uint elements) { printf(.x = "fe"); - InputPair[] result = @array::make(InputPair, pairs.len()); + InputPair[] result = @array::make(InputPair, pairs.len); TopoList* top = mem::calloc(TopoList.sizeof, elements); - for (int i = 0; i < pairs.len(); i++) + for (int i = 0; i < pairs.len; i++) { InputPair pair = pairs[i]; assert(pair.value >= 0 && pair.value < elements); diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index 4b1ba57e4..faa8ea831 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -1758,14 +1758,6 @@ static inline bool sema_expr_analyse_call(Context *context, Type *to, Expr *expr decl = func_expr->macro_expansion_expr.decl; macro = true; break; - case EXPR_LEN: - if (func_expr->type == type_void) - { - expr_replace(expr, func_expr); - expr_set_type(expr, type_usize); - return true; - } - FALLTHROUGH; case EXPR_TYPEINFO: if (func_expr->type_expr->resolve_status == RESOLVE_DONE) { @@ -2399,7 +2391,8 @@ CHECK_DEEPER: { expr->expr_kind = EXPR_LEN; expr->len_expr.inner = parent; - expr_set_type(expr, type_void); + expr->original_type = type_compint; + expr->type = type_usize; expr->resolve_status = RESOLVE_DONE; return true; } @@ -6139,10 +6132,6 @@ static inline bool sema_cast_rvalue(Context *context, Type *to, Expr *expr) return false; } break; - case EXPR_LEN: - if (expr->type != type_void) return true; - SEMA_ERROR(expr, "Expected () after 'len' for subarrays."); - return false; case EXPR_TYPEINFO: SEMA_ERROR(expr, "A type must be followed by either (...) or '.'."); return false; diff --git a/test/test_suite/functions/test_regression.c3t b/test/test_suite/functions/test_regression.c3t index 04a100508..bfcb950d0 100644 --- a/test/test_suite/functions/test_regression.c3t +++ b/test/test_suite/functions/test_regression.c3t @@ -86,7 +86,7 @@ define Argh2 = func int(double, Bobo); func int sum_us(int... x) { int sum = 0; - if (x.len() == 0) return 0; + if (x.len == 0) return 0; sum += x[0] + sum_us(...x[1..^1]); return sum; } @@ -96,7 +96,7 @@ define Frob = long; func int sumd(int[] x) { int sum = 0; - for (int i = 0; i < x.len(); i++) sum += x[i]; + for (int i = 0; i < x.len; i++) sum += x[i]; return sum; } diff --git a/test/test_suite/functions/test_regression_mingw.c3t b/test/test_suite/functions/test_regression_mingw.c3t index c8c7e7ba1..1a007b80f 100644 --- a/test/test_suite/functions/test_regression_mingw.c3t +++ b/test/test_suite/functions/test_regression_mingw.c3t @@ -86,7 +86,7 @@ define Argh2 = func int(double, Bobo); func int sum_us(int... x) { int sum = 0; - if (x.len() == 0) return 0; + if (x.len == 0) return 0; sum += x[0] + sum_us(...x[1..^1]); return sum; } @@ -96,7 +96,7 @@ define Frob = long; func int sumd(int[] x) { int sum = 0; - for (int i = 0; i < x.len(); i++) sum += x[i]; + for (int i = 0; i < x.len; i++) sum += x[i]; return sum; } diff --git a/test/test_suite/statements/foreach_custom.c3t b/test/test_suite/statements/foreach_custom.c3t index d0836e7ed..adabc049d 100644 --- a/test/test_suite/statements/foreach_custom.c3t +++ b/test/test_suite/statements/foreach_custom.c3t @@ -18,7 +18,7 @@ func FooIterator Foo.iterator(Foo *f) func bool FooIterator.next(FooIterator *it, int *value) { - if (it.index == it.f.x.len()) return false; + if (it.index == it.f.x.len) return false; *value = it.f.x[it.index++]; return true; } diff --git a/test/test_suite/statements/foreach_custom_macro.c3t b/test/test_suite/statements/foreach_custom_macro.c3t index 157bd8883..0f41cc19c 100644 --- a/test/test_suite/statements/foreach_custom_macro.c3t +++ b/test/test_suite/statements/foreach_custom_macro.c3t @@ -16,7 +16,7 @@ macro FooIterator Foo.iterator(Foo *f) macro bool FooIterator.next(FooIterator *it, int *value) { - if (it.index == it.f.x.len()) return false; + if (it.index == it.f.x.len) return false; *value = it.f.x[it.index++]; return true; }