@RespiteSage using begin/rescue to recover from exception will probably be slow. In Arithmethic overflow should not raise an exception I shared the following snippet that uses llvm intrinsics for the task
lib LibIntrinsics
fun sat_uadd8 = "llvm.uadd.sat.i8"(a : UInt8, b : UInt8) : UInt8
end
module Intrinsics
def self.sadd(a : UInt8, b : UInt8) : UInt8
LibIntrinsics.sat_uadd8(a, b)
end
end
pp! a = 100_u8 # => 100
pp! a = Intrinsics.sadd(a, 100_u8) # => 200
pp! a = Intrinsics.sadd(a, 100_u8) # => 255
Another idea that I would like to throw is that I think it should be possible to use macros to rewrite calls to +
to some custom function like Intrinsics.sadd
.
So Saline.eval(1 + 2 + 3)
would compile to Intrinsics.sadd(Intrinsics.sadd(1, 2), 3)
or something similar.