6.1.0860 C, c-comma CORE

( char -- )

Reserve space for one character in the data space and store char in the space. If the data-space pointer is character aligned when C, begins execution, it will remain character aligned when C, finishes execution. An ambiguous condition exists if the data-space pointer is not character-aligned prior to execution of C,.

See:

Testing:

HERE 1 C,
HERE 2 C,
CONSTANT 2NDC
CONSTANT 1STC

T{    1STC 2NDC U< -> <TRUE> }T \ HERE MUST GROW WITH ALLOT
T{      1STC CHAR+ ->  2NDC  }T \ ... BY ONE CHAR
T{  1STC 1 CHARS + ->  2NDC  }T
T{ 1STC C@ 2NDC C@ ->   1 2  }T
T{       3 1STC C! ->        }T
T{ 1STC C@ 2NDC C@ ->   3 2  }T
T{       4 2NDC C! ->        }T
T{ 1STC C@ 2NDC C@ ->   3 4  }T

ContributeContributions

NieDzejkobavatar of NieDzejkob [49] Suggested reference implementationSuggested reference implementation2018-01-08 17:54:46

: C, ( char -- ) HERE @ TUCK ! CHAR+ HERE ! ;

NieDzejkobavatar of NieDzejkob

Nope, this won't work, since HERE is more like a VALUE than a VARIABLE in ANS Forth. Why is standarization so hard?

alextangentavatar of alextangent

: 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.

Reply New Version

ruvavatar of ruv [194] Additional test for UTF-16Suggested Testcase2021-04-28 22:47:11

Somebody could think that UTF-16 is a valid string representation in memory, having the char size = 1 byte (see a discussion/raw message in comp.lang.forth).

This test case ensures that C, stores a primitive character (a code unit).

T{ ALIGN HERE CHAR A C, CHAR B C, CHAR C C, 3 S" ABC" COMPARE -> 0 } 

AntonErtlavatar of AntonErtl

This is a good test case that tests the (standardized) relation between characters as processed with C, C@ C!, and ASCII characters in a string (processed with S" and COMPARE). ASCII characters are guaranteed to consume only one char in memory, only extended characters may consume more than one char in their memory representation.

PeterKnaggsavatar of PeterKnaggs

I don't agree, I use UTF-16 to store a string in memory.

My C, stores a 16-bit value, so would pass this test without problem. Forth '94 went to some extent to allow UTF-16 which is inherited by Forth '12.

ruvavatar of ruv

I don't agree, I use UTF-16 to store a string in memory.

Certainly UTF-16 may be used. But then the char size should be 2 bytes. My point was that it's not allowed to use UTF-16 when the char size is 1 byte (and the test will fail in such case). By the same ground, UTF-32 may be used when the char size is 4 bytes only (any other char size will not be compliant).

AntonErtlavatar of AntonErtl

If your system passes your test, it is unclear what you disagree with.

Reply New Version