A recursive descent parser in Forth
mcturra2000.wordpress.com
mcturra2000.wordpress.com
Imagine getting your Forth firmware loaded with this in it, you can already do e.g. OMeta-style grammars because it's still Forth, so you can send your device a language description preamble and then just talk to it in whatever language you like, all in one stream.
After my experiences with postscript (and being a web user like everyone else) allowing for recursion when you don’t really need it freaks me out a little.
This is actually not a major limitation. We've just been blinkered by decades of one kind of notion for "Assembler". It's actually quite easy to create a syntax atop machine code (and implemented in machine code) that provides string literals.
As evidence: I've done it. Here's the pass in my 'assembler' that implements string literals: http://akkartik.github.io/mu/html/apps/dquotes.subx.html. That page is 1925 lines long, but take out the comments and unit tests and there's only 383 lines of code (336 x86 instructions). Including library functions elsewhere (also built in machine code) the resulting binary is 6.5KB large.
There's an example up top that I'll reproduce here:
$ cat x
== code
ab "cd ef"/imm32
$ cat x | apps/dquotes
== code
ab __string1/imm32
== data
__string1:
5/imm32/length
0x63/c 0x64/d 0x20/ 0x65/e 0x66/f
All it's doing is moving string literals to the data segment and replacing their original location with the label they were moved to. Currently there's zero optimization; if you refer to "foo" 3 times you get 3 separate literals in your binary and data segment.More details on the syntax: http://akkartik.name/post/mu-2019-1. HN discussion: https://news.ycombinator.com/item?id=21268252.
- generate a call to a subroutine LITERAL
- store the characters of the string directly after that call
- pad to the ‘word’ size
LITERAL does: - pop the return address from the call stack
- from that address, compute the address of the literal
- push it to Forth’s data stack
- adjust the return address to point beyond the literal
- push the adjusted return address
- return from subroutine
And of course, this being Forth, you can define such words yourself (https://www.forth.com/starting-forth/11-forth-compiler-defin... (an excellent site, by the way))[1] https://plforums.org/practice/minimal-features-required-for-...
[0] https://github.com/siraben/meta-yacc/blob/master/meta.fs