harec

[hare] Hare compiler, written in C11 for POSIX OSs
Log | Files | Refs | README | LICENSE

10-binarithms.ha (2204B)


      1 use testmod;
      2 
      3 fn error() bool = {
      4 	abort();
      5 };
      6 
      7 fn set(x: *int) bool = {
      8 	*x = 42;
      9 	return true;
     10 };
     11 
     12 fn andorxor() void = {
     13 	assert((false || false) == false);
     14 	assert((false || true) == true);
     15 	assert((true || false) == true);
     16 	assert((true || true) == true);
     17 	assert((true || error()) == true);
     18 	let x = 0;
     19 	assert((false || set(&x)) == true);
     20 	assert(x == 42);
     21 
     22 	let x = 0;
     23 	let f = false;
     24 	f ||= false;
     25 	assert(!f);
     26 	f ||= set(&x);
     27 	assert(x == 42);
     28 	assert(f);
     29 	f || error();
     30 	assert(f);
     31 	f ||= false;
     32 	assert(f);
     33 
     34 	assert((false && false) == false);
     35 	assert((false && true) == false);
     36 	assert((true && false) == false);
     37 	assert((true && true) == true);
     38 	assert((false && error()) == false);
     39 	x = 0;
     40 	assert((true && set(&x)) == true);
     41 	assert(x == 42);
     42 
     43 	let x = 0;
     44 	let f = true;
     45 	f &&= true;
     46 	f &&= set(&x);
     47 	assert(x == 42);
     48 	assert(f);
     49 	f &&= false;
     50 	assert(!f);
     51 	f &&= error();
     52 	f &&= true;
     53 	assert(!f);
     54 
     55 	assert((false ^^ false) == false);
     56 	assert((false ^^ true) == true);
     57 	assert((true ^^ false) == true);
     58 	assert((true ^^ true) == false);
     59 
     60 	let f = true;
     61 	f ^^= true;
     62 	assert(!f);
     63 	f ^^= false;
     64 	assert(!f);
     65 	f ^^= true;
     66 	assert(f);
     67 	f ^^= false;
     68 	assert(f);
     69 };
     70 
     71 fn sar_shr() void = {
     72 	assert(-12697259629065987i64 >> 26 == -189203913);
     73 
     74 	let x = 1i64;
     75 	x <<= 63;
     76 	assert(x == -9223372036854775808i64);
     77 	x >>= 63;
     78 	assert(x == -1);
     79 
     80 	let y = 1u64;
     81 	y <<= 63;
     82 	assert(y == 9223372036854775808);
     83 	y >>= 63;
     84 	assert(y == 1);
     85 
     86 	assert(-4i32 >> 1 == -2);
     87 
     88 	let h0 = -12697259629065987i64;
     89 	let h1 = (h0 + (1i64 << 25)) >> 26;
     90 	assert(h1 == -189203912);
     91 };
     92 
     93 def FLOAT: f64 = 6.0 * 7.0;
     94 def I8: i8 = 127 * 2;
     95 def U8: u8 = 128 * 2;
     96 def ALIAS: testmod::enum_alias = 1: testmod::_enum: testmod::enum_alias + 1: testmod::enum_alias;
     97 let a: i8 = 3i8 - (-128i8);
     98 let b: i8 = 3i8 + (-128i8);
     99 def A: i8 = 3i8 - (-128i8);
    100 def B: i8 = 3i8 + (-128i8);
    101 def I32: i32 = 3 - (-2147483648i32);
    102 
    103 fn eval() void = {
    104 	assert(FLOAT == 42.0);
    105 	assert(I8 == -2i8);
    106 	assert(U8 == 0);
    107 	assert(a == -125i8);
    108 	assert(b == -125i8);
    109 	assert(A == -125i8);
    110 	assert(B == -125i8);
    111 	assert(I32 == -2147483645i32);
    112 	assert(ALIAS == 2);
    113 };
    114 
    115 export fn main() void = {
    116 	// TODO: other binarithms
    117 	andorxor();
    118 	sar_shr();
    119 	eval();
    120 };