commit 63ec80bb114593fbe5ba79e72d6b5f72c3ea6439 parent b9a6486cd8b9ba13790f00dc3c5d65fc24beba98 Author: Virgil Dupras <hsoft@hardcoded.net> Date: Sun, 18 Sep 2022 19:16:03 -0400 cc/lib: prototyping what the C "fprintf" will look like I couldn't make it work because "fmt[i]" doesn't work yet. Diffstat:
M | fs/cc/lib.fs | | | 33 | ++++++++++++++++++++++++++++++++- |
1 file changed, 32 insertions(+), 1 deletion(-)
diff --git a/fs/cc/lib.fs b/fs/cc/lib.fs @@ -11,11 +11,42 @@ $100 MemIO :new const _sio +: fputc ( c hdl -- ) IO :putc ; +: fprintd ( n hdl -- ) to@! StdOut swap . to StdOut ; + +\ This is what it will look like +\ TODO: switch statements +\ TODO: allow "int len = strlen(fmt++);" +\ TODO: allow indexing with ident ("fmt[i]") +\ :cfunc void fprintf() { +\ void *iohdl = pspop(); +\ char *fmt = pspop(); +\ int len; +\ int i; +\ char c; +\ len = strlen(fmt); +\ fmt++; +\ for (i=0; i<len; i++) { +\ c = fmt[i]; +\ if (c == '%') { +\ i++; +\ c = fmt[i]; +\ if (c == 'd') { +\ fprintd(iohdl); // n already on PS +\ } else { +\ stype("unsupported fmt argument"); +\ abort(); +\ } +\ } else { +\ fputc(c, iohdl); +\ } +\ } +\ } : fprintf ( .. n1? n0? fmt io-hdl -- ) >r c@+ >r begin ( a ) \ V1=hdl V2=loopcnt c@+ dup '%' = if -1 to+ V2 drop c@+ case ( n a ) - 'd' of = swap V1 to@! StdOut swap . to StdOut endof + 'd' of = swap V1 fprintd endof abort" unsupported fmt argument" endcase else V1 IO :putc then next drop rdrop ;