Skip to content

Made Polycompiler Recursive (aka add bin/merge.py.js) #7

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

lennyerik
Copy link

With minimal changes to the Javascript file (using require() instead of import and switching from modules to commonjs) and using the Python implementation from #3 we can make polycompiler able to recursively compile itself. The compiled file bin/merge.py.js can be executed using both python and nodejs.

@Zvitorep
Copy link

Also, another thing I wanted to change is the actual polylang structure.
I think these two formats are nicer than using the original comment + lambda structure

eval(["console.log('Hello, js!')", "print('Hello, py!')"][(-1 % 2 + 1) >> 1])

or

eval(["print('Hello, py!')", "console.log('Hello, js!')"][1|0==2])

@Zvitorep
Copy link

Zvitorep commented Mar 17, 2025

^ Should be done using eval("exec(...)") inside the python segment to allow loops and other multi-line syntax, and maybe decoding the actual code with base64 to ensure proper functionality (avoiding new-line/char escaping hell)

@Zvitorep
Copy link

@lennyerik ?

bin/merge py Outdated

def merge_files(python_content, js_content):
# Escape backslashes first, then newlines, then quotes
escaped_python_content = python_content.replace('\\', '\\\\').replace('\n', '\\n').replace('"""', '\\"\\"\\"')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with b64 encoding, this escaping logic could be avoided entirely

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would also fix #4

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that seems like a good idea if it fixes newline handling then

@Zvitorep
Copy link

Also, the compilers should strip comments IMO, to save up space

@lennyerik
Copy link
Author

Yeah, that sounds good, although I can't really say anything on project direction as this is obviously not my project either... :P
But as far as I understand it, the scope of this polylang compiler isn't really to save on space by stripping comments and rather just to enable us to compile js and python files into one polyglot file. I would have argued that stripping comments, tree-shaking, etc. is clearly a feature for a minifier. There is nothing stopping you from running the python and JS code through a bundler, obfuscator, minifier, etc. before running it through this tool.
Regarding base64, I'm not sure, that just makes the code more unreadable imo. But feel free to implement these things in another branch and open a PR, this one was really just me trying the silly idea of compiling the compiler with itself. :)

@lennyerik lennyerik changed the title Made Polycompiler Recursive Made Polycompiler Recursive (aka add bin/merge.py.js) Mar 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants