Commit ce49d90d authored by Yukihiro Matsumoto's avatar Yukihiro Matsumoto

modify compiler API; replace mrb_compie_xxx with mrb_load_xxx() that combines...

modify compiler API; replace mrb_compie_xxx with mrb_load_xxx() that combines compilatoin and execution
parent 97b1c576
...@@ -94,9 +94,10 @@ struct mrb_parser_state* mrb_parse_string(mrb_state*,const char*); ...@@ -94,9 +94,10 @@ struct mrb_parser_state* mrb_parse_string(mrb_state*,const char*);
struct mrb_parser_state* mrb_parse_nstring(mrb_state*,const char*,int); struct mrb_parser_state* mrb_parse_nstring(mrb_state*,const char*,int);
int mrb_generate_code(mrb_state*, mrb_ast_node*); int mrb_generate_code(mrb_state*, mrb_ast_node*);
int mrb_compile_file(mrb_state*,FILE*); /* program load functions */
int mrb_compile_string(mrb_state*,const char*); mrb_value mrb_load_file(mrb_state*,FILE*);
int mrb_compile_nstring(mrb_state*,const char*,int); mrb_value mrb_load_string(mrb_state *mrb, const char *path);
mrb_value mrb_load_nstring(mrb_state *mrb, const char *path, int len);
#if defined(__cplusplus) #if defined(__cplusplus)
} /* extern "C" { */ } /* extern "C" { */
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "mruby.h" #include "mruby.h"
#include "mruby/compile.h" #include "mruby/compile.h"
#include "mruby/proc.h"
#include "node.h" #include "node.h"
#include "st.h" #include "st.h"
...@@ -4793,46 +4794,49 @@ mrb_parse_string(mrb_state *mrb, const char *s) ...@@ -4793,46 +4794,49 @@ mrb_parse_string(mrb_state *mrb, const char *s)
return mrb_parse_nstring(mrb, s, strlen(s)); return mrb_parse_nstring(mrb, s, strlen(s));
} }
void parser_dump(mrb_state *mrb, node *tree, int offset); static mrb_value
load_exec(mrb_state *mrb, parser_state *p)
int
mrb_compile_file(mrb_state * mrb, FILE *f)
{ {
parser_state *p;
int n; int n;
p = mrb_parse_file(mrb, f); if (!p || !p->tree || p->nerr) {
if (!p) return -1; char buf[256];
if (!p->tree) return -1;
if (p->nerr) return -1; n = snprintf(buf, sizeof(buf), "line %d: %s\n",
p->error_buffer[0].lineno, p->error_buffer[0].message);
mrb_pool_close(p->pool);
mrb->exc = (struct RObject*)mrb_object(mrb_exc_new(mrb, E_SYNTAX_ERROR, buf, n));
return mrb_nil_value();
}
n = mrb_generate_code(mrb, p->tree); n = mrb_generate_code(mrb, p->tree);
mrb_pool_close(p->pool); mrb_pool_close(p->pool);
if (n < 0) {
return n; mrb->exc = (struct RObject*)mrb_object(mrb_exc_new(mrb, E_SCRIPT_ERROR, "codegen error", 13));
return mrb_nil_value();
}
return mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb));
} }
int mrb_value
mrb_compile_nstring(mrb_state *mrb, const char *s, int len) mrb_load_file(mrb_state *mrb, FILE *f)
{ {
parser_state *p; return load_exec(mrb, mrb_parse_file(mrb, f));
int n; }
p = mrb_parse_nstring(mrb, s, len);
if (!p) return -1;
if (!p->tree) return -1;
if (p->nerr) return -1;
n = mrb_generate_code(mrb, p->tree);
mrb_pool_close(p->pool);
return n; mrb_value
mrb_load_nstring(mrb_state *mrb, const char *s, int len)
{
return load_exec(mrb, mrb_parse_nstring(mrb, s, len));
} }
int mrb_value
mrb_compile_string(mrb_state *mrb, const char *s) mrb_load_string(mrb_state *mrb, const char *s)
{ {
return mrb_compile_nstring(mrb, s, strlen(s)); return load_exec(mrb, mrb_parse_nstring(mrb, s, strlen(s)));
} }
void parser_dump(mrb_state *mrb, node *tree, int offset);
static void static void
dump_prefix(int offset) dump_prefix(int offset)
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment