I'm creating a stack oriented virtual machine, and so I started learning Forth for a general understanding about how it would work. Then I shortlisted the essential stack manipulation operations I would need to implement in my virtual machine:
drop ( a -- )
dup ( a -- a a )
swap ( a b -- b a )
rot ( a b c -- b c a )
I believe that the following four stack manipulation operations can be used to simulate any other stack manipulation operation. For example:
nip ( a b -- b ) swap drop
-rot ( a b c -- c a b ) rot rot
tuck ( a b -- b a b ) dup -rot
over ( a b -- a b a ) swap tuck
That being said however I wanted to know whether I have listed all the fundamental stack manipulation operations necessary to manipulate the stack in any possible way.
Are there any more fundamental stack manipulation operations I would need to implement, without which my virtual machine wouldn't be Turing complete?
rot rot
as an alternative for-rot
? What happens when there are more than 3 items on the stack? Wouldn't you then have torot
as often asLength-1
times to achieve-rot
?dup
,swap
androt
I usepick ( a_n ... a_0 n -- a_n ... a_0 a_n)
androll ( a_n ... a_0 n i )
instead. Ifi
is negative thenroll
shifts the elements to the left; else to the right.