node/lib/metasm/samples/dynamic_ruby.rb
def compile(ast, klass, meth, singleton=false)
		return if not ast

		# TODO handle arbitrary block/yield constructs
		# TODO analyse to find/optimize numeric locals that never need a ruby VALUE (ie native int vs INT2FIX)
		# TODO detect block/closure exported out of the func & abort compilation

		@klass = klass
		@meth = meth
		@meth_singleton = singleton

		mname = escape_varname("m_#{@klass}#{singleton ? '.' : '#'}#{@meth}".gsub('::', '_'))
		@cp.parse "static void #{mname}(VALUE self) { }"
		@cur_cfunc = @cp.toplevel.symbol[mname]
		@cur_cfunc.type.type = value	# return type = VALUE, w/o 'missing return statement' warning

		@scope = @cur_cfunc.initializer

		case ast[0]
		when :ivar	# attr_reader
			ret = fcall('rb_ivar_get', rb_self, rb_intern(ast[1]))
		when :attrset	# attr_writer
			compile_args(@cur_cfunc, [nil, 1])
			ret = fcall('rb_ivar_set', rb_self, rb_intern(ast[1]), local(2))
		when :scope	# standard ruby function
			@cref = ast[1][:cref]
			if ast[2] and ast[2][0] == :block and ast[2][1] and ast[2][1][0] == :args
				compile_args(@cur_cfunc, ast[2][1])
			end
			want_value = true
			if meth.to_s == 'initialize' and not singleton
				want_value = false
			end
			ret = ast_to_c(ast[2], @scope, want_value)
			ret = rb_nil if not want_value
		#when :cfunc	# native ruby extension
		else raise "unhandled function ast #{ast.inspect}"
		end

		@scope.statements << C::Return.new(ret)

		mname
	end
# convert a ruby AST to a new C function
# returns the new function name
convert a ruby AST to a new C function
returns the new function name
convert a ruby AST to a new C function returns the new function name
def compile(ast, klass, meth, singleton=false)
		return if not ast
		@klass = klass
		@meth = meth
		@meth_singleton = singleton
		mname = escape_varname("m_#{@klass}#{singleton ? '.' : '#'}#{@meth}".gsub('::', '_'))
		@cp.parse "static void #{mname}(VALUE self) { }"
		@cur_cfunc = @cp.toplevel.symbol[mname]
		@cur_cfunc.type.type = value	
		@scope = @cur_cfunc.initializer
		case ast[0]
		when :ivar	
			ret = fcall('rb_ivar_get', rb_self, rb_intern(ast[1]))
		when :attrset	
			compile_args(@cur_cfunc, [nil, 1])
			ret = fcall('rb_ivar_set', rb_self, rb_intern(ast[1]), local(2))
		when :scope	
			@cref = ast[1][:cref]
			if ast[2] and ast[2][0] == :block and ast[2][1] and ast[2][1][0] == :args
				compile_args(@cur_cfunc, ast[2][1])
			end
			want_value = true
			if meth.to_s == 'initialize' and not singleton
				want_value = false
			end
			ret = ast_to_c(ast[2], @scope, want_value)
			ret = rb_nil if not want_value
		else raise "unhandled function ast #{ast.inspect}"
		end
		@scope.statements << C::Return.new(ret)
		mname
	end
def compile ( ast , klass , meth , singleton = false ) return if not ast @klass = klass @meth = meth @meth_singleton = singleton mname = escape_varname ( "m_#{@klass}#{singleton ? '.' : '#'}#{@meth}" . gsub ( '::' , '_' ) ) @cp . parse "static void #{mname}(VALUE self) { }" @cur_cfunc = @cp . toplevel . symbol [ mname ] @cur_cfunc . type . type = value @scope = @cur_cfunc . initializer case ast [ 0 ] when :ivar ret = fcall ( 'rb_ivar_get' , rb_self , rb_intern ( ast [ 1 ] ) ) when :attrset compile_args ( @cur_cfunc , [ nil , 1 ] ) ret = fcall ( 'rb_ivar_set' , rb_self , rb_intern ( ast [ 1 ] ) , local ( 2 ) ) when :scope @cref = ast [ 1 ] [ :cref ] if ast [ 2 ] and ast [ 2 ] [ 0 ] == :block and ast [ 2 ] [ 1 ] and ast [ 2 ] [ 1 ] [ 0 ] == :args compile_args ( @cur_cfunc , ast [ 2 ] [ 1 ] ) end want_value = true if meth . to_s == 'initialize' and not singleton want_value = false end ret = ast_to_c ( ast [ 2 ] , @scope , want_value ) ret = rb_nil if not want_value else raise "unhandled function ast #{ast.inspect}" end @scope . statements << C :: Return . new ( ret ) mname end
convert a ruby AST to a new C function
returns the new function name
convert a ruby AST to a new C function returns the new function name
# TODO handle arbitrary block/yield constructs # TODO analyse to find/optimize numeric locals that never need a ruby VALUE (ie native int vs INT2FIX) # TODO detect block/closure exported out of the func & abort compilation # return type = VALUE, w/o 'missing return statement' warning # attr_reader # attr_writer # standard ruby function #when :cfunc	# native ruby extension
ast
klass
meth
singleton