jococo / sallied-forth

A forth-a-like interpreter written in JavaScript for the browser.
MIT License
10 stars 1 forks source link

The word 'js->' for executing JavaScript functions is a bit ugly/unwieldy. #3

Closed jococo closed 10 years ago

jococo commented 10 years ago

Need thoughts on what to replace it with.

So far I have:

I'm guessing that I need to keep the 'js' part of the name to tie in with 'js@' and 'js!' and to cause least confusion?

jococo commented 10 years ago

Another option is to just send a String of valid JavaScript i.e. js( console.log("Look Ma!, I made a thing!"); ) I'm not keen on this approach though. Already confused myself with all the brackets and I don't think this would work without evil.

jococo commented 10 years ago

Or another option is to hide the JS part of the calls completely and use the regular '@', '!', 'exec' & 'find'. This would entail a patch where, if a word isn't found in the search stack of word-lists (ahem..), then it would be passed to a find that checked to see if the word was available in the JavaScript context (or valueStore if that's where we are saving it)

In particular this would mean: @ document would fetch the browser document [ deleteBtn ] document.getElementById would call the JavaScript function and return its result.

This is quite an interesting idea but I will have to think it through. Do like the fact that we are reducing extra syntax but not sure about hiding implementation detail.

jococo commented 10 years ago

OK in commit ce01b1edd4449c1a41f6ae87a0dda5317a4c5782 have gone with using the existing forth words where possible. The JS context is merged with the valueStore at initialisation of the interpreter so ! @ and calls to javascript functions work the same as forth stored values.

For JavaScript calls that don't have parameters or return values then you can just call the function by name exactly the same as any other forth word.

To call a function with parameters, prefix it with an array of the parameters:

[ Billy 99 ] js functionName where 'functionName' is the JavaScript function.

and, for functions that return a value:

[ 77 88 99 ] js- functionName which will push the return value on the stack or the JavaScript 'undefined' value if there was nothing returned.

[] is a forth word which pushes an empty array on the stack and can be used if you are sending no parameters but need a return value.

Also, 'console.log' works now and entailed a patch to ensure it is called with the right context.