( ROMANS by Leo Brodie, Thinking Forth:1) CREATE ROMANS ( ones) CHAR I , CHAR V , ( tens) CHAR X , CHAR L , ( hundreds) CHAR C , CHAR D , ( thousands) CHAR M , VARIABLE COLUMN# ( current offset) : ONES 0 COLUMN# ! ; : TENS 2 COLUMN# ! ; : HUNDREDS 4 COLUMN# ! ; : THOUSANDS 6 COLUMN# ! ; : COLUMN ( -- address of column) ROMANS COLUMN# @ + ; ( ROMANS by Leo Brodie, Thinking Forth:2) : .SYMBOL ( offset --) COLUMN + @C EMIT ; : ONER 0 .SYMBOL ; : FIVER 1 .SYMBOL ; : TENER 2 .SYMBOL ; : ONERS ( # of oners --) 0 ?DO ONER LOOP ; : ALMOST ( quotient of 5 / --) ONER IF TENER ELSE FIVER THEN ; : DIGIT ( digit --) 5 /MOD OVER 4 = IF ALMOST DROP ELSE IF FIVER THEN ONERS THEN ; ( ROMANS by Leo Brodie, Thinking Forth:3) : ROMAN ( number --) 1000 /MOD THOUSANDS DIGIT 100 /MOD HUNDREDS DIGIT 10 /MOD TENS DIGIT ONES DIGIT ; : ENTER BEGIN REFILL DROP BL PARSE-WORD NUMBER ERROR? WHILE DROP REPEAT ; : PROMPT ." Enter number: " ENTER ." Roman number: " ROMAN CR ; PROMPT
|