,---------------. | Contributions | `---------------´ ,------------------------------------------ | 2024-07-29 13:31:50 kumaym wrote: | requestClarification - compile semantics or compile action of literal word | see: https://forth-standard.org/standard/core/LITERAL#contribution-353 `------------------------------------------ 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? ,---------. | Replies | `---------´ ,------------------------------------------ | 2024-07-29 17:43:04 ruv replies: | requestClarification - compile semantics or compile action of literal word | see: https://forth-standard.org/standard/core/LITERAL#reply-1267 `------------------------------------------ > shouldn't be equivalent these two definitions? > ``` > : foo [ 4 ] literal ; > 4 : foo literal ; > ``` They are not equivalent because the [word `:`](https://forth-standard.org/standard/core/Colon) (colon) leaves _colon-sys_ on the stack, and the [word `;`](https://forth-standard.org/standard/core/Semi) (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](https://forth-standard.org/standard/usage#subsubsection.3.1.5.1). 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.