Spin1/Ruby polyglot

So for some reason I wondered: "Can you make a file that's both a Spin program and a Ruby program"
Then I passed out and an hour later I woke up with this in my editor (hey forum admins, why is there still no button for the code block?):
''# The amazing Ruby/Spin polyglot!
''; puts "Henlo World!"
''; text = %{
CON

_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000

OBJ
pst : "parallax serial terminal"

PUB main
  pst.start(115_200)
  repeat
    pst.str(string("Henlo world!",13))

''}

'';p text

Comments

  • Wuerfel_21Wuerfel_21 Posts: 270
    edited 2019-03-15 - 22:45:09
    I also managed to find a way to include more than just oneliners of ruby:
    ''# The amazing Ruby/Spin polyglot!
    ''; puts "Henlo World!"
    {{ } => 0
    }
    10.times { |x|
      p x
    }
    ''; { 0 => {
    }}
    ''; text = %{
    CON
    
    _clkmode = xtal1 + pll16x
    _xinfreq = 5_000_000
    
    OBJ
    pst : "parallax serial terminal"
    
    PUB main
      pst.start(115_200)
      repeat
        pst.str(string("Henlo world!",13))
    
    ''}
    
    '';p text
    
  • :cool:
    Stockholm, Sweden
    I am only an egg -- Stranger in a Strange land, Robert A. Heinlein
  • What is Ruby?
    That's not an explosion! That's an unscheduled rapid disassembly!
  • Infernal Machine
  • So, @Wuerfel_21, how on earth did you do this? Are you compiling Ruby with Spin in the Propeller Tool???
    That's not an explosion! That's an unscheduled rapid disassembly!
  • nah, the trick is quite simple - abusing the syntax of both languages so that they read the other language code either as comments/strings or ineffective statements. It makes more sense when you actually paste it into an editor with syntax highlighting.
    I don't think there is really anything useful one can do with this, but it's cool, still.
  • So, it doesn’t compile?
    That's not an explosion! That's an unscheduled rapid disassembly!
  • It does, but it doesn't do anything cool. The cool part is that the same file compiles succesfully in Ruby and Spin and does the same thing (print out "Henlo World!").
  • The trick is that the Spin code is a comment in Ruby, and the Ruby code is a comment in Spin.

    -Phil
    “Perfection is achieved not when there is nothing more to add, but when there is nothing left to take away. -Antoine de Saint-Exupery
  • 🤯
    That's not an explosion! That's an unscheduled rapid disassembly!
Sign In or Register to comment.