Not surprisingly – PS and Forth have the common feature of being essentially RPN-shaped languages based around an operand stack, so they look quite similar in some ways.
But PS is higher-level. Forth deals in addresses and pointers: declaring a variable allocates space for it (somewhere) and defines the variable's name as a word that pushes the address of that space, so even reading a variable requires a word for "dereference" (if I remember rightly, "varname @" is the rune), and the word to set a variable ("!", I think) is the same as the word for "store this data to some other arbitrary memory address I've come up with by other methods".
By contrast, PS is all logical data types with garbage collection and doesn't give you access to the underlying address space at all. Instead, it has the Lisplike concept of symbolic name tokens (written with that leading /, unlike Lisp's really awkward unmatched quote character), and also a dictionary data type not unlike your Perl/Python hash / dict / associative array. Dictionaries can be used explicitly as user variables using the get and put operators, but there's also an implicit stack of them that hold the current variable scopes, so what "/foo value def" really means is "add the key-value pair (/foo, value) to the dict at the top of the current scope stack, overwriting any previous value that went with that key".
But yes, I suppose that this same idiom for variable-swapping would work very similarly, in that if you'd do a normal Forth assignment by saying
srcvar @ destvar !
then you can do a swap by saying
var1 @ var2 var2 @ var1 ! !
or perhaps it looks neater to rearrange things a bit:
Not surprisingly – PS and Forth have the common feature of being essentially RPN-shaped languages based around an operand stack, so they look quite similar in some ways.
But PS is higher-level. Forth deals in addresses and pointers: declaring a variable allocates space for it (somewhere) and defines the variable's name as a word that pushes the address of that space, so even reading a variable requires a word for "dereference" (if I remember rightly, "varname @" is the rune), and the word to set a variable ("!", I think) is the same as the word for "store this data to some other arbitrary memory address I've come up with by other methods".
By contrast, PS is all logical data types with garbage collection and doesn't give you access to the underlying address space at all. Instead, it has the Lisplike concept of symbolic name tokens (written with that leading /, unlike Lisp's really awkward unmatched quote character), and also a dictionary data type not unlike your Perl/Python hash / dict / associative array. Dictionaries can be used explicitly as user variables using the
get
andput
operators, but there's also an implicit stack of them that hold the current variable scopes, so what "/foo value def" really means is "add the key-value pair (/foo, value) to the dict at the top of the current scope stack, overwriting any previous value that went with that key".But yes, I suppose that this same idiom for variable-swapping would work very similarly, in that if you'd do a normal Forth assignment by saying
then you can do a swap by saying or perhaps it looks neater to rearrange things a bit: