Live data from Hacker News

Arc Ported to JavaScript

halogen.note.amherst.edu

21–25 of 25 posts

Re: Arc Ported to JavaScript

#21

Earlier quoted context omitted.

Tab-return?

Yeah, that's what I do. I couldn't figure out how to get accesskeys to take "return" as the key. If anyone knows a better solution, I can change it.

    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);})();

Re: Arc Ported to JavaScript

#22

Earlier quoted context omitted.

This guy implemented a continuation-based syntax highlighter in JavaScript: 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...

That approach could work. He's storing the current continuation as a global variable, which I never thought of, because typically you want your interpreters to be self-contained. (I was also working off Guy Steele's toy CPS interpreter, which passes cont in and then ends each function with a tailcall to another function or the invocation of the continuation.) But I can't see any real reason why the global won't work;…

I believe you can use the "with" operator in JavaScript to use any object as the global object (normally "window" in the browser). I'm not sure how useful that could be, but it might help make it self contained.

Re: Arc Ported to JavaScript

#23

Earlier quoted context omitted.

That approach could work. He's storing the current continuation as a global variable, which I never thought of, because typically you want your interpreters to be self-contained. (I was also working off Guy Steele's toy CPS interpreter, which passes cont in and then ends each function with a tailcall to another function or the invocation of the continuation.) But I can't see any real reason why the global won't work;…

I believe you can use the "with" operator in JavaScript to use any object as the global object (normally "window" in the browser). I'm not sure how useful that could be, but it might help make it self contained.

"with" is dangerous, because it makes it impossible to tell lexically whether you're assigning to an object or to a global variable. If you mistype a variable or pass an object of the wrong type in to "with", you can find yourself with very difficult-to-track-down bugs. In practice I'd only use it with a bunch of assertions, which could bloat the code significantly.

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.

Re: Arc Ported to JavaScript

#24

Earlier quoted context omitted.

This guy implemented a continuation-based syntax highlighter in JavaScript: 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...

That approach could work. He's storing the current continuation as a global variable, which I never thought of, because typically you want your interpreters to be self-contained. (I was also working off Guy Steele's toy CPS interpreter, which passes cont in and then ends each function with a tailcall to another function or the invocation of the continuation.) But I can't see any real reason why the global won't work;…

That is not a global variable, it is a closed-over variable, which, in terms of self-containedness, is a whole different beast. See also http://marijn.haverbeke.nl/cps/ .

Re: Arc Ported to JavaScript

#25

an important limitation of javascript means this will never quite be a complete implementation: 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.

You can fake tail calls with window.setTimeout(continuation, 0)
Post reply on HN