Hatena::ブログ(Diary)

urekatのスカンク日記3 このページをアンテナに追加 RSSフィード

2012-04-28

mrubyのexamples

https://github.com/takeru/mruby/tree/examples/examples

までできた、のでpushed.

/*
 [step3] call ruby method from C.
*/

#include "mruby.h"
#include "mruby/proc.h"
#include <stdio.h>

void _error(const char* s){
  printf("ERROR: %s\n", s);
  exit(1);
}

void plus(mrb_state *mrb, mrb_value a, mrb_value b)
{
  mrb_value v;

  v = mrb_funcall(mrb, mrb_top_self(mrb), "plus", 2, a, b);

  printf("mrb_type(v)=%d\n", mrb_type(v));
  mrb_funcall(mrb, mrb_top_self(mrb), "p", 1, v);
}

int main()
{
  mrb_state *mrb;
  int n;
  FILE* f;
  mrb_value str;
  mrb_value z;

  mrb = mrb_open();
  f = fopen("step3.rb", "r");
  if(f==NULL){ _error("file not found."); }
  n = mrb_compile_file(mrb, f);
  fclose(f);
  mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_nil_value());

  mrb_funcall(mrb, mrb_top_self(mrb), "hello3", 0);

  str = mrb_str_new_cstr(mrb, "string from c-string");
  mrb_funcall(mrb, mrb_top_self(mrb), "puts", 1, str);
  mrb_funcall(mrb, mrb_top_self(mrb), "p", 1, str);

  z = mrb_funcall(mrb, mrb_top_self(mrb), "tak", 3,
    mrb_fixnum_value(24),
    mrb_fixnum_value(16),
    mrb_fixnum_value( 8)
  );

  str = mrb_str_new_cstr(mrb, "z=");
  mrb_funcall(mrb, mrb_top_self(mrb), "print", 2, str, z);
  mrb_funcall(mrb, mrb_top_self(mrb), "puts", 0);

  if(mrb_type(z) == MRB_TT_FIXNUM){
    int int_z = mrb_fixnum(z);
    printf("z=%d\n", int_z);
  }

  plus(mrb, mrb_fixnum_value(1),        mrb_fixnum_value(1)       );
  plus(mrb, mrb_str_new_cstr(mrb, "1"), mrb_str_new_cstr(mrb, "1"));

  mrb_close(mrb);
}

/*
% gcc -o step3 step3.c -I ../include -I ../src ../lib/ritevm.a -lm
% ./step3
welcome to step3.
hello3 called.
string from c-string
"string from c-string"
z=9
z=9
mrb_type(v)=3
2
mrb_type(v)=15
"11"
*/

puts "welcome to step3."

def hello3
  puts 'hello3 called.'
end

def tak x, y, z
  if y >= x
    return z
  else
    return tak( tak(x-1, y, z),
                tak(y-1, z, x),
                tak(z-1, x, y))
  end
end

def plus(a,b)
  a + b
end

/*
 [step4] call C function from Ruby.
*/

#include "mruby.h"
#include "mruby/proc.h"
#include "mruby/string.h"
#include <stdio.h>
#include <string.h>

void _error(const char* s){
  printf("ERROR: %s\n", s);
  exit(1);
}

mrb_value plus(mrb_state *mrb, mrb_value self)
{
  mrb_value arg1;
  mrb_value arg2;
  mrb_value ret;

  mrb_get_args(mrb, "oo", &arg1, &arg2);
  // ret = mrb_funcall(mrb, arg1, "+", 1, arg2);

  if(mrb_type(arg1)==MRB_TT_FIXNUM && mrb_type(arg2)==MRB_TT_FIXNUM){
    return mrb_fixnum_value(mrb_fixnum(arg1) + mrb_fixnum(arg2));
  }

  if(mrb_type(arg1)==MRB_TT_STRING && mrb_type(arg2)==MRB_TT_STRING){
    // printf("arg1=%s\n", mrb_string_value_cstr(mrb, &arg1));
    // printf("arg2=%s\n", mrb_string_value_cstr(mrb, &arg2));
    char* s1    = mrb_string_value_cstr(mrb, &arg1);
    char* s2    = mrb_string_value_cstr(mrb, &arg2);
    char* s_ret = malloc(strlen(s1)+strlen(s2)+1);
    s_ret[0] = '\0';
    strcat(s_ret, s1);
    strcat(s_ret, s2);
    ret = mrb_str_new_cstr(mrb, s_ret);
    free(s_ret);
    return ret;
  }

  return mrb_nil_value();
}

int main()
{
  mrb_state *mrb;
  int n;
  FILE* f;
  struct RClass* cObject;

  mrb = mrb_open();
  cObject = mrb_class_obj_get(mrb, "Object");
  mrb_define_method(mrb, cObject, "plus", plus, ARGS_ANY());
  //mrb_define_singleton_method(mrb, mrb_top_self(mrb), "plus", plus, ARGS_REQ(2));
  f = fopen("step4.rb", "r");
  if(f==NULL){ _error("file not found."); }
  n = mrb_compile_file(mrb, f);
  fclose(f);
  mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_nil_value());
  mrb_close(mrb);
}

/*
% gcc -o step4 step4.c -I ../include -I ../src ../lib/ritevm.a -lm
% ./step4
welcome to step4.
plus(1,2) => 3
plus('1','2') => "12"
hello world from mruby
*/


begin
  puts "welcome to step4."

  a = plus(1,2)
  puts "plus(1,2) => #{a.inspect}"

  b = plus('1','2')
  puts "plus('1','2') => #{b.inspect}"

  c = plus(plus('hello',' world'), plus(' from',' mruby'))
  puts c
rescue => e
  p e
end

スパム対策のためのダミーです。もし見えても何も入力しないでください
ゲスト

コメントを書くには、なぞなぞ認証に回答する必要があります。

トラックバック - http://d.hatena.ne.jp/urekat/20120428/1335602756