Skip to content

Instantly share code, notes, and snippets.

@aphistic
Created November 9, 2012 02:34
Show Gist options
  • Save aphistic/4043359 to your computer and use it in GitHub Desktop.
Save aphistic/4043359 to your computer and use it in GitHub Desktop.
Mruby via parser and mrb_run
#include "mruby.h"
#include "mruby/proc.h"
#include "mruby/data.h"
#include "mruby/compile.h"
int main(int argc, char** argv)
{
mrb_state *state = mrb_open();
mrbc_context *cxt = mrbc_context_new(state);
char* ruby_code = "def stuff ; print('Testing mrb') ; end";
struct mrb_parser_state *parser = mrb_parser_new(state);
parser->s = ruby_code;
parser->send = ruby_code + strlen(ruby_code);
parser->lineno = 1;
mrb_parser_parse(parser, cxt);
int n = mrb_generate_code(state, parser);
mrb_value result = mrb_run(state,
mrb_proc_new(state, state->irep[n]),
mrb_top_self(state));
mrb_p(state, result);
mrb_parser_free(parser);
mrbc_context_free(state, cxt);
mrb_close(state);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment