Skip to content

Comment on Radiation-hardened Quine (2014)parent

Comments

This project has always fascinated me, so I tried making a tiny uroboros quine with Python -> Javascript -> Python -> etc. I started with a quine for each language, and tried inserting them into each other, but the output always grew exponentially.

    py_quine_template = "s = {}; print(s.format(repr(s)))"
    py_quine = py_quine_template.format(repr(py_quine_template))
    
    js_quine_template = "s = {}; console.log(s.replace('{}', JSON.stringify(s)))"
    js_quine = js_quine_template.format(repr(js_quine_template))

    py_js_quine = py_quine_template.format(repr(js_quine_template))
    ???
So I checked how it's done in the Github repo[1], but the language snippets only seemed to be quoting and printing without any self-references like you'd expect in a quine...

Then it hit me. You only need the quine machinery in one language. If I can make a Python script that has a string with its own source code, then the Javascript code will simply be `console.log(py_source)`, no manipulation necessary.

So here's my Python/Javascript uroboros quine:

    s = "s = {}\npy_source = s.format(repr(s))\nprint('console.log('+repr(py_source)+')')"
    py_source = s.format(repr(s))
    print('console.log('+repr(py_source)+')')
If there were more languages, then it'd be `console.log("System.out.println({python_source})")`, etc. The problem then becomes escaping inner quotes, and escaping the escape characters themselves. I managed to sidestep the issue by using two types of quotes, and relying on Python's `repr` also giving valid JS strings, but if I had to add just one more language I'd need a lot more scaffolding.

I still think the Quine Relay is a tour de force, but now for different reasons. It's not 128 quines in different languages, but an incredibly robust system for escaping strings in 128 wildly different languages.

[1] https://github.com/mame/quine-relay/blob/master/src/code-gen...

but an incredibly robust system for quoting and escaping strings in 128 wildly different languages

I'm getting a tension headache just thinking about it.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.