Update examples and contracts.

This commit is contained in:
Christoffer Lerno
2023-03-08 15:57:42 +01:00
parent b94c647ead
commit 1480b8f872
11 changed files with 39 additions and 30 deletions

View File

@@ -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;

View File

@@ -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);

View File

@@ -18,3 +18,4 @@ macro promote_int(x)
$endif;
}
macro TypeKind @inner_kind(#value) => types::inner_kind($typeof(#value));

View File

@@ -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);
}

View File

@@ -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++)
{

View File

@@ -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);
}

View File

@@ -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;

View File

@@ -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++)

View File

@@ -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);
}

View File

@@ -1,6 +1,6 @@
module tmem;
import std::mem;
import std::io;
import libc;
struct VarString
{

View File

@@ -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)
{