From: John Lam Date: 2005-12-05T13:50:59+09:00 Subject: Re: IL Generator syntax proposal ------=_Part_32086_21553279.1133758250113 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline I had some free cycles this evening to do a first pass implementation of some of the ideas that Wayne proposed, and here's the net result: require 'RbDynamicMethod' include RbDynamicMethod include ILGenerator create_ruby_method('say_hello') do ldstr 'Hello, World' call 'static System.Console.WriteLine(System.String)' ldc_i4_4 ret end say_hello This code generates a Ruby method with a VALUE (int, VALUE*, VALUE) signature. The create_ruby_method yields to the block, and uses method_missing to generate the IL instructions. Once the block has finished executing, I bake the dynamic method into the RbDynamicMethod module. Overall I'm pretty happy with this syntax. I'm attaching the implementation of ILGenerator below for those who are curious. The MC++ part (RbDynamicMethod) weighs in at about 160 lines of code. Comments appreciated! Thanks -John http://www.iunknown.com module ILGenerator include RbDynamicMethod @@op_codes =3D { 'ldstr' =3D> 's', 'call' =3D> 'm', 'ret' =3D> nil, 'ldc_i4_4' =3D> nil } def create_ruby_method(name) method =3D create_dynamic_method('', 'System.UInt32', 'System.Int32, System.UInt32*,System.UInt32') @@g =3D get_cil_generator(method) yield if block_given? define_ruby_method(method, name) end def parse_method_ref(sig) re =3D /(static )?(.*)\.(\w+)\((.*)\)/ m =3D re.match(sig) is_static =3D m[1] =3D=3D 'static ' type_name =3D m[2] method_name =3D m[3] parameters =3D m[4] return is_static, type_name, method_name, parameters end def method_missing(name, *parameters) op_code =3D name.to_s type =3D @@op_codes[op_code] case type when 's': emit_string(@@g, op_code, parameters[0]) when 'm': emit_method_ref(@@g, op_code, *parse_method_ref(parameters[0])) when nil: emit(@@g, op_code) end end end ------=_Part_32086_21553279.1133758250113--