Digest #26 2018-01-09
Contributions
referenceImplementation - Suggested reference implementation
: C, ( char -- ) HERE @ TUCK ! CHAR+ HERE ! ;
Replies
referenceImplementation - Suggested reference implementation
-ROT is not in the standard. A reference implementation should (must?) use standard words.
referenceImplementation - Suggested reference implementation
-ROT is not in the standard. A reference implementation should (must?) use standard words.
referenceImplementation - Suggested reference implementation
Sorry, I could've sworn the previous implementation isn't there. Please ignore.
referenceImplementation - Suggested reference implementation
- This is not a correct definition; the closing ; is missing.
- It will only compile correctly if the 16 is interpreted as a decimal number, which the definition does not ensure.
referenceImplementation - Suggested reference implementation
Sorry, my Forth is slightly non-standard in that regard.
referenceImplementation - Suggested reference implementation
: FILL ( c-char u char -- ) ROT ROT 0 ?DO 2DUP C! CHAR+ LOOP 2DROP ;
The ROT ROT
does not seem right to me, but finding anything better has proven not to be simple.
referenceImplementation - Suggested reference implementation
: TYPE ( c-addr u -- ) 0 MAX 0 ?DO DUP C@ EMIT CHAR+ LOOP DROP ;
Alternatively, without ?DO
or LOOP
:
: TYPE ( c-addr u -- ) BEGIN DUP 0> WHILE OVER C@ EMIT [ -1 CHARS ] LITERAL - SWAP CHAR+ SWAP REPEAT 2DROP ;
referenceImplementation - Suggested reference implementation
: TYPE 0 MAX 0 ?DO DUP C@ EMIT 1+ LOOP DROP ;
should read
: TYPE ( c-addr u -- ) 0 MAX 0 ?DO DUP C@ EMIT CHAR+ LOOP DROP ;
There are several problems with (even the corrected version) as a reference implementation. The biggest issue is that it doesn't permit using the XCHAR wordset. The primitive for output there is XEMIT, and there is no equivalent XTYPE. The extension says "All words dealing with strings shall handle xchars when the xchar word set is present." It may be that there is no reference implementation in terms of standard words possible.
referenceImplementation - Suggested reference implementation
Nope, this won't work, since HERE
is more like a VALUE than a VARIABLE in ANS Forth. Why is standarization so hard?
referenceImplementation - Suggested reference implementation
: c, ( n -- ) here c! 1 chars allot ;
You're now running into chicken and egg problems. There aren't reference implementations for everything; some things are primitives qv DUP
. You might want to stand back and work out those words that justify a reference implementation because they provide an explanatory point, and those that don't because they might be considered self evident. I'd contend that you've reached that point.
referenceImplementation - Suggested reference implementation
Consider not using ?DO
: FILL ( c-addr u char -- )
>R BEGIN DUP 0<> WHILE
OVER R@ SWAP C!
1 /STRING
REPEAT R> DROP ;
referenceImplementation - Suggested reference implementation
Addendum; and the use of 0 MAX
is not correct, as the length of the buffer is unsigned.