This is a little Forth code parser I'm starting to help me learn Forth
and Go at the same time. Currently it only supports math and printing
but otherwise it has room to grow.
I liked the way Forth did math and operations, in a way that was
nostalgic of ASM. A good example of this is included in the code but
it's always nice to see it.
10 25 * 50 + . <cr>
That puts 10 on the data stack, then 25, then " * " multiplies the top
two elements and replaces them with the result. 50 is then pushed on,
then + pops the top 2 numbers, adds them, then pushes the sum onto the
stack. " . " prints then pops the last thing on the data stack.
I plan on adding a lookup table for functions and probably some
goroutines to speed up parsing.
It's not the fastest thing on earth, and it doesn't support much but
it's a good tool if you want to use some Forth in your code.
any ideas/thoughts?
: star 45 emit ;
: stars 0 do star loop ;
10 stars
**********
This is the first Forth program in the tutorials from the old days.
Correct formating would be:
: star ( -- )
45 emit;
: stars ( n -- )
0 do
star
loop ;
10 stars
. .S 0SP + - * / DUP ?DUP 2DUP PICK TUCK NIP DROP 2DROP OVER 2OVER
SWAP 2SWAP 2SWAP ROT -ROT ( -- )
so it was quite a day. I'm currently going through
http://www.softsynth.com/pforth/pf_tut.html and learning it as I go.
Soon, probably Tuesday, I'll put in a dictionary with function look up
so you can actually define your own functions. Also I'll try to make
goroutines out of some of the functions but it seems unnecessary.
you can find it, still, at http://github.com/ArtemTitoulenko/GoForth/
;
A normal dictionary in your case would be enum:s of all primitive
Forth words, and integers stored in the dictionary. New words gets the
next number after the internaly defined words. A map between string
and interger gives a lookup table.
The interesting with Forth is the way it can jump between interpering
and compiling.
: 100* 100 [ 5 . ] * ;
his will display 5 while compiling, since [ turn of compiler flag
and ] turns it on again.
--HB