Skip to content

Instantly share code, notes, and snippets.

@hkdnet
Created September 18, 2017 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hkdnet/aebf584e8784061632d002108b18e025 to your computer and use it in GitHub Desktop.
Save hkdnet/aebf584e8784061632d002108b18e025 to your computer and use it in GitHub Desktop.
vernacular
$ bundle exec ruby exec.rb
== disasm: #<ISeq:<compiled>@src.rb>====================================
0000 trace 1 ( 1)
0002 putself
0003 putobject 10
0005 putobject 2
0007 opt_plus <callinfo!mid:+, argc:1, ARGS_SIMPLE>, <callcache>
0010 opt_send_without_block <callinfo!mid:puts, argc:1, FCALL|ARGS_SIMPLE>, <callcache>
0013 pop
0014 trace 1 ( 2)
0016 putself
0017 putobject 12
0019 opt_send_without_block <callinfo!mid:puts, argc:1, FCALL|ARGS_SIMPLE>, <callcache>
0022 leave
--------------------
12
12
require 'pry'
require 'vernacular'
Vernacular.configure do |config|
pattern = /~n\(([\d\s+-\/*\(\)]+?)\)/
modifier =
Vernacular::RegexModifier.new(pattern) do |match|
eval(match[3..-2])
end
config.add(modifier)
end
result = RubyVM::InstructionSequence::load_iseq('src.rb')
puts result.disasm
puts '-' * 20
result.eval
puts (10 + 2)
puts ~n(10 + 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment