lib/nydp/builtin/ruby_wrap.rb in nydp-0.5.1 vs lib/nydp/builtin/ruby_wrap.rb in nydp-0.6.0

- old
+ new

@@ -6,18 +6,18 @@ end class Coder < Struct.new(:name, :size, :code, :helpers) def msize ; size + 1 ; end - def arg_mapper + def arg_mapper_novm case size when 0 ; "" - when 1 ; ", a0" - when 2 ; ", a0, a1" - when 3 ; ", a0, a1, a2" - when 4 ; ", a0, a1, a2, a3" - when 5 ; ", a0, a1, a2, a3, a4" + when 1 ; "a0=nil" + when 2 ; "a0=nil, a1=nil" + when 3 ; "a0=nil, a1=nil, a2=nil" + when 4 ; "a0=nil, a1=nil, a2=nil, a3=nil" + when 5 ; "a0=nil, a1=nil, a2=nil, a3=nil, a4=nil" else ; raise "maximum 5 arguments!" end end def to_ruby @@ -29,16 +29,22 @@ gsub(/a4/, "args.cdr.cdr.cdr.cdr.car") <<CODE class #{name} include Nydp::Builtin::Base, Singleton#{helpers} - def builtin_invoke_#{msize} vm#{ arg_mapper } - vm.push_arg(#{code}) + def builtin_call #{ arg_mapper_novm } + (#{code}) end - def builtin_invoke vm, args - vm.push_arg(#{generic_code}) + # return the ruby equivalent of this code if it was inlined inside another builtin + def inline_code arg_expressions + #{code.inspect}. + gsub(/a0/, arg_expressions[0]). + gsub(/a1/, arg_expressions[1]). + gsub(/a2/, arg_expressions[2]). + gsub(/a3/, arg_expressions[3]). + gsub(/a4/, arg_expressions[4]) end end CODE end end @@ -63,10 +69,17 @@ def self.builder includes WrapperBuilder.new(includes) end core_builder = builder "" - core_builder.build(:Cons, 2, %{ Nydp::Pair.new(a0, a1) } ) - core_builder.build(:Car , 1, %{ a0.car } ) - core_builder.build(:Cdr , 1, %{ a0.cdr } ) - core_builder.build(:Log , 1, %{ r2n Nydp.logger.info(a0.to_s) } ) + core_builder.build(:Cons , 2, %{ Nydp::Pair.new(a0, a1) } ) + core_builder.build(:Car , 1, %{ a0.car } ) + core_builder.build(:Cdr , 1, %{ a0.cdr } ) + core_builder.build(:Ln , 1, %{ Math.log(a0) } ) + core_builder.build(:Modulo , 2, %{ a0 % a1 } ) + core_builder.build(:Sqrt , 1, %{ Math.sqrt a0 } ) + core_builder.build(:Regexp , 1, %{ ::Regexp.compile(a0) } ) + core_builder.build(:StringPadLeft , 3, %{ a0.to_s.rjust(a1, a2.to_s) } ) + core_builder.build(:StringPadRight, 3, %{ a0.to_s.ljust(a1, a2.to_s) } ) + core_builder.build(:ToList , 1, %{ a0.to_a._nydp_wrapper } ) + core_builder.build(:StringForceEncoding, 2, %{ a0.to_s.force_encoding(a1)._nydp_wrapper } ) end