6.1.2410 VARIABLE CORE

( "<spaces>name" -- )

Skip leading space delimiters. Parse name delimited by a space. Create a definition for name with the execution semantics defined below. Reserve one cell of data space at an aligned address.

name is referred to as a "variable".

name Execution:

( -- a-addr )

a-addr is the address of the reserved cell. A program is responsible for initializing the contents of the reserved cell.

See:

Rationale:

Typical use: VARIABLE XYZ

Testing:

T{ VARIABLE V1 ->     }T
T{    123 V1 ! ->     }T
T{        V1 @ -> 123 }T

ContributeContributions

JimPetersonavatar of JimPeterson [232] Possible Reference ImplementationSuggested reference implementation2022-04-05 14:05:53

Would the following be a viable reference implementation?

: VARIABLE ALIGN HERE 0 , CONSTANT ;

Of course... this initializes the contents, and you might not want to imply that such is required in an implementation. So maybe this more verbose definition:?

: VARIABLE ALIGN HERE 1 CELLS ALLOT CONSTANT ;

ruvavatar of ruv

Both variants are correct and viable. For reference implementations, the shorter and simpler the better.

And anyway, stack diagrams are required.

lmravatar of lmr

I think the comma already aligns, so, while we're onto this particular horse, the following is probably even more efficient:

: VARIABLE ( "\<spaces\> name" -- )
  0 , HERE CELL- CONSTANT
;

... and the above also proves an ironic point about the requirement for stack diagrams: one can't actually input < and > right before or after a non-whitespace char within triple-backtick sections with the current web front-end (not with ampersand-gt / lt, not with plain less-than). It gets backslash-ified. So one can't actually provide "accurate" stack diagrams (accurate in the sense that I tried to copy the stack diagram from the reference above itself).

AntonErtlavatar of AntonErtl

, is not guaranteed to align (and, e.g., in Gforth it does not). This is explicity stated in 6.1.0150:

An ambiguous condition exists if the data-space pointer is not aligned prior to execution of ,.

The reference implementations by Jim Peterson are standard programs, your reference implementation could be described as having an environmental dependency on an aligning ,.

ruvavatar of ruv

@lmr writes:

one can't actually input < and > right before or after a non-whitespace char within triple-backtick sections with the current web front-end

Yes, it's a known problem, see Issue #54 in the project on GitHub.

Reply New Version