Log In  


Cart #picoforth-0 | 2021-05-14 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
5

picoforth - a stack-based programming language

picoforth is a project I started working on a while ago, and then gave up on, then returned to yesterday. It is unfinished (there are a few minor bugs, there is essentially no support for strings yet, and the method of inputting text is pretty bad), but still can do quite a lot.

If you are unfamiliar with stack-based languages, I recommend you check out easyforth, a stack-based language resembling a typical Forth. It can teach you the basics of stack-based languages, and to a lesser extent, Forth.

Here's the list of the words (for those unfamiliar, a "word" in forth is roughly comparable to a function in most other languages) currently in picoforth:

(if a word doesn't have a comment, that means it works exactly like it would in a normal Forth)
pop
swap
dup
over
rotl   -- rotates the top three stack items left (n1 n2 n3 -> n3 n1 n2)
rotr   -- same, but to the right (n1 n2 n3 -> n2 n3 n1)
+
-
*
/
//     -- integer division 
=
~=     -- not equals
>
<
&      -- bitwise and
|      -- or
^^     -- xor
~      -- not
>>     -- shr
<<     -- shl
not    -- 1 -> 0, 0 -> 1
!      -- @ and ! are peek and poke, respectively
@
.
.stack -- prints the current stack, mostly useful for debugging
key    -- halts the program until a key is pressed; puts the ord() of that key on the stack
emit
branch -- relative branch: adds the instruction pointer with the value at the top of the stack (can be negative)
absbranch -- absolute branch: sets the instruction pointer to the value of the item at the top of the stack
:
;         

Here's an example program, the one that generated the label image:

0 1 + dup dup 0x6000 + dup rotl ! 0x7fff < 2 * absbranch

My eventual goal with this is to make tiny games/demos that you can copy and paste into this cart, similar to a tweetcart. If you want to, make spinoffs of this cart! I'd love to see them.

5


1

Whoa. Ok, I'm impressed! You don't usually see a programming language implementation when you randomly update the WIP section in SPLORE! I am actually quite fascinated with Forth. In fact, I made a programming language a while ago inspired by it! It isn't very good though.


@wallgraffiti Thanks!
I have previously made forth-likes in the past, but they always seemed to break at some point and I just couldn't figure out why.
Also, picoforth recently got updated! You can check it out here. (It too suffered from this breaking -- when I was adding new stuff to this picoforth, it broke somewhere and I couldn't figure out why. That's why the update took so long to get out; I had to start from scratch.)



[Please log in to post a comment]