Fix stack setting after error return. Some fixes to examples.

This commit is contained in:
Christoffer Lerno
2022-07-02 10:08:45 +02:00
committed by Christoffer Lerno
parent b1d83e2ccd
commit bb28f6e61c
12 changed files with 140 additions and 110 deletions

View File

@@ -0,0 +1,25 @@
module binarydigits;
import std::math;
import libc;
fn void main()
{
for (int i = 0; i < 20; i++)
{
String s = bin(i);
defer s.destroy();
libc::printf("%s\n", s.zstr());
}
}
fn String bin(int x)
{
int bits = 1 + (int)(x == 0 ? 0 : math::log10((double)(x)) / math::log10(2));
String str;
str.append_repeat('0', bits);
for (int i = 0; i < bits; i++)
{
str.set((usize)(bits - i - 1), x & 1 ? '1' : '0');
x >>= 1;
}
return str;
}