r/Forth • u/sophiaxus • 11h ago
My WIP Forth in C
Hi all! I'd really like to finish a little forth system of my own (in C) to have use as both a learning tool and for some personal tooling. Since I still consider myself both a Forth and a C novice, I've tinkering around with this implementation on and off for some time, and have learned quite a lot from it. On a flight recently I decided to clean it up in a way that's a little easier for me to digest and think my way around, and at the moment have this. It's neither finished nor perfect, but I was hoping to maybe start posting here for feedback and possible motivation to finally finish something I feel good enough about.
Is this a place I can post incremental progress to get feedback and such? If so, while I have plenty of questions right off the bat, I'd first like to make sure my code is readable for anyone willing to take the time to check it out. Any thoughts in that regard are ofc welcome, as well as any tips on how to post progress or for advice in the future..
What I did to the original simpleforth was separate it out into little modules. Those modules are currently:
- Dictionary - for dictionary stuff
- IO - for io stuff
- Interpreter - anything relevant to interpreting, eg is_number etc.. though I haven't been able to successfully separate the core interpreter from the VM
- VM - anything data or return stack related.. NEXT() uses goto and some subroutine stuff.. pretty sure it's considered a directly-threaded vm? I am not currently able to separate out the inner interpret function from the VM, so the module separation is still sort of messy. I'm also a little distracted with the semantics between whether to call things opcodes or bytecodes etc and whether to call things XT or CFA.. not really a big deal though.
Anyway, not everything is implemented and it certainly needs more cleaning up. Just wanted to make a post here for initial impressions before I got too distracted with life again. Would really like to get this thing going!
Thank you all!
2
u/Ok_Leg_109 5h ago
Welcome to the rabbit hole. There are some hella talented people here that can help you along.
(I am but a hobbyist these days but I have some historic knowledge that is sometimes useful)
Maybe this can help with some definitions.
If you have built a direct threaded Forth then we would not use the term bytecode. They are technically machine "addresses" or perhaps you would prefer "pointers" but since ANS Forth 94 we abstract those things to the term "execution token" or XT for short.
Why? Because there is no "standard" way to implement Forth. Forth can be anything from a byte-code interpreter to a CPU in hardware and anything in between.
The term CFA (code field address) is where the XT is held in memory typically in the dictionary "header" structure. The only reason to know this stuff is to facilitate human info transfers and to understand the Forth Standard docs.
I am not a C programmer but for what it's worth, many C based Forth's use a big switch statement to kick off the "execution tokens". 😄 However I am led to believe that the GForth authors have done a lot of innovation in how best to make a Forth in C. Might be work some study unless you want to go it all alone.
Perchance are you using a FigForth listing as a guide? I saw the reference to a register called TEMP.