r/Forth • u/[deleted] • Dec 06 '18
Swapless forth
I am toying with the idea of a swapless forth here:
https://github.com/johnmorrisbeck/newlangspecification/blob/master/swapless
Afaik if you took something along the lines of gforth or jonesforth it should be possible to write this code so it is integrated and compatible with those versions of code while still having swap inside the system.
In jonesforth the . That I specificied would be something along the lines of this:
load global pointer into eax.
Execute function that gets subobject of foo and load that into eax
Keep loading subobjects into eax until you have the pointer you want.
Push it onto the stack.
Do the same until you have the second object you want.
Pop the stack into both ea registers.
Store one inside the other, and leave the stack minus two items.
. So sorry but not sorry for using the . but it is how subobjects are fetched in javascript.
3
u/_crc Dec 06 '18
I took a look at this and have no idea what you are trying to get across.
From the link:
This is the formal specification of a language which I like to call a post-forth language for the time for a no swap forth which is the best name that I have to describe it at the moment.
This is not a formal specification, more a stream of thoughts.
A no swap forth is a type of forth which intends to operate using a stack and a global object instead of a swap.
I'm not sure what this means. swap is just a word for reordering a couple of items on the stack. The use of objects on (or in addition to) the stack wouldn't have any impact I can see on the need (or lack thereof) for a word like swap.
In forth data can be accessed by passing it around in a stack. In javascript this works like this.
g.stack=[]; stack.push(3); stack.push(3); stack.push(stack.pop()+stack.pop());
This is similar to how I desire things to work in my own language except that there should be a global object inside of the forth and not a swap.
I don't see anything here involving a swap. Later you have:
g . foo . bar . bazz 2 @
What @ does in this specification is takes an object that is on the stack and stores it inside the object.
This is the simple specification of the language.
A way to write an addition in this language might look like this.
g . foo . bar g . foo . baz +
Ignoring the non-standard uses for . and @, there's nothing here that would appear to have anything to do with a swap function.
Can you clarify what the actual point is?
0
Dec 07 '18
The point is to use global objects in forth as a means of passing input and output between functions instead of swap rot etc. However keeping the paradigm of no boilerplate function calls.
3
u/comma_at Dec 07 '18
The whole point of the stack is to have your arguments there, ready to be used. Here is a forth-like addition:
+Now here is yours:
g . foo . bar g . foo . baz +Now here is javascript:
g.foo.bar + g.foo.bazNow here is lisp:
(+ (bar (foo g)) (baz (foo g)))Can you see where I'm going with this?
Furthermore you can still achieve the same code you are proposing in forth with variables/values and objects with OO extensions like gforth provides.
In short, with your proposed style you are not gaining much from forth. If you really want to mix forth with some OO/FP concepts take a look at Factor, 8th, retro etc.
2
u/eritain Dec 07 '18
If OP does build this system and program in it, I suspect he'll find that keeping values on the stack is more necessary than he thought. At a minimum, something has to say what to retrieve from that global memory, so that something can't itself be hidden in said global memory. But if he wants to do it, hey. Some people juggle geese.
0
Dec 07 '18
I think you would still want swap for things like addition and subtraction you are right.
I think though that what I am aiming to probe at is a way of stack juggling which is not so intimidating for beginners and is not so inefficient if you don't feel like calculating a large stack juggle.
1
u/_crc Dec 07 '18
So you want an exposed stack for passing/returning data, but no way to order the data on it? Fetching & storing from values in RAM only?
0
Dec 07 '18
I see now based on another person's analysis swap would still be good for arithmetic. However I think I like this for storing hierarchical data in a named way and also for avoiding some larger stack juggling.
2
u/eritain Dec 06 '18
Comprehension check: You want to build a forth-like language where data values usually live in a hierarchically structured namespace (the global object and its subobjects), and are only brought out to the stack (by chasing pointers) just prior to use. And they're brought out in order, so that there isn't stack juggling. Do I understand you correctly?
0
Dec 07 '18
It is certainly a forth-like although I think if it is not compatible with ans/jones then it is a good dsl in forth. The thing I like about it is I prefer the c style where things are fetched from a global object however I like the forth style where to invoke a function is just to call the function and the simplest thing to do in the language is to call a function with no boilerplate.
3
u/8thdev Dec 06 '18
What do you mean by "swapless"? I don't understand what you're getting at.