Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to hy2py that converts symbols to valid python identifiers #970

Closed
gilch opened this issue Oct 16, 2015 · 4 comments · Fixed by #1517
Closed

add option to hy2py that converts symbols to valid python identifiers #970

gilch opened this issue Oct 16, 2015 · 4 comments · Fixed by #1517
Labels

Comments

@gilch
Copy link
Member

gilch commented Oct 16, 2015

Hy would be more useful if it could compile to Python, and not just ast, see #923.

hy2py can translate ast back into Python source. It's very useful as a debugging tool, but the result is not always executable. The main issue seems to be that Hy identifiers are a superset of valid Python identifiers. The ast doesn't care, but when importing Hy from Python we have a system to resolve this problem using Punycode and a few other rules (- to _ etc.)

I'm proposing we add an option to hy2py that converts all invalid symbols using the same system. This way the Python code will be still be executable. I'm not sure if there are other problems with the output, but it's a step in the right direction.

This might also be more useful as the default for hy --spy.

@refi64
Copy link
Contributor

refi64 commented Oct 16, 2015

I would argue against that being the default for hy --spy, since my own main goal of using that is to see exactly what Python code is generated.

This is a great idea; I've had it on my mind for a year or so. Do note that it's more complicated than it seems. You can't just mangle every name you see:

(defn $ [a b]) ; this can be mangled to whatever
(reduce + [1 2]) ; this would add an extra import from `hy.shadow`, and mindlessly mangling that will give an ImportError
(import x [$$]) ; same here

Basically, in particular, imports need extra attention.

@gilch gilch added the feature label Oct 16, 2015
@koji-kojiro
Copy link

koji-kojiro commented May 8, 2017

I propose that use foo = getattr(bar, "foo") instead of from bar import foo.

The second case, (reduce + [1 2]) becomes:

import hy.core.shadow
+ = getattr(hy.core.shadow, "+") 
# equivalent to `from hy.core.shadow import +`

reduce(+, [1L, 2L])
# we can replace '+' with any valid identifier

@refi64
Copy link
Contributor

refi64 commented May 8, 2017

Only problem is that that breaks any usage of globals and locals:

(setv + 123)
(print (get (globals) "+"))  ; BOOM!!

Though I guess we already mangle ! and ?...

@koji-kojiro
Copy link

That might be solved as follows:

globals().update({'+': 123})
+ = globals().get('+')
# equivalent to `+ = 123`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants