mark-hahn / fjs

FORTH-like language for Javascript / Node
MIT License
50 stars 4 forks source link

New function calling method and reverse word order #1

Closed mark-hahn closed 9 years ago

mark-hahn commented 11 years ago

I've designed some major changes to fjs that I think will make it much easier to write and read. The changes will also make the code more javascript-centric while keeping all the advantages of a concatenative language, because FJS will still be purely concatenative. It will be much less forth-like. Comments are welcome.

Right now you need the > and < word modifiers to switch between stack items and function arguments. It is a manual process and makes the code ugly. I plan on removing these altogether. Every function call will place all local stack items in arguments when calling the function and the local stack will be emptied. When the function returns, any items left in the arguments array will be put back in the local stack. Then the function return value will be pushed on the stack. This will be true for both javascript-written functions and fjs-written ones.

The second change is even bigger. I'm planning on changing the word-order form left-to-right to right-to-left. It will still be top-to-bottom. Thus it will resemble languages like Japanese. The reason for this is to make the code read more like normal Javascript function calls.

Here of examples of old code and new. I'm using console.log instead of periods because the periods are hard to read here.

1 2 3 >Math.max >console.log  // old code
console.log Math.max 3 2 1    // new code

Note how much cleaner the new code is and how much more it looks like javascript. But it still executes one word at a time and uses the stack. The cb and wait word will work the same.

I have designed how all this works and the internals will be simpler also. A primitive function written in javascript will no longer need conditional checks for whether it is handling the stack or the arguments. For example, this is the rot function ...

function rot() {
    tmp = arguments[2];
    arguments[2] = arguments[1];
    arguments[1] = arguments[0];
    arguments[0] = tmp;
    return arguments;
}

Please let me know about any concerns. I'm about to start the rewrite.

mark-hahn commented 11 years ago

Here is the http server example rewritten in the new language ...

createServer. ( require 'http' ) cb
drop ( listen. @ 1337 '127.0.0.1' )
dup drop wait . 'Server running at http://127.0.0.1:1337/'
    ( writeHead. @ 200 `{'Content-Type':'text/plain'}` )
    ( end.       @ 'Hello World'                       )

I am very pleased with how much more readable it is. Some comments ...