Tiny PRINTF and SPRINTF words added

36 views
Skip to first unread message

The Beez

unread,
Mar 19, 2017, 10:34:38 AM3/19/17
to 4tH-compiler
Hi 4tH-ers!

I  know, it's quite hard for newbies to compose on-screen tables or even lines, since we don't have those handy printf() routines. Even I have trouble making logfile messages with lots of parameters. And even if I do manage, it's still hard to maintain.

So I thought it would be a nice exercise to do them in the weekend. Aller Anfang ist schwierig, so I had some trouble to get a grasp on it, but when I did, it proved to be solid enough to do an even handier sprintf().

Now printf() don't come cheap, so this one takes a few hundred bytecodes as well, which is about 40 lines. Not EVERYTHING is implemented, only 4tH's native types, but I still think it will cover about 80% of your needs. To give you an impression on the format, here it is:

   s" This is %%char%c%%, string '%-6s' and number%*d"

If there is a REAL need for octal and hexadecimal stuff, let me know. That one ain't too hard to add. You also have to remember, you have to feed the parameters in reverse, Forth way. It was either that or an extra parameter to ensure that the whole thing is cumbersomely inverted, before it is fed to PRINTF. Also note, PRINTF is acting blind. If you haven't positioned in string in place when %s is due, it's your problem - not PRINTFs.

Quick demo:

  80 string buffer
  23 4 s" Hello" char ! s" This is %%char%c%%, string '%-6s' and number%*d" buffer sprintf type cr


Gives:

  This is %char!%, string 'Hello ' and number  23


Code in SVN.

Hans Bezemer
Reply all
Reply to author
Forward
0 new messages