r/Forth • • 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.

0 Upvotes

15 comments sorted by

3

u/8thdev Dec 06 '18

What do you mean by "swapless"? I don't understand what you're getting at.

1

u/[deleted] Dec 07 '18

Moving objects around by passing their struct onto the stack then storing them onto a struck further behind on the stack.

1

u/8thdev Dec 07 '18

Passing a pointer to their struct, you (probably) mean. OK, I thought you somehow meant to get rid of swap

1

u/[deleted] Dec 07 '18

No though you could do that I am more saying that I think that they are compatible but demonstrably separate ways to pass IO. I just don't have a better name for it than swapless forth because I am proposing you can pass with just structs and not swap. Though structs that work like the dictionary hopefully.

1

u/eritain Dec 07 '18

Ah. So you can have structs/objects persisting on the stack, in addition to being accessible via the global?

This notion of folding one value into another is reminiscent of currying, and with some care you ought to be able to get closures too. Might be a fun way to program.

0

u/[deleted] Dec 07 '18

Yeah it reminds me of currying too. The reason I like forth is just because you can flash off a bunch of functions one after another with no boilerplate. I would like to make my programming work like i just name out the programs I want to run in order and have a syntax for soubroutines of that. But stack juggling is my nightmare and I am hoping I can alleviate it some way without adding too much bulk.

My research in language design is about taking a system and removing the fat from it and then adding parts back in to try to make it easier to use. Then I play with it a bit and try to remove the fat again. The key to my algorithm for language design though is I keep trying to make it easier. What I have found is one of the best ways to make sure a language is ultimately easy is if it has full syntactic macros. You can get a repl/debugger going and type in a series of functions that will generate the compiled code you want and hand compile the parts you want. But with full syntactic macros that is easier. So for a while my research was just about finding a language with full syntactic macros then it became about filtering that down to a smaller and smaller thing which was forth.

I don't actually like forth because of it's speed or size advantages. I personally thing it is the easiest programming language because even if you are a beginner programmer you can learn it all the way through in a short time and bloat it to all hell if you'd like.

The thing is some of these die hards really like stack juggling by understanding the right thing to do in the right time I.e. the ideal combination of swap drop rot combined with not leaving extra things on the stack that don't need to be/factoring code/understanding the processor/etc.

I want to hear out those people's advice because on my ideal pc the thing that has full macros should also be able to be the kernel of the machine. This is why I love forth so much is that the green arrays chip essentially is a forth or a series of forths at boot so this is in line with what my research aims for.

However a huge part of my design in trying to make languages easier is to remove boilerplate. I was looking for boilerplateless languages i.e. lisp/forth. And I really like the forth way of doing things which is just to blare off subroutines one after the other with only a space or newline for separation.

I think though that there may always be room for improvement and that for a new user some of these stack operations can be confusing and I want it to be easily approachable by someone who is new to begin with as well as try to be fast and efficient. It is a sort of optimization problem and it depends on where your tastes lie but you find your mix between speed and ease of use.

I really lie more in the camp that a forth should be around 8 k to begin with and the rest of it should boot up. I think that it would not be so hard as people make it out to be to put some forth code on a wide range of processors but maybe I am mistaken.

I really only care about syntax though in the end. I don't in my own research care much about the speed or efficiency of the thing unless I feel like I am getting away with something.

Some ways to do things just make it so that your system is extra hard to use and extra slow. I would cite something like bash as an example of that. Bash is the best shell around probably but it is very difficult to use especially for a new user and it is very slow indeed. Part of why I was driven to forth after so many years of research is my original project was to try to find a way to fix bash or replace it because it was such a terrible shell and I wanted a better one. Forth I think blows bash out of the water. I would rather market forth as a shell and not as a programming language.

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

u/[deleted] 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.baz

Now 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

u/[deleted] 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

u/[deleted] 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

u/[deleted] 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.