4 ms·
That is a masterpiece of Forth programming! I particularly enjoyed this section from the template renderer which made me think... Return stack manipulation - t
by nickcw 8y ago
That is a masterpiece of Forth programming!
I particularly enjoyed this section from the template renderer which made me think... Return stack manipulation - that's playing with fire. And also - wow Forth is not much higher level than assembly, and gosh, I don't think I could have written that without writing some stack diagrams!
2dup s" $>" search if \ Check to see if there's a closing tag $>.
2 - \ Add the close tag to the search result.
nip \ Save the end position of $>.
dup >r
- \ Reduce the string to <$ ... $>.
evaluate \ Run user's code (maybe a bad idea?).
r> \ Retrieve our saved end position of $>.
r> r> \ Retrive the addr u from start of loop iter.
rot \ Bring end $> to stack top.
over >r \ Store the real string's length.
- \ Subtract end $> from u to get the pos from top
r> swap \ that we'd like to strip away. Restore saved u.
/string \ Drop top of the string until the end of $>.
0 \ Keep looping.
Thanks for the trip down memory lane!
- james_s_tayler 8y agoI like the (maybe a bad idea?) comment.
- RodgerTheGreat 8y agoThe rstack is just a temporary second stack with the caveat that you have to be a bit careful about cleaning it up after you're finished with it. (Unless you intend to do something fun and exotic like : goto r> drop >r ;) Usually I'm more tense about code that uses rot/-rot; a little harder to keep track of in my head.
- panic 8y agoThe beauty of Forth is that you can write at as low or as high a level as you'd like. I think most Forth programmers would factor out some of these operations into their own words rather than writing such a low-level definition, but there's nothing preventing you from writing your web server like it's assembly code.