diff --git a/lib/std/core/mem.c3 b/lib/std/core/mem.c3 index fc6f73f2c..71af5ec31 100644 --- a/lib/std/core/mem.c3 +++ b/lib/std/core/mem.c3 @@ -122,10 +122,10 @@ macro void set(void* dst, char val, usz len, usz $dst_align = 0, bool $is_volati } /** - * @require $typeof(a).kindof == TypeKind.SUBARRAY || $typeof(a).kindof == TypeKind.POINTER - * @require $typeof(b).kindof == TypeKind.SUBARRAY || $typeof(b).kindof == TypeKind.POINTER - * @require $typeof(a).kindof != TypeKind.SUBARRAY || len == -1 - * @require $typeof(a).kindof != TypeKind.POINTER || len > -1 + * @require values::@inner_kind(a) == TypeKind.SUBARRAY || values::@inner_kind(a) == TypeKind.POINTER + * @require values::@inner_kind(b) == TypeKind.SUBARRAY || values::@inner_kind(b) == TypeKind.POINTER + * @require values::@inner_kind(a) != TypeKind.SUBARRAY || len == -1 + * @require values::@inner_kind(a) != TypeKind.POINTER || len > -1 * @checked (a = b), (b = a) **/ macro bool equals(a, b, isz len = -1, usz $align = 0) @@ -135,7 +135,7 @@ macro bool equals(a, b, isz len = -1, usz $align = 0) $endif; void* x @noinit; void* y @noinit; - $if ($typeof(a).kindof == TypeKind.SUBARRAY): + $if (values::@inner_kind(a) == TypeKind.SUBARRAY): len = a.len; if (len != b.len) return false; x = a.ptr; diff --git a/lib/std/core/types.c3 b/lib/std/core/types.c3 index 1bb0fada3..cbdb67657 100644 --- a/lib/std/core/types.c3 +++ b/lib/std/core/types.c3 @@ -159,6 +159,15 @@ macro bool is_vector($Type) return $Type.kindof == TypeKind.VECTOR; } +macro TypeKind inner_kind($Type) +{ + $if ($Type.kindof == TypeKind.DISTINCT): + return inner_kind($typefrom($Type.inner)); + $else: + return $Type.kindof; + $endif; +} + macro bool @convertable(#a, $TypeB) @builtin { return $checks($TypeB x = #a); diff --git a/lib/std/core/values.c3 b/lib/std/core/values.c3 index 021050ef0..0daa86930 100644 --- a/lib/std/core/values.c3 +++ b/lib/std/core/values.c3 @@ -18,3 +18,4 @@ macro promote_int(x) $endif; } +macro TypeKind @inner_kind(#value) => types::inner_kind($typeof(#value)); diff --git a/resources/examples/base64.c3 b/resources/examples/base64.c3 index 6bd7ca1f9..31002a6b4 100644 --- a/resources/examples/base64.c3 +++ b/resources/examples/base64.c3 @@ -42,7 +42,7 @@ const char PAD = '='; const char FIRST = '+'; const char LAST = 'z'; -fn void encode(String in, char *out) +fn void encode(char[] in, char *out) { int j = 0; char c = LUT_ENC[1]; @@ -120,7 +120,7 @@ fn int! decode(String in, char* out, int* invalid_char_index = null) extern fn void printf(char *fmt, ...); -fn void main() +fn void! main() { char *helloworld = "Hello World\n"; char[1000] buffer; @@ -128,7 +128,7 @@ fn void main() printf("Result: %s\n", &buffer); char *to_decode = "aGVsbG8gd29ybGRcMA=="; char[*] result = b64"aGVsbG8gd29ybGRcMA=="; - decode(to_decode[0..19], &buffer); + decode((String)to_decode[0..19], &buffer)?; printf("Result: %s\n", &buffer); printf("Result direct: %.*s\n", 13, &result); } \ No newline at end of file diff --git a/resources/examples/binarydigits.c3 b/resources/examples/binarydigits.c3 index c08da6723..f2588549f 100644 --- a/resources/examples/binarydigits.c3 +++ b/resources/examples/binarydigits.c3 @@ -5,16 +5,16 @@ fn void main() { for (int i = 0; i < 20; i++) { - VarString s = bin(i); - defer s.destroy(); + DString s = bin(i); + defer s.free(); io::printf("%s\n", s); } } -fn VarString bin(int x) +fn DString bin(int x) { int bits = x == 0 ? 1 : 1 + (int)math::log2(x); - VarString str; + DString str; str.append_repeat('0', bits); for (int i = 0; i < bits; i++) { diff --git a/resources/examples/contextfree/guess_number.c3 b/resources/examples/contextfree/guess_number.c3 index 1ba110b94..1f4c28b55 100644 --- a/resources/examples/contextfree/guess_number.c3 +++ b/resources/examples/contextfree/guess_number.c3 @@ -32,11 +32,11 @@ fn int! askGuess(int high) fn String! readLine() { - char* chars = tmalloc(1024)?; + char* chars = tmalloc(1024); isz loaded = getline(&chars, &&(usz)1023, libc::stdin()); if (loaded < 0) return InputResult.FAILED_TO_READ!; chars[loaded] = 0; - return chars[0..(loaded - 1)]; + return (String)chars[0..(loaded - 1)]; } fn int! askGuessMulti(int high) @@ -87,7 +87,7 @@ fn void! main() int high = 100; int answer = libc::rand() % high + 1; Game game = { .answer = answer, .high = high }; - game.play(); + (void)game.play(); libc::printf("Finished in %d guesses.\n", game.guesses); libc::printf("Total input errors: %d.\n", err_count); } \ No newline at end of file diff --git a/resources/examples/fannkuch-redux.c3 b/resources/examples/fannkuch-redux.c3 index 3aca2fdee..cfd8b7400 100644 --- a/resources/examples/fannkuch-redux.c3 +++ b/resources/examples/fannkuch-redux.c3 @@ -5,9 +5,9 @@ import libc; fn int fannkuchredux(int n) { - int* perm = array::alloc(int, n); - int* perm1 = array::alloc(int, n); - int* count = array::alloc(int, n); + int* perm = malloc(int, n); + int* perm1 = malloc(int, n); + int* count = malloc(int, n); int max_flips_count; int perm_count; int checksum; diff --git a/resources/examples/spectralnorm.c3 b/resources/examples/spectralnorm.c3 index faacf67f8..6f8894b2a 100644 --- a/resources/examples/spectralnorm.c3 +++ b/resources/examples/spectralnorm.c3 @@ -44,10 +44,10 @@ fn void eval_AtA_times_u(double[] u, double[] atau, double[] x) fn void main(String[] args) { int n = args.len == 2 ? str::to_int(args[1])!! : 2000; - temparr = array::alloc(double, n); - double[] u = array::alloc(double, n); - double[] v = array::alloc(double, n); - double[] x = array::alloc(double, n * n); + temparr = malloc(double, n); + double[] u = malloc(double, n); + double[] v = malloc(double, n); + double[] x = malloc(double, n * n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) diff --git a/resources/testfragments/test.c3 b/resources/testfragments/test.c3 index 0baa2e5dc..802ad4281 100644 --- a/resources/testfragments/test.c3 +++ b/resources/testfragments/test.c3 @@ -2,9 +2,9 @@ import std::io; extern fn int printf(char* message, ...); -macro void swap(&a, &b) +macro void @swap(&a, &b) { - typeof(a) temp = a; + $typeof(a) temp = a; a = b; b = temp; } @@ -14,5 +14,5 @@ fn void main() int x = 1; int y = 2; @swap(x, y); - printf("x: %d y: &d\n", x, y); + printf("x: %d y: %d\n", x, y); } \ No newline at end of file diff --git a/resources/testfragments/tmem.c3 b/resources/testfragments/tmem.c3 index ace0ec9a8..6ad804d84 100644 --- a/resources/testfragments/tmem.c3 +++ b/resources/testfragments/tmem.c3 @@ -1,6 +1,6 @@ module tmem; -import std::mem; import std::io; +import libc; struct VarString { diff --git a/resources/testfragments/toposort.c3 b/resources/testfragments/toposort.c3 index 773328beb..c15ef61cb 100644 --- a/resources/testfragments/toposort.c3 +++ b/resources/testfragments/toposort.c3 @@ -1,5 +1,4 @@ module topologicalsort; -import std::mem; extern fn void printf(char* x, ...); @@ -24,8 +23,8 @@ struct TopoList fn void sort(InputPair[] pairs, uint elements) { - InputPair[] result = array::alloc(InputPair, pairs.len); - TopoList* top = array::alloc(TopoList, elements); + InputPair[] result = malloc(InputPair, pairs.len); + TopoList* top = malloc(TopoList, elements); for (int i = 0; i < pairs.len; i++) { InputPair pair = pairs[i]; @@ -41,7 +40,7 @@ fn void sort(InputPair[] pairs, uint elements) } *next_ref = successor_entry; } - int[] intout = array::alloc(int, elements); + int[] intout = malloc(int, elements); int count = 0; while LOOP: (1) {