[pyar] OT: Programa poliglota (habla python).

Sebastian Bassi sbassi en clubdelarazon.org
Dom Jun 13 23:00:45 ART 2010


polyglot program: Python, Lua, and Ruby*Kragen Javier Sitaker* kragen at
canonical.org <kragen-hacks%40canonical.org?Subject=polyglot%20program%3A%20Python%2C%20Lua%2C%20and%20Ruby&In-Reply-To=>
*Sun Jun 13 05:57:37 EDT 2010*


   - *Messages sorted by:* [ date ] <date.html#501> [ thread ]<thread.html#501>
    [ subject ] <subject.html#501> [ author ] <author.html#501>

------------------------------

This program parses and runs successfully in Lua, Ruby, or
Python. It’s the first polyglot program I’ve seen that includes
Lua. Is it possible to add more languages to it?

x=1 or 1--"""
print('hello, world, from Lua!')
x=[["
puts 'hello, world, from Ruby!'
"""
print 'hello, world, from Python!'
#"#]]

(End of `pyluaglot.min`.)

Here's a less minimal version with comments:

# A polyglot program that runs in either Python or Lua.
x = 1 or 1 -- """ Begin a Python/Ruby multiline string inside a Lua comment.
-- In Python or Ruby, the expression parses as 1 or (1 - (- 'some string')).
-- As it happens, strings don't have an unary minus operator defined for them,
-- and if they did, its result probably couldn't be subtracted from 1.
-- But that would only matter if the second half of the 'or' expression
-- were to be evaluated. And it's not.
-- The 'x =' part is to make the expression '1 or 1' into a statement
-- in Lua, which doesn't have expression statements.
-- See http://codegambler.wordpress.com/2009/10/04/polyglotsingle-program-runs-on-multiple-languages/
-- and http://osdc.tw/2010/02/schedule_polyglot_programming.html
-- and http://www.nyx.net/~gthompso/poly/polyglot.htm
-- for techniques that might permit adding some other languages.
print('hello, world, from Lua!')
x = [[ " # end the Ruby (but not the Python) multiline string inside a
Lua multiline string.
# Now, until we do it again, Python and Ruby are out of phase.
# Thanks to http://www.riffraff.info/2009/1/16/life-polyglot-ruby-python
for the technique!
puts 'hello, world, from Ruby!'
"""
print 'hello, world, from Python!'
#"#]] -- end the Ruby multiline string inside a Python comment and the
Lua multiline string inside a Python/Ruby comment.

(End of `pyluaglot`.)

Like everything else posted to kragen-hacks without a notice to the
contrary, this software is in the public domain.
------------ próxima parte ------------
Se ha borrado un adjunto en formato HTML...
URL: <http://listas.python.org.ar/pipermail/pyar/attachments/20100613/17482510/attachment.html>


More information about the pyar mailing list