Arc Ported to JavaScript
halogen.note.amherst.edu
halogen.note.amherst.edu
Continuations are not implemented. I had started on a CPS-based interpreter, but then realized that JavaScript lacks tail-call optimization and has a recursion limit of 1000 calls, which'll make any CPS-transformation blow the stack.
looks like a good piece of code otherwise, though.
http://marijn.haverbeke.nl/codemirror/story.html
I'm not sure how relevant it is to implementing Arc, but never say never. JavaScript is Turing complete, after all...
It'll mean that any loop that may call back into the interpreter has to be rewritten as a tail-recursive function though, with the tail call being saved into the global continuation variable. Could be quite a bit of overhead.
Chris Double suggested another alternative over on Proggit: tailcall to window.setTimeout with a delay of 0. This is effectively a trampoline; the continuation gets pushed onto a timer, the eval function returns, then JavaScript picks up again where it should. As a nice side effect, it gives the browser's event loop a chance to run, which eliminates any chance of locking up the machine. Pretty elegant solution, though I'm guessing there's a significant performance penalty.
this seems to be the first implementation of Arc in a language other than Scheme. I'm curious if others will follow.
> wc [^j]*.js
66 147 1359 buffer.js
369 1114 10672 eval.js
149 488 4522 primitives.js
188 509 4989 reader.js
356 1145 11869 types.js
136 370 3260 utils.js
1264 3773 36671 total
> wc [^d]*.scm
1093 4404 36047 ac.scm
16 30 242 as.scm
48 152 1252 brackets.scm
1157 4586 37541 total
So ArcLite is about 100 lines of code more. OTOH, if you take out the reader (which is supplied by Scheme but I had to write myself for ArcLite) it's -254 lines for ArcLite and -48 lines for Arc0. Which means the JavaScript version is actually smaller, at least in line count. (There's also the matter of the missing I/O primitives, but most of them just delegate to Scheme in Arc0 as well.)I have no idea how it compares in code-tree count. Probably larger, as my JavaScript coding style is fairly dense. One could argue that that's the point of syntax though; it makes common operations take up very little space on the page.
I just played around with it quickly, pasted in the recursive definition of factorial:
(def fact (x) (if (< x 1) 1 (* x (fact (- x 1)))))
It was fairly quick, but the results came back as floating point numbers. It broke on (fact 100).
Under arc-over-scheme it did better.
CLISP performed better than both, but the default limit in the call depth was easily exceeded (E.g. it busted on (fact 8000)). I don't recall exactly, but for around fact 5000 CLISP seemed about twice as fast as arc-over-scheme.
These are very rough estimates, but perhaps give people an intuition about performance.
Now what I am really waiting for is the arc-over-CLISP. I am too busy with other things to dare an attempt...
I'm truly amazed you got it to work up to (fact 100), and that it came back in a reasonable amount of time. Firefox has a recursion limit depth of 1000, which means that the interpreter overhead is only about a factor of 10. I was expecting much more...
document.getElementById("input").addEventListener("keypress", function(e) {
e = e || window.event;
if (e.keyCode == 13) {
$('#eval').click();
e.target.value = "";
}}, false);
...or the equivalent in jQuery. Not IE compatible (no, I don't care)In the meantime, here's a bookmarklet you can use to "patch" it:
javascript:(function(){document.getElementById("input").addEventListener("keypress", function(e) { e = e || window.event; if (e.keyCode == 13) { $('#eval').click(); e.target.value = ""; }}, false);})();