2 modes of the compiler

11 views
Skip to first unread message

Ondrej Certik

unread,
Mar 2, 2011, 12:59:22 PM3/2/11
to py...@googlegroups.com
Hi,

after the discussion with Christian, here is one point that we both
seemed to converge upon. There need to be 2 modes of the compiler,
syntax and semantics.

1) Syntax: Python syntax to JavaScript, e.g.: a.foo("5", b) -> a.foo("5", js(b))
2) Semantics: Python language conversion to JavaScript, e.g.:
a.foo("5", b) -> a.__getattr__(str("foo")).__call__(str("5"), b)

Both things have to be implemented in the compiler. 2) Would be used
by default, and 1) would be used for interfacing external JavaScript
libraries (notice the automatic insertion of the js() method). Later
on, the py2js might optimize some code and use 1) instead of 2)
automatically to make things faster, but that I would not worry about
for now.

There needs to be some way to tell py2js to use 1) instead of 2). Here
are couple options that we discussed:

a)

b = {}
a.js__foo(5, b)

b)

JS("a").foo(5, b)

c)

from __js__ import a

b = {}
a.foo(5, b)

d)

@py2js("a")
def f():
b = {}
a.foo(5, b)


From these options, I like the c) the most, because if you provide
Python implementation of the __js__ module, e.g. providing some Python
implementation of let's say jQuery (or browser handling), then you can
even run your code from Python on your desktop. And it is like the
__future__ in Python, simply by importing "form __js__ import
something", it will be clear to everybody, that "something" will
receive a special treatment in the translation.

Ondrej

Samuel Ytterbrink

unread,
Mar 2, 2011, 1:20:23 PM3/2/11
to py...@googlegroups.com
im not sure if c) is suppose to be what i men't when talking to chrivers.
any ways its not...

@javascriptSpace
def f():
  b = {}
  a.fo(5,b)

or
f = javascriptSpace(lambda x,y : a(a,y))
f({}, 5)

//Samuel Ytterbrink

2011/3/2 Ondrej Certik <ond...@certik.cz>

Ondrej Certik

unread,
Mar 2, 2011, 1:46:53 PM3/2/11
to py...@googlegroups.com
Hi Samuel,

On Wed, Mar 2, 2011 at 10:20 AM, Samuel Ytterbrink <nep...@gmail.com> wrote:
> im not sure if c) is suppose to be what i men't when talking to chrivers.
> any ways its not...

thanks for pointing out even more ways to do it. So this is

e)

> @javascriptSpace
> def f():
>   b = {}
>   a.fo(5,b)
> or

f)


> f = javascriptSpace(lambda x,y : a(a,y))
> f({}, 5)

The e) is good for switching to the mode 1) for the whole function,
which will be needed in order to call the generated code *from*
JavaScript (and py2js can remember, that f() is using mode 1). And f)
seems like e), just a different way to do it.

Ondrej

Neppord

unread,
Mar 2, 2011, 8:18:18 PM3/2/11
to py...@googlegroups.com
You need also a Pythonspace function which for the length of the decorator go back to the other mode.

//Samuel Ytterbrink

Reply all
Reply to author
Forward
0 new messages