6.1.1780 LITERAL CORE

Interpretation:

Interpretation semantics for this word are undefined.

Compilation:

( x -- )

Append the run-time semantics given below to the current definition.

Run-time:

( -- x )

Place x on the stack.

See:

Rationale:

Typical use: : X ... [ x ] LITERAL ... ;

Testing:

T{ : GT3 GT2 LITERAL ; -> }T
T{ GT3 -> ' GT1 }T

ContributeContributions

kumaymavatar of kumaym [353] compile semantics or compile action of literal wordRequest for clarification2024-07-29 13:31:50

it's said compile semantics ( x -- ) of literal is to compile the rutime semantics of place x on stack ( -- x ) , in other words the action literal is supposed to do in compilation state is to compile a literal in the definition of currently defining word, and the literal compiled is obtained from the stack at the moment literal is executed (or compiled since it is a immediate word).

so,

: foo [ 4 ] literal ;

is supposed to define a word foo as having 4 in its body, and that's true

see foo
: foo 4 ; ok

following the compilation of foo step by step we first create a word foo then switch to interpretation mode and push 4 to the stack and then go back to compilation mode to compile literal witch being an immediate word gets executed just to compile 4 (what is on the stack) to the definition of foo and then ; ends the definiton, getting a word containing 4 in its body.

But, then shouldn't be equivalent these two definitions?

: foo [ 4 ] literal ;
4 : foo literal ;

so strange they're not, the first one is ok but the last one gives this error in gforth:

*the terminal*:20:17: error: Control structure mismatch
4 : foo literal >>>;<<<
Backtrace:
kernel/cond.fs:114:26                    0 $6FFFFF810200 throw
glocals.fs:635:5                         1 $6FFFFF821AB0 ?struc
kernel/comp.fs:773:5                     2 $6FFFFF806BF0 ;-hook

why?

ruvavatar of ruv

shouldn't be equivalent these two definitions?

: foo [ 4 ] literal ;
4 : foo literal ;

They are not equivalent because the word : (colon) leaves colon-sys on the stack, and the word ; (semicolon) consumes this colon-sys from the stack.

Therefore, in the latter case, before the compilation semantics for literal are performed, the top of the stack is ( 4 colon-sys ). See: 3.1.5.1 System-compilation types.

In some Forth systems colon-sys occupies zero items in the data stack. In these systems your two definitions for foo are equivalent. But the latter case is not standard compliant.

TammoFreeseavatar of TammoFreese

If colon-sys would be non-zero width and be placed on the data stack, then the following word could not be compiled, right?

: foo [ 42 ] ;

AntonErtlavatar of AntonErtl

: foo [ 42 ] ; 

is not a standard program, so it's up to the individual system what it does with this program. Some report an error, some don't.

kumaymavatar of kumaym

: ANS definition says

Skip leading space delimiters. Parse name delimited by a space. Create a definition for name, called a "colon definition". Enter compilation state and start the current definition, producing colon-sys. Append the initiation semantics given below to the current definition.

I do not fully understand what is a colon-sys in terms of implementation but I suppose it is an abstract concept in order to define a colon definition leaving its real meaning to implementation. I see gforth puts a quite long colon-sys on stack when defining a word, but anyway I feel uncomfortable with this behaviour, I feel that word : shouldn't push anything on stack, I can understand this is necessary in terms of standardization but I see this too complicated and contrary to forth spirit of full control.

A : word mission is to create a new entry in the dictionary filling all slots (including CFA) and start to copy (compile) each xt it encounters in definition to dictionary are pointed by here (into PFA). All those steps can be done without polluting the stack, at the time it starts compiling words in definition the stack shoud be untouched (empty from the point of view of : word). Finally, word ; just have to compile EXIT and switch to interpret state, again it doesn't need anything in the stack to do its action.

This is my point of view, is it erroneus somehow? what made the standard to define a colon-sys concept and implementations to push all that stuff on stack when defining a word?

kumaymavatar of kumaym

Also I doen't understand the reason for initiation semantics in word :

Standard says

Save implementation-dependent information nest-sys about the calling definition. The stack effects i * x represent arguments to name.

So I assume its mission is to leave in return stack the return addres to continue the current definition in the case of a nesting definition, but as far as I know nesting definitions are not allowed in Forth, in fact the standard has something to say about it :

During the compilation of the current definition, a program shall not execute any defining word, :NONAME, or any definition that allocates dictionary data space.

and I think a nesting definition implies executing a defining word during the compilation of the current definition.

Maybe it is related to quotations anyhow?

ruvavatar of ruv

Finally, word ; just have to compile EXIT and switch to interpret state, again it doesn't need anything in the stack to do its action.

How to check that all control flow structures are correctly finished? How to create a nested definition? (e.g., a quotation, but a system is allowed to implement and use other cases)

Some systems employ colon-sys on the data stack to save the previous state and to check pairing. An alternative is to use a separate control-flow stack, but this approach is more complex to implement.


to leave in return stack the return addres to continue the current definition in the case of a nesting definition.

It is not about compilation-time, but about run-time. It's the first part of the well-known "call/return" mechanism. Semantics of "call" are specified as initiation semantics, due to the way of specifying the behavior of compile,.

AntonErtlavatar of AntonErtl

If you prefer a system where the colon-sys is empty, use such a system (or as an implementor, implement such a system).

An example of why systems have non-empty colon-sys is Gforth: it puts a DEFSTART tag on the stack to detect (and report) unbalanced control structures. It also puts an xt on the stack that is used for remembering some of the things needed at the end of the definition (like resolving a DOES>). It also puts some data there that allows to perform some static stack depth checking (but that project is not finished yet).

The question about the initiation semantics (and also the one about colon-sys) would be more appropriate for the entry on :, but anyway: Yes, the initiation semantics is about saving the return address (the nest-sys in standards lingo) on the return stack; this happens at run-time when the new colon definition is called from some other colon definition in some way (directly or through EXECUTE); the initiation semantics is implemented by docol in the traditional threaded-code implementations. And having one colon definition call another colon definition is definitely allowed. The initiation semantics has no particular relation with quotations (which don't exist in Forth-2012).

By contrast, nesting colon definitions at compile time is not allowed in the Forth-2012, but initiation semantics are what happens at run-time and are not related to that. I.e., the following is non-standard:

: foo [ : bar ; ] ;

while the following is standard:

: bar ;
: foo bar 1 . ;

Here the initiation semantics of bar pushes the return address on the return stack so that at the end of bar the run-time semantics of ; knows how to return control to foo.

kumaymavatar of kumaym

You're right when saying this dicussion is better placed in word : , it was my fault not to do it .

But now I don't know how to move it there, so would you prefer to move the discussion to word : or continue here?

If you consider it's better to move it I will continue there and even rewrite what is already written here.

AntonErtlavatar of AntonErtl

The system currently has no way for ordinary users to move contributions or replies elsewhere (and it would probably be a lot of work for the admin, too). My suggestion is to formulate a followon for : that stands alone. You may also provide a pointer to the present discussion, but in order to make your followon easy to understand without having to first follow a link and read that discussion, make it standalone. However, I think that you should refrain from repeating more stuff from here than is necessary to make the new question standalone; your link should be enough for those people who want to read this discussion.

Reply New Version