
require 'rb/yasm'

yasm = YARVCore::Assembler.new

=begin
m1 = yasm.method(:m1, :a, :b){|x|
  x.putself
  x.getlocal :a
  x.getlocal :b
  x.newarray 2
  x.send :p, 1
  x.end
}

top = yasm.top{|x|
  x.putself
  x.putstring "hello yasm"
  x.send      :p, 1
  x.methoddef :m1, m1
  x.putself
  x.putobject 100
  x.putobject 200
  x.send      :m1, 2
  x.end
}

top = yasm.top{|x|
  x.putself
  x.putstring "hello yasm"
  x.send      :p, 1
  x.methoddef :m1, yasm.method(:m1, :a, :b){|y|
    y.putself
    y.getlocal :a
    y.getlocal :b
    y.newarray 2
    y.send :p, 1
    y.end
  }

  x.putself
  x.putobject 100
  x.putobject 200
  x.send      :m1, 2
  x.end
}
=end

#
# def m3(a)   # abs
#   if a > 0
#     a
#   else
#     -a
#   end
# end
abs = yasm.method(:abs, :a){|x|
  x.getlocal   :a
  x.putobject  0
  x.send :>,   1
  x.unless     :lelse
  x.getlocal   :a
  x.jump       :lend
x.label      :lelse ##
  x.getlocal   :a
  x.send       :-@, 0
x.label      :lend  ##
  x.end
}

top = yasm.top{|x|
  x.methoddef :abs, abs
  x.putself
  x.putobject 10
  x.send      :abs, 1
  x.send      :p, 1
  
  x.putself
  x.putobject -10
  x.send      :abs, 1
  x.send      :p, 1
  x.end
}



puts top.disasm
puts '-- '
YARVCore::eval_parsed top

