// sized integers: scalars wrap to their size, arrays and pointers of them are packed struct pix { u8 r, g, b, a; i16 dx; [4]u8 tag; }; u8 gbyte = 300; i8 gneg = 200; u16 gword[5]; [10]u8 gbuf; u8 add8(u8 a, u8 b) { return a + b; } fn scalars() { u8 a = 250; i8 s = 100; i16 h = 32767; u32 w = 4294967295; a = a + 10; put_n(a); put_c(' '); // 4 s = s + 100; put_n(s); put_c(' '); // -56 h++; put_n(h); put_c(' '); // -32768 w++; put_n(w); put_c(' '); // 0 put_n(gbyte); put_c(' '); put_n(gneg); put_c(' '); // 44 -56 put_n(add8(200, 100)); put_c(' '); // 44 put_n((i8)200); put_c(' '); put_n(300 as u8); put_c(10); // -56 44 return 0; } fn arrays() { u8 buf[20]; i16 sh[7]; u32 ws[3]; i8 sb[9]; int i = 0, sum = 0; while (i < 20) { buf[i] = i * 20; i++; } // wraps past 255 i = 0; while (i < 20) { sum = sum + buf[i]; i++; } put_n(sum); put_c(' '); // 1250 sh[3] = 40000; put_n(sh[3]); put_c(' '); // -25536 sh[3] += 30000; put_n(sh[3]); put_c(' '); // 4464 ws[1] = 5000000000; put_n(ws[1]); put_c(' '); // 705032704 sb[8] = 127; sb[8]++; put_n(sb[8]); put_c(' '); // -128 sb[0] = 200; put_n(sb[0] < 0); put_c(' '); // 1: -56 is negative ws[0] = 4000000000; put_n(ws[0] + ws[0]); put_c(' '); // arithmetic is done in int: 8000000000 put_n(sizeof(buf)); put_c(' '); // 3 words put_n(sizeof(sh)); put_c(10); // 2 words return 0; } fn heap() { u8* p = new(u8, 1000); int i = 0, sum = 0; while (i < 1000) { p[i] = i; i++; } i = 0; while (i < 1000) { sum = sum + p[i]; i++; } put_n(sum); put_c(' '); // 1000 bytes of 0..255: 3 * 32640 + 28 * ... = 127.. put_n(osize(p) < 256); put_c(32); // 125 words plus the header, not 1000 *p = 77; put_n(p[0]); put_c(' '); free(d*p); gbuf[9] = 255; gword[4] = 65535; gword[4]++; put_n(gbuf[9]); put_c(' '); put_n(gword[4]); put_c(10); return 0; } fn fields() { pix* q = new(pix); q.r = 255; q.g = 256; q.dx = 65535; q.tag[2] = 7; q.tag[3] = 300; put_n(q.r); put_c(' '); put_n(q.g); put_c(' '); put_n(q.dx); put_c(' '); put_n(q.tag[2]); put_c(' '); put_n(q.tag[3]); put_c(' '); put_n(sizeof(pix)); put_c(10); free(d*q); return 0; } o3 fn checksum(u8* p, int n) { int i = 0, a = 1, b = 0; while (i < n) { a = (a + p[i]) % 251; b = (b + a) % 251; i++; } return b * 256 + a; } fn o3run() { u8* p = new(u8, 5000); int i = 0; while (i < 5000) { p[i] = i * 7; i++; } put_n(checksum(p, 5000)); put_c(10); free(d*p); return 0; } fn main() { scalars(); arrays(); heap(); fields(); o3run(); return 0; }