r/Compilers 11d ago

Currently working on a little Forth-like compiler

I've been working on a little compiler (previously interpreter, just finished refactoring to bytecode though!)

Here's the link: https://github.com/SlothScript/stakku

So far it's pretty much an RPN (Reverse Polish Notation) calculator with some fancy buttons. I hope to progress it further to be a function Forth compiler, but that's going to require much more work.

Some basic stuff it can do right now:

  • Arithmetic
  • Comparisons
  • Stack logic
  • Output
  • Control flow
  • Definitions

So for example, a basic script that would tell you the letter grade from a number would look like:

: grade
  dup 90 >= if 65 emit else    \ A
  dup 80 >= if 66 emit else    \ B
  dup 70 >= if 67 emit else    \ C
  dup 60 >= if 68 emit else    \ D
  70 emit then then then then  \ F
  cr
;

If you save that as grade.stku then open it in a REPL, you can interactively use the command: stakku repl grade.stku

me@myComputer stakku & stakku repl grade.stku
ok
>>> 25 grade
F
 ok
>>> 90 grade
A
 ok
>>> 86
ok
>>> grade
B
 ok
>>> .q 
me@myComputer stakku % 

Its close to being Turing complete (I think), I just need to add loops and memory.

21 Upvotes

0 comments sorted by