joelibaceta
5/11/2016 - 12:37 AM

low_level_hello_world.rb

codigo = " p 'hello world' "

pp Ripper.lex(codigo) # Paso 1, Tokenizacion

# [[[1, 0], :on_sp, " "],
#  [[1, 1], :on_ident, "p"],
#  [[1, 2], :on_sp, " "],
#  [[1, 3], :on_tstring_beg, "'"],
#  [[1, 4], :on_tstring_content, "hello world"],
#  [[1, 15], :on_tstring_end, "'"],
#  [[1, 16], :on_sp, " "]]

pp Ripper.sexp(codigo)  # Paso 2, Parseo

# [:program,
#  [[:command,
#    [:@ident, "p", [1, 1]],
#    [:args_add_block,
#     [[:string_literal,
#       [:string_content, [:@tstring_content, "hello world", [1, 4]]]]],
#     false]]]]

puts RubyVM::InstructionSequence.compile(codigo).disasm  # Compilacion y ejecucion a bajo nivel

# == disasm: <RubyVM::InstructionSequence:<compiled>@<compiled>>==========
#     0000 trace            1                                               (   1)
# 0002 putself
# 0003 putstring        "hello world"
# 0005 opt_send_without_block <callinfo!mid:p, argc:1, FCALL|ARGS_SIMPLE>
#     
# 0007 leave