News for hackers. Stuff that matters.
Re:Show HN: a compiler from Python syntax to JavaScript synt... by daeken on Friday October 29, 2010 @06:05PM (#1849236)
I built something similar a while back (guess it's been in use for about two years now -- built it to support my company's product): http://github.com/daeken/Pyvascript

Key difference between Pyvascript and the other compilers I've seen is a facility for macros performing arbitrary AST manipulations. E.g. in the Pylons environment, you can use _uriOf(Controller.someAction) to have it generate a url in your JS code at compile-time.

Always nice to see competition among compilers, though.

Re:Show HN: a compiler from Python syntax to JavaScript synt... by jiaaro on Friday October 29, 2010 @03:59PM (#1848779)
I think that python is sufficiently different from javascript that you'll need to provide your own documentation beyond explaining the transformation.

Coffeescript is mostly just javascript shorthand; Python on the otherhand has language features with no javascript equivalent (I'm thinking keyword arguments, generator functions, classes, and especially super() here)

That being said, I think to make this project really work you're going to be responsible for documenting the dom, and providing support for libraries like jQuery.

Re:Show HN: a compiler from Python syntax to JavaScript synt... by s3graham on Friday October 29, 2010 @04:02PM (#1848784)
At first blush, I'd say this similar to Coffeescript in that it's meant to be javascript shorthand also.
Re:Show HN: a compiler from Python syntax to JavaScript synt... by endian on Friday October 29, 2010 @04:06PM (#1848800)
Exactly. It's purely syntax to syntax.

Thanks for the confusion -- I've made the first paragraphs more clear about PJ's scope.

Title changed: s/compiler/syntax cross-compiler/ EDIT: title changed again

Re:Show HN: a compiler from Python syntax to JavaScript synt... by scott_s on Friday October 29, 2010 @04:41PM (#1848938)
That's not a term I've ever heard, and "cross compiler" tends to imply something else: http://en.wikipedia.org/wiki/Cross_compiler

I would call what you have done a source-to-source compiler: it translates from one target high-level language to another. http://en.wikipedia.org/wiki/Source-to-source_compiler

Re:Show HN: a compiler from Python syntax to JavaScript synt... by s3graham on Friday October 29, 2010 @04:14PM (#1848839)
I guess defining what you consider syntax features vs. Python language/runtime would be appropriate for the docs.

ie. 2 * * 1024? print object.__mro__? slice objects? __call__? etc.

Re:Show HN: a compiler from Python syntax to JavaScript synt... by endian on Friday October 29, 2010 @04:39PM (#1848929)
Here's the decision procedure:

If there's no collateral damage and the JS is clear (e.g. 28 --> Math.pow(2, 8)): do it.

If there's no proper way to express the JS in Python (e.g. instanceof and typeof keywords): do it

If it's really important (only "print" and "self"): do it

Else: DON'T

These few special cases that mess with your code are all mentioned in a section on the the main page.

And you can easily remove them from the transformations if you want.

Re:Show HN: a compiler from Python syntax to JavaScript synt... by extension on Friday October 29, 2010 @08:02PM (#1849570)
Here's my thing like this for Ruby:

http://github.com/jedediah/prettyscript

I abandoned it when CoffeeScript appeared, but you might find something interesting there.

Re:Show HN: a compiler from Python syntax to JavaScript synt... by tjarratt on Friday October 29, 2010 @04:23PM (#1848875)
This is a really neat idea, and I'm definitely going to look into this more when I have time, but I have a small nit to pick.

print(...) is a common debugging, or output call in python, and I can see how in many use cases that would translate to console.log in javascript.

However, some browsers don't support console.log, and in serverside javascript, objects like console and window are not available. I don't know of any common output technique that would work in all browsers as well as with node.js and ringo. Depending on how much you care about supporting serverside js and different browsers, this may be a non-issue.

Re:Show HN: a compiler from Python syntax to JavaScript synt... by endian on Friday October 29, 2010 @04:31PM (#1848899)
Thanks. I'll change

  pj.transformations.special.Call_print
to have the emitted code check if you've defined a print function, using console.log only if you haven't.

  ((typeof print !== "undefined" && print !== null) || console.log)(...)
Re:Show HN: a compiler from Python syntax to JavaScript synt... by endian on Friday October 29, 2010 @03:50PM (#1848736)
Feedback welcome. And if you encounter any problems, I'll fix 'em quickly.
Re:Show HN: a compiler from Python syntax to JavaScript synt... by JeffJenkins on Friday October 29, 2010 @09:27PM (#1849767)
I wrote one of these for a project. The trouble is that to write functioning python code, even for just the primitives, you have to add a lot of extra scaffolding. I eventually stopped working on it since Pyjamas seemed to be doing a much more complete job (http://pyjs.org/).

I may still write a limited one for use in creating code blocks in my project MongoAlchemy (http://mongoalchemy.org/).

Re:Show HN: a compiler from Python syntax to JavaScript synt... by reinhardt on Friday October 29, 2010 @09:18PM (#1849741)
A more conservative (i.e. closer to the generated javascript) approach is pythonicjavascript (http://code.google.com/p/pythonicjavascript/). Personally I prefer making Javascript look more like Python (http://classy.pocoo.org/) than creating yet another language variant with subtle differences from the original.
Re:Show HN: a compiler from Python syntax to JavaScript synt... by random42 on Friday October 29, 2010 @04:17PM (#1848852)
For which use cases this compiler would be helpful? I believe python and JavaScript are typically used for different purposes.

That being said, it looks fantastic. :)

Disclaimer - I do not know JavaScript.

Re:Show HN: a compiler from Python syntax to JavaScript synt... by hasenj on Friday October 29, 2010 @06:24PM (#1849300)
Better way of writing javascript (just like coffe script).

You can embed this in a web app and write your client-side code in python-syntax. Would work specially well if you server-side code is also in python.

Re:Show HN: a compiler from Python syntax to JavaScript synt... by plinio_silva on Friday October 29, 2010 @05:33PM (#1849127)
This would be fantastic to write validation code, because the same code can be used on both the server and client sides. Thanks endian for sharing this with us, I can't wait to try it out!
Re:Show HN: a compiler from Python syntax to JavaScript synt... by endian on Friday October 29, 2010 @05:05PM (#1849029)
PJ is a way to do your JavaScript coding using a less horrible syntax.
Re:Show HN: a compiler from Python syntax to JavaScript synt... by bhiggins on Friday October 29, 2010 @04:27PM (#1848887)
on the flip side, here's an experiment for converting JavaScript to Python, and then if it turns out to be valid RPython (a subset of Python where among other things the types of variables don't change), converts it to a native executable.

http://mozakai.blogspot.com/2010/07/experiments-with-static-...