String works with printf. Example cleanup.

This commit is contained in:
Christoffer Lerno
2022-07-26 01:15:36 +02:00
parent cdff5c3e26
commit 4471ccff13
7 changed files with 29 additions and 24 deletions

View File

@@ -69,6 +69,10 @@ private fn void! out_str(PrintParam* param, variant arg)
case FAILABLE:
unreachable();
case DISTINCT:
if (arg.type == String.typeid)
{
return out_substr(param, ((String*)arg).str());
}
return out_str(param, variant { arg.ptr, arg.type.inner });
case POINTER:
typeid inner = arg.type.inner;

View File

@@ -1,13 +1,13 @@
module binarydigits;
import std::math;
import libc;
import std::io;
fn void main()
{
for (int i = 0; i < 20; i++)
{
String s = bin(i);
defer s.destroy();
libc::printf("%s\n", s.zstr());
io::printf("%s\n", s);
}
}

View File

@@ -1,8 +1,8 @@
module fannkuch;
import std::array;
import std::io;
import std::math;
import libc;
import std::mem;
fn int fannkuchredux(int n)
{
@@ -46,7 +46,7 @@ fn int fannkuchredux(int n)
{
if (r == n)
{
libc::printf("%d\n", checksum);
io::printf("%d\n", checksum);
return max_flips_count;
}
@@ -71,6 +71,6 @@ fn int fannkuchredux(int n)
fn int main(int argc, char** argv)
{
int n = argc > 1 ? libc::atoi(argv[1]) : 7;
libc::printf("Pfannkuchen(%d) = %d\n", n, fannkuchredux(n));
io::printf("Pfannkuchen(%d) = %d\n", n, fannkuchredux(n));
return 0;
}

View File

@@ -1,4 +1,5 @@
module fasta;
import std::io;
import libc;
const IM = 139968;
@@ -59,10 +60,10 @@ fn void repeat_fasta(char[] seq, int n)
int i = void;
for (i = 0; i < n; i++)
{
libc::putchar(seq[i % len]);
if (i % LINELEN == LINELEN - 1) libc::putchar('\n');
io::putchar(seq[i % len]);
if (i % LINELEN == LINELEN - 1) io::putchar('\n');
}
if (i % LINELEN != 0) libc::putchar('\n');
if (i % LINELEN != 0) io::putchar('\n');
}
fn void random_fasta(char[] symb, double[] probability, int n)
@@ -80,10 +81,10 @@ fn void random_fasta(char[] symb, double[] probability, int n)
v -= probability[j];
if (v < 0) break;
}
libc::putchar(symb[j]);
if (i % LINELEN == LINELEN - 1) libc::putchar('\n');
io::putchar(symb[j]);
if (i % LINELEN == LINELEN - 1) io::putchar('\n');
}
if (i % LINELEN != 0) libc::putchar('\n');
if (i % LINELEN != 0) io::putchar('\n');
}
fn void main(int argc, char **argv)
@@ -91,13 +92,13 @@ fn void main(int argc, char **argv)
int n = 1000;
if (argc > 1) n = libc::atoi(argv[1]);
libc::printf(">ONE Homo sapiens alu\n");
io::printf(">ONE Homo sapiens alu\n");
repeat_fasta(alu, n * 2);
libc::printf(">TWO IUB ambiguity codes\n");
io::printf(">TWO IUB ambiguity codes\n");
random_fasta(iub, iub_p, n * 3);
libc::printf(">THREE Homo sapiens frequency\n");
io::printf(">THREE Homo sapiens frequency\n");
random_fasta(homosapiens, homosapiens_p, n * 5);
}

View File

@@ -1,4 +1,6 @@
module game_of_life;
import std::io;
import libc;
extern fn void usleep(int time);
@@ -14,16 +16,16 @@ struct GameBoard
fn void GameBoard.show(GameBoard *board)
{
libc::printf("\e[H");
io::printf("\e[H");
char* current = board.world;
for (int y = 0; y < board.h; y++)
{
for (int x = 0; x < board.w; x++)
{
libc::printf(*current ? "\e[07m \e[m" : " ");
io::printf(*current ? "\e[07m \e[m" : " ");
current++;
}
libc::printf("\e[E");
io::printf("\e[E");
}
libc::fflush(libc::stdout());
}
@@ -67,8 +69,8 @@ fn int main(int c, char** v)
GameBoard board;
board.w = w;
board.h = h;
board.world = libc::malloc((ulong)(h * w));
board.temp = libc::malloc((ulong)(h * w));
board.world = mem::alloc((ulong)(h * w));
board.temp = mem::alloc((ulong)(h * w));
for (int i = h * w - 1; i >= 0; i--)
{

View File

@@ -22,5 +22,5 @@ fn int levenshtein(char[] s, char[] t)
int b = levenshtein(s, t[0..^2]);
int c = levenshtein(s[0..^2], t);
return @min(@min(a, b), c) + 1;
return min(min(a, b), c) + 1;
}

View File

@@ -1,8 +1,6 @@
module antiprime;
import std::io;
extern fn int printf(char* message, ...);
fn int countDivisors(int n)
{
if (n < 2) return 1;
@@ -25,7 +23,7 @@ fn int main()
int d = countDivisors(n);
if (d > maxDiv)
{
printf("%d ", n);
io::printf("%d ", n);
maxDiv = d;
count++;
}