3 ms·
> We can categorize things: > IMMEDIATE words used here are : ( if then ; `:` normally isn’t immediate > First up is `:`. `:` is an IMMEDIATE word, so the
by Someone 1y ago
> We can categorize things:
> IMMEDIATE words used here are : ( if then ;
`:` normally isn’t immediate
> First up is `:`. `:` is an IMMEDIATE word, so the compiler just calls it now
`:` gets executed because the interpreter, when it isn’t compiling, goes through a loop:
1) read a token until the next space in the input
2) look up that token in the dictionary
3a) if a word is found: call it
3b) if no word is found: try interpreting the token as a number
4a) if it can be interpreted such: push that number on the stack
4b) if it cannot: bail out with an error message
So, `:` gets called in step 3a.
> Now the compiler sees `0`. This is a literal token, so we don't even bother with the symbol table; we special-case code to push this value on the stack.
As indicated above, that’s not how ‘normal’ forths work. A lookup is done for a word named `0`, and if it exists, a call to it is compiled.
Many forths had words named after small constants such as `0`, `1`, `2` or `-1` because compiling a call to a function took less memory than compiling a call to the “LIT” function and compiling the constant value.