( romnma - Roman numerals from symbol-value table )
[needs lib\enter.4th]
12 constant maxsym
create symbol ," M" ," CM" ," D" ," CD"
," C" ," XC" ," L" ," XL"
," X" ," IX" ," V" ," IV" ," I"
create symval 1000 , 900 , 500 , 400 ,
100 , 90 , 50 , 40 ,
10 , 9 , 5 , 4 , 1 ,
: roman ( n -- )
1 max 3999 min
maxsym 1+ 0 do
begin dup symval i th @c >= while
symbol i th @c count type
symval i th @c -
repeat
loop
drop
;
: prompt ( -- )
." Enter number: " enter
." Roman numeral: " roman cr
;
prompt
- else ." drop Datatype '" type ." ' not supported" cr abort + else drop ." Datatype '" type ." ' not supported" cr abort
A misplaced quote that masked a DROP. Darn. After that, it ran just fine. Lesson: murky code, no matter how well made, is hard to understand and change after so many years. Better use libs - when available - and (after the great Herman Brood) "never be clever".
Hans Bezemer
That's some pretty clean code, I must say! How long have you been developing for Forth?
Today I needed a (written in 4tH) PHP generator I hadn't touched since 2011. But when I used it all it produced was a *LOT* of garbage. Ok, I thought. Maintained, compiles fine, but unused for 8-some years - this thing is fragging dead. Note when it was built we didn't have structures yet, so everything was blindly poked into plain buffers. I was on the verge of ripping the code out and building it again, when I half-heartedly tried to debug it. I found the error:- else ." drop Datatype '" type ." ' not supported" cr abort + else drop ." Datatype '" type ." ' not supported" cr abortA misplaced quote that masked a DROP. Darn. After that, it ran just fine. Lesson: murky code, no matter how well made, is hard to understand and change after so many years. Better use libs - when available - and (after the great Herman Brood) "never be clever".
\ numeral9 - superstring and accessor/printer for roman numerals "9";
\ u = power of 10 [0, 2]
: "numeral9" c" IXXCCM" ;
: numeral9 ( u -- ) 2 * "numeral9" 1+ + 2 type ;
\ numeral5 - superstring and accessor/printer for roman numerals "5";
\ u = power of 10 [0, 2]
: "numeral5" c" VLD" ;
: numeral5 ( u -- ) "numeral5" 1+ + 1 type ;
\ numeral4 - superstring and accessor/printer for roman numerals "4";
\ u = power of 10 [0, 2]
: "numeral4" c" IVXLCD" ;
: numeral4 ( u -- ) 2 * "numeral4" 1+ + 2 type ;
\ numeral1 - superstring and accessor/printer for roman numerals "1";
\ u = power of 10 [0, 3]
: "numeral1" c" IXCM" ;
: numeral1 ( u -- ) "numeral1" 1+ + 1 type ;
\ pvnumeral - type roman numeral for given power of 10 [0, 3] and place value
\ [1, 9]
: pvnumeral ( upow uval -- )
dup 9 = if
drop numeral9
else
dup 4 = if
drop numeral4
else
dup 4 > if
over numeral5
5 -
then
0 u+do dup numeral1 loop
drop
then
then
;
\ romnumeral - print Roman numeral for given number [1, 3999]
: romnumeral ( u -- )
-1 3 -do
10 i u** /mod ( u%10^i u/10^i )
dup if
i swap pvnumeral
else drop
then
1 -loop
drop
;
\ range check
: romnumeral ( u -- )
dup 0>
over 4000 <
and if
romnumeral
else
." ERROR romnumeral: argument out of range: " .
then
;
Still, the coolest thing they ever let me program was a single awk script many years ago for a long-since retired system. There hasn't been any programming in my job description for maybe 20 years :_( , and all programming has since been outsourced. So you win. ;)
There hasn't been any programming in my job description for maybe 20 years :_( , and all programming has since been outsourced. So you win. ;)
--
You received this message because you are subscribed to the Google Groups "4tH-compiler" group.
To unsubscribe from this group and stop receiving emails from it, send an email to 4th-compiler...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/4th-compiler/792e533a-4cfe-43e7-98ee-71a25e76681c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
I've cleaned up the code a little. I don't know where "maxsym" came up,
but I'm kind of allergic to magic numbers - especially when I can get
the compiler to calculate them for me. Which is what I did here. It was
listed "12" where the tables contain 13 entries - which is also the loop
index. Did I assume correctly that this is where this constant came
from?
Furthermore I used the word "EXCEPT" which is new in v3.64 and a kind of
inverted "WHILE" so I can get rid of all those pesky "0=". If you want
to get that running in 3.62, you'll need the preprocessor:
So, in short my questions are:
- Were my changes correct or did I miss anything?
- Do you allow me to add it to the codebase?
- If so, which license/attribution do you want to add to it?