5 ms·
while(true) { switch(*ip) { //arithmetics (+,-,*...): case PLUS: ds.push(ds.pop() + ds.pop()); ++ip; //stack manipulation (drop,swap,rot.
by Corsome 3y ago
while(true) {
switch(*ip) {
//arithmetics (+,-,*...):
case PLUS: ds.push(ds.pop() + ds.pop()); ++ip;
//stack manipulation (drop,swap,rot...):
case DROP: ds.pop(); ++ip;
...
Hmm... shouldn't each case contain a break? It doesn't seem like a fall through is a good idea here.
- DonHopkins 3y agoMitch Bradley's CForth has an infinite while loop as the inner interpreter with a switch statement inside, that uses a macro "#define next continue" to jump to the next instruction in the infinite while loop, instead of break. https://github.com/MitchBradley/cforth/blob/master/src/cforth/forth.c#L91 https://github.com/MitchBradley/cforth/blob/master/src/cfort... Mitch's CForth is about as efficient (and well designed and refined) a Forth interpreter you can possibly implement in portable C. Mitch's OpenFirmware is a more traditional industrial strength Forth implementation, extremely advanced, feature rich, and well designed, that runs on many different processors and hardware, has assemblers for different CPUs, and a metacompiler that supports many different CPUs, word sizes, byte orders, threading techniques, top level interactive control structures, headerless code, machine independent bytecode, read-only firmware, portable device drivers, hardware and driver configuration, and many hardware platforms. https://github.com/MitchBradley/openfirmware https://github.com/MitchBradley/openfirmware This is an implementation of Open Firmware, a processor-independent firmware design. Open Firmware is specified by IEEE 1275-1994, Standard for Boot (Initialization, Configuration) Firmware. The IEEE standard designation lapsed in 1999, when the Open Firmware working group declined to go through the rather time-consuming IEEE reaffirmation process, but the design lives on. This implementation was written primarily by Mitch Bradley, the original author of Open Firmware. The bulk of the recent development was done at Firmworks by Bradley and colleagues. It traces its roots back to the original Open Boot firmware implementation that Bradley developed at Sun Microsystems in the late 80s and early 90s. That in turn had roots in Forthmacs, a Forth programming language implementation developed and marketed during the early 80s by Bradley Forthware. And Forthmacs, in turn, owes a debt of gratitude to the public domain Forth implementations F83, developed by Michael Perry, and MVP Forth, by Glen Haydon. Lilian Walter wrote the USB stack. https://news.ycombinator.com/item?id=29279022 https://news.ycombinator.com/item?id=29279022 DonHopkins on Nov 19, 2021 | parent | context | favorite | on: Forth vs Lisp The inner interpreter of Mitch Bradley's portable and efficient CForth is simply implemented with a big switch statement, in the function called "inner_interpreter". The C compiler typically compiles that code with the big switch statement into an efficient jump table, behind the scenes. https://github.com/MitchBradley/cforth/blob/master/src/cforth/forth.c#L72 https://github.com/MitchBradley/cforth/blob/master/src/cfort... Properly speaking, that's a token threaded interpreter, since it switches on token numbers in the code field, instead of indirecting function through pointers to the C equivalent of "code" words. https://en.wikipedia.org/wiki/Threaded_code#Token_threading https://en.wikipedia.org/wiki/Threaded_code#Token_threading https://news.ycombinator.com/item?id=27484426 https://news.ycombinator.com/item?id=27484426 DonHopkins on June 12, 2021 | root | parent | next [–] From https://news.ycombinator.com/item?id=21823081 https://news.ycombinator.com/item?id=21823081 I wrote malloc.fth for Mitch Bradley's ForthMacs, which ended up in OpenFirmware: https://github.com/openbios/openfirmware/blob/master/ofw/core/ofwcore.fth#L3453 https://github.com/openbios/openfirmware/blob/master/ofw/cor... [...] https://news.ycombinator.com/item?id=21822840 https://news.ycombinator.com/item?id=21822840 DonHopkins on Dec 18, 2019 | parent | context | favorite | on: Thoughts on Forth Programming Here's the interview with Mitch Bradley saved on archive.org: https://web.archive.org/web/20120118132847/http://howsoftwareisbuilt.com/2008/03/27/interview-with-mitch-bradley-firmware-olpc/ https://web.archive.org/web/20120118132847/http://howsoftwar... I've previously posted some stuff about Mitch Bradley -- I have used various versions of his ForthMacs / CForth / OpenFirmware systems, and I was his summer intern at Sun in '87! Mitch is an EXTREMELY productive FORTH programmer! He explains that FORTH is a "Glass Box": you just have to memorize its relatively simple set of standard words, and then you can have a complete understanding and full visibility into exactly how every part of the system works: there is no mysterious "magic", you can grok and extend every part of the system all the way down to the metal. It's especially nice when you have a good decompiler / dissassembler ("SEE") like ForthMacs, CForth, and OpenFirmware do. https://news.ycombinator.com/item?id=9271644 https://news.ycombinator.com/item?id=9271644 [...]
- benj111 3y agoWell it's not stated to be C, and it isn't anywhere near complete. I read it more as C ish pseudo code that just so happens to be 'valid' C.
- turndown 3y agoNo. This is a kind of duff’s device[0] although it is hard to identify. In essence, it’s attempting to manually unroll the loop’s - the while(true) - switch cases, so that if you have a PLUS then a DROP right after it can just run linearly(that is, fall through to case DROP), not having to go back to the top of the loop to go back into the switch statement and then check that ip != PLUS to get to case DROP, etc. 0: https://en.wikipedia.org/wiki/Duff%27s_device https://en.wikipedia.org/wiki/Duff%27s_device
- jonsen 3y agoYou are probably over-interpreting this interpreter.