commit 9a94607b0c751cf1b4fe56c1c80242d1e7e71a93
parent 721ddda6c184c2e99ffd631c9e87cdfb7d8d5245
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Mon, 19 Sep 2022 13:45:10 -0400
cc/lib: implement fprintf in C
That compiler is beginning to compile some real stuff!
Diffstat:
M | fs/cc/lib.fs | | | 56 | ++++++++++++++++++++++---------------------------------- |
1 file changed, 22 insertions(+), 34 deletions(-)
diff --git a/fs/cc/lib.fs b/fs/cc/lib.fs
@@ -4,6 +4,7 @@
?f<< /lib/str.fs
0 const NULL
+(S str -- len )
: strlen c@ ;
\ TODO: write this in C. but for this, we need to have 8-bit operations on char
@@ -16,40 +17,27 @@ $100 MemIO :new const _sio
\ 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 fprintd endof
- abort" unsupported fmt argument"
- endcase
- else V1 IO :putc then next drop rdrop ;
+:cfunc void fprintf() {
+ void *iohdl = pspop();
+ char *fmt = pspop();
+ int len = strlen(fmt++);
+ int i;
+ char c;
+ for (i=0; i<len; i++) {
+ c = fmt[i];
+ if (c == '%') {
+ c = fmt[++i];
+ if (c == 'd') {
+ fprintd(iohdl); // n already on PS
+ } else {
+ stype("unsupported fmt argument");
+ abort();
+ }
+ } else {
+ fputc(c, iohdl);
+ }
+ }
+}
: printf ( .. n1? n0? fmt -- ) StdOut fprintf ;