The issue I had with DOES> isn't that it's hard to use—it's just that I had no idea how one would go about implementing it, much like Javascript programmers use closures without having to think about how they're implemented (even if they're aware of closures in the first place). So, before going into how it works, a sample from Starting Forth is in order. : STAR 42 EMIT ; : .ROW CR 8 0 DO DUP 128 AND IF STAR ELSE SPACE THEN 2* LOOP DROP ; : SHAPE CREATE 8 0 DO C, LOOP DOES> DUP 7 + DO I C@ .ROW -1 +LOOP CR ; HEX 18 18 3C 5A 99 24 24 24 SHAPE MAN These two words support the example. The first word, STAR just prints a asterisk (42 is the ASCII code for the word). The second word, .ROW, takes an 8-bit value and for each bit, if it's a 1, prints an asterisk, otherwise, it prints a space. DO LOOP is Forth's for loop by the way. The next word, SHAPE is the interesting one. But first, we need to discuss CREATE. This word creates a new entry in the Forth dictionary by reading the next word (defined as a collection of non-space letters) in the input as the name. It then gives the newly created word a default action of pushing the address of the body of the word into the stack. Going ahead a bit, the word MAN just after CREATE is run will look like this (in assembly): man fdb shape ; link to next word fdb .xt - .name .name fcc 'man' .xt fdb forth_core_create.runtime .body When MAN is run, the address of .body will be pushed onto the stack. CREATE is typically used to create “smart data structures”—data structures that know how to do some action. Now, getting back to the example, when SHAPE is run, the first thing it does is call CREATE to create a new word, then it compiles 8 values off the top of the stack into the body of the newly created word. Just prior to DOES>, MAN will look like: man fdb shape ; link to next word fdb .xt - .name .name fcc 'man' .xt fdb forth_core_create.runtime .body fcb $24 fcb $24 fcb $24 fcb $99 fcb $5A fcb $3C fcb $18 fcb $18 Now we get to DOES>. ...
First seen: 2025-06-10 02:21
Last seen: 2025-06-10 17:24