From 2fff59dc8674ad6689acc5080463eede4c5bb2ab Mon Sep 17 00:00:00 2001 From: Jon Date: Sun, 3 Jun 2012 10:04:51 -0400 Subject: Check mrb_open return value for NULL --- tools/mirb/mirb.c | 5 +++++ tools/mrbc/mrbc.c | 5 +++++ tools/mruby/mruby.c | 5 +++++ 3 files changed, 15 insertions(+) (limited to 'tools') diff --git a/tools/mirb/mirb.c b/tools/mirb/mirb.c index 59e5046cb..35558acfb 100644 --- a/tools/mirb/mirb.c +++ b/tools/mirb/mirb.c @@ -142,6 +142,11 @@ main(void) /* new interpreter instance */ mrb_interpreter = mrb_open(); + if (mrb_interpreter == NULL) { + fprintf(stderr, "Invalid mrb_interpreter, exiting mirb"); + return EXIT_FAILURE; + } + /* new parser instance */ parser = mrb_parser_new(mrb_interpreter); memset(ruby_code, 0, sizeof(*ruby_code)); diff --git a/tools/mrbc/mrbc.c b/tools/mrbc/mrbc.c index 3553fe646..99fea76d8 100644 --- a/tools/mrbc/mrbc.c +++ b/tools/mrbc/mrbc.c @@ -158,6 +158,11 @@ main(int argc, char **argv) struct _args args; struct mrb_parser_state *p; + if (mrb == NULL) { + fprintf(stderr, "Invalid mrb_state, exiting mrbc"); + return EXIT_FAILURE; + } + n = parse_args(mrb, argc, argv, &args); if (n < 0 || args.rfp == NULL) { diff --git a/tools/mruby/mruby.c b/tools/mruby/mruby.c index 8b227df5d..0e38879db 100644 --- a/tools/mruby/mruby.c +++ b/tools/mruby/mruby.c @@ -142,6 +142,11 @@ main(int argc, char **argv) struct _args args; struct mrb_parser_state *p; + if (mrb == NULL) { + fprintf(stderr, "Invalid mrb_state, exiting mruby"); + return EXIT_FAILURE; + } + n = parse_args(mrb, argc, argv, &args); if (n < 0 || (args.cmdline == NULL && args.rfp == NULL)) { cleanup(mrb, &args); -- cgit v1.2.3 From 03d32796b44420fa07ac375a2e4768a5c5195235 Mon Sep 17 00:00:00 2001 From: "NARUSE, Yui" Date: Thu, 14 Jun 2012 07:52:58 +0900 Subject: Support mruby -e'p 1'. mruby supports already -e 'p 1' (-e, space, command), but hadn't supported no space version. --- tools/mruby/mruby.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'tools') diff --git a/tools/mruby/mruby.c b/tools/mruby/mruby.c index 0e38879db..5cf3d8a37 100644 --- a/tools/mruby/mruby.c +++ b/tools/mruby/mruby.c @@ -51,12 +51,14 @@ parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args) memset(args, 0, sizeof(*args)); for (argc--,argv++; argc > 0; argc--,argv++) { + char *item; if (argv[0][0] != '-') break; if (strlen(*argv) <= 1) return -1; - switch ((*argv)[1]) { + item = argv[0] + 1; + switch (*item++) { case 'b': args->mrbfile = 1; break; @@ -64,19 +66,24 @@ parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args) args->check_syntax = 1; break; case 'e': - if (argc > 1) { + if (item[0]) { + goto append_cmdline; + } + else if (argc > 1) { argc--; argv++; + item = argv[0]; +append_cmdline: if (!args->cmdline) { char *buf; - buf = mrb_malloc(mrb, strlen(argv[0])+1); - strcpy(buf, argv[0]); + buf = mrb_malloc(mrb, strlen(item)+1); + strcpy(buf, item); args->cmdline = buf; } else { - args->cmdline = mrb_realloc(mrb, args->cmdline, strlen(args->cmdline)+strlen(argv[0])+2); + args->cmdline = mrb_realloc(mrb, args->cmdline, strlen(args->cmdline)+strlen(item)+2); strcat(args->cmdline, "\n"); - strcat(args->cmdline, argv[0]); + strcat(args->cmdline, item); } } else { -- cgit v1.2.3 From 83e5999d7efcad648e9ecbd64c51b305b6261999 Mon Sep 17 00:00:00 2001 From: Daniel Bovensiepen Date: Mon, 18 Jun 2012 11:22:05 +0800 Subject: Make CFLAG choices in Makefiles more flexible --- Makefile | 17 ++++++++++++----- mrblib/Makefile | 17 ++++++++++++----- src/Makefile | 17 ++++++++++++----- test/Makefile | 17 ++++++++++++----- tools/mirb/Makefile | 13 ++++++++++--- tools/mrbc/Makefile | 17 ++++++++++++----- tools/mruby/Makefile | 17 ++++++++++++----- 7 files changed, 82 insertions(+), 33 deletions(-) (limited to 'tools') diff --git a/Makefile b/Makefile index e7408d793..a960fc14a 100644 --- a/Makefile +++ b/Makefile @@ -7,12 +7,19 @@ export LL = gcc export AR = ar export YACC = bison -DEBUG_MODE = 1 -ifeq ($(DEBUG_MODE),1) -CFLAGS = -g -O3 -else -CFLAGS = -O3 +ifeq ($(strip $(COMPILE_MODE)),) + # default compile option + COMPILE_MODE = debug +endif + +ifeq ($(COMPILE_MODE),debug) + CFLAGS = -g -O3 +else ifeq ($(COMPILE_MODE),release) + CFLAGS = -O3 +else ifeq ($(COMPILE_MODE),small) + CFLAGS = -Os endif + ALL_CFLAGS = -Wall -Werror-implicit-function-declaration $(CFLAGS) ifeq ($(OS),Windows_NT) MAKE_FLAGS = --no-print-directory CC=$(CC) LL=$(LL) ALL_CFLAGS='$(ALL_CFLAGS)' diff --git a/mrblib/Makefile b/mrblib/Makefile index c7226ddcd..01a5a6198 100644 --- a/mrblib/Makefile +++ b/mrblib/Makefile @@ -18,12 +18,19 @@ LIBR := ../lib/libmruby.a # libraries, includes INCLUDES = -I../src -I../include -DEBUG_MODE = 1 -ifeq ($(DEBUG_MODE),1) -CFLAGS = -g -else -CFLAGS = -O3 +ifeq ($(strip $(COMPILE_MODE)),) + # default compile option + COMPILE_MODE = debug +endif + +ifeq ($(COMPILE_MODE),debug) + CFLAGS = -g -O3 +else ifeq ($(COMPILE_MODE),release) + CFLAGS = -O3 +else ifeq ($(COMPILE_MODE),small) + CFLAGS = -Os endif + ALL_CFLAGS = -Wall -Werror-implicit-function-declaration $(CFLAGS) ifeq ($(OS),Windows_NT) MAKE_FLAGS = CC=$(CC) LL=$(LL) ALL_CFLAGS="$(ALL_CFLAGS)" diff --git a/src/Makefile b/src/Makefile index 61012ea68..13f80b694 100644 --- a/src/Makefile +++ b/src/Makefile @@ -19,12 +19,19 @@ OBJS := $(OBJ1) $(OBJ2) $(OBJ3) # libraries, includes INCLUDES = -I$(BASEDIR) -I$(BASEDIR)/../include -DEBUG_MODE = 1 -ifeq ($(DEBUG_MODE),1) -CFLAGS = -g -O3 -else -CFLAGS = -O3 +ifeq ($(strip $(COMPILE_MODE)),) + # default compile option + COMPILE_MODE = debug endif + +ifeq ($(COMPILE_MODE),debug) + CFLAGS = -g -O3 +else ifeq ($(COMPILE_MODE),release) + CFLAGS = -O3 +else ifeq ($(COMPILE_MODE),small) + CFLAGS = -Os +endif + ALL_CFLAGS = -Wall -Werror-implicit-function-declaration $(CFLAGS) diff --git a/test/Makefile b/test/Makefile index 170c1dac8..921442b28 100644 --- a/test/Makefile +++ b/test/Makefile @@ -20,12 +20,19 @@ OBJS := driver.o $(MLIB) LIBS = -lm INCLUDES = -I$(BASEDIR)/../src -I$(BASEDIR)/../include -DEBUG_MODE = 1 -ifeq ($(DEBUG_MODE),1) -CFLAGS = -g -else -CFLAGS = -O3 +ifeq ($(strip $(COMPILE_MODE)),) + # default compile option + COMPILE_MODE = debug +endif + +ifeq ($(COMPILE_MODE),debug) + CFLAGS = -g -O3 +else ifeq ($(COMPILE_MODE),release) + CFLAGS = -O3 +else ifeq ($(COMPILE_MODE),small) + CFLAGS = -Os endif + ALL_CFLAGS = -Wall -Werror-implicit-function-declaration $(CFLAGS) ifeq ($(OS),Windows_NT) MAKE_FLAGS = CC=$(CC) LL=$(LL) ALL_CFLAGS="$(ALL_CFLAGS)" diff --git a/tools/mirb/Makefile b/tools/mirb/Makefile index ba307227c..52941f242 100644 --- a/tools/mirb/Makefile +++ b/tools/mirb/Makefile @@ -21,12 +21,19 @@ EXTS := $(EXT1) LIBS = -lm INCLUDES = -I$(BASEDIR) -I$(BASEDIR)/../include -DEBUG_MODE = 1 -ifeq ($(DEBUG_MODE),1) +ifeq ($(strip $(COMPILE_MODE)),) + # default compile option + COMPILE_MODE = debug +endif + +ifeq ($(COMPILE_MODE),debug) CFLAGS = -g -O3 -else +else ifeq ($(COMPILE_MODE),release) CFLAGS = -O3 +else ifeq ($(COMPILE_MODE),small) + CFLAGS = -Os endif + ALL_CFLAGS = -Wall -Werror-implicit-function-declaration $(CFLAGS) ifeq ($(OS),Windows_NT) MAKE_FLAGS = CC=$(CC) LL=$(LL) ALL_CFLAGS="$(ALL_CFLAGS)" diff --git a/tools/mrbc/Makefile b/tools/mrbc/Makefile index 99f5830e6..eea0c02cb 100644 --- a/tools/mrbc/Makefile +++ b/tools/mrbc/Makefile @@ -23,12 +23,19 @@ LIBS = -lm INCLUDES = -I$(BASEDIR) -I$(BASEDIR)/../include # compiler, linker (gcc) -DEBUG_MODE = 1 -ifeq ($(DEBUG_MODE),1) -CFLAGS = -g -O3 -else -CFLAGS = -O3 +ifeq ($(strip $(COMPILE_MODE)),) + # default compile option + COMPILE_MODE = debug +endif + +ifeq ($(COMPILE_MODE),debug) + CFLAGS = -g -O3 +else ifeq ($(COMPILE_MODE),release) + CFLAGS = -O3 +else ifeq ($(COMPILE_MODE),small) + CFLAGS = -Os endif + ALL_CFLAGS = -Wall -Werror-implicit-function-declaration $(CFLAGS) ifeq ($(OS),Windows_NT) MAKE_FLAGS = CC=$(CC) LL=$(LL) ALL_CFLAGS="$(ALL_CFLAGS)" diff --git a/tools/mruby/Makefile b/tools/mruby/Makefile index 0442bd422..9955b4302 100644 --- a/tools/mruby/Makefile +++ b/tools/mruby/Makefile @@ -26,12 +26,19 @@ LIBS = -lm INCLUDES = -I$(BASEDIR) -I$(BASEDIR)/../include # compiler, linker (gcc) -DEBUG_MODE = 1 -ifeq ($(DEBUG_MODE),1) -CFLAGS = -g -O3 -else -CFLAGS = -O3 +ifeq ($(strip $(COMPILE_MODE)),) + # default compile option + COMPILE_MODE = debug +endif + +ifeq ($(COMPILE_MODE),debug) + CFLAGS = -g -O3 +else ifeq ($(COMPILE_MODE),release) + CFLAGS = -O3 +else ifeq ($(COMPILE_MODE),small) + CFLAGS = -Os endif + ALL_CFLAGS = -Wall -Werror-implicit-function-declaration $(CFLAGS) ifeq ($(OS),Windows_NT) MAKE_FLAGS = CC=$(CC) LL=$(LL) ALL_CFLAGS="$(ALL_CFLAGS)" -- cgit v1.2.3 From 65096c4c1bd1bfb6f547808fc01ab6ea223d9dc6 Mon Sep 17 00:00:00 2001 From: Yukihiro Matsumoto Date: Tue, 3 Jul 2012 20:59:07 +0900 Subject: add context to parser, that would hold local variable info, filename, and line number. mrbc_context argument has been added to mrb_parse_xxx() functions. Normally, you just to need to add NULL (or 0) to the last argument of the above functions. --- include/mruby/compile.h | 37 +++++++++++++++++------- src/parse.y | 77 +++++++++++++++++++++++++++++++++++++------------ test/driver.c | 12 ++------ tools/mrbc/mrbc.c | 2 +- tools/mruby/mruby.c | 15 +++++----- 5 files changed, 95 insertions(+), 48 deletions(-) (limited to 'tools') diff --git a/include/mruby/compile.h b/include/mruby/compile.h index 2ea141da8..401f52854 100644 --- a/include/mruby/compile.h +++ b/include/mruby/compile.h @@ -14,13 +14,26 @@ extern "C" { #include "mruby.h" #include #include +#include + +/* load context */ +typedef struct mrbc_context { + mrb_sym *syms; + int slen; + char *filename; + int lineno; +} mrbc_context; + +mrbc_context* mrbc_context_new(mrb_state *mrb); +void mrbc_context_free(mrb_state *mrb, mrbc_context *cxt); +const char *mrbc_filename(mrb_state *mrb, mrbc_context *c, const char *s); +/* AST node structure */ typedef struct mrb_ast_node { struct mrb_ast_node *car, *cdr; } mrb_ast_node; -#include - +/* lexer states */ enum mrb_lex_state_enum { EXPR_BEG, /* ignore newline, +/- is a sign. */ EXPR_END, /* newline significant, +/- is an operator. */ @@ -36,21 +49,23 @@ enum mrb_lex_state_enum { EXPR_MAX_STATE }; +/* saved error message */ struct mrb_parser_message { int lineno; int column; char* message; }; +/* parser structure */ struct mrb_parser_state { mrb_state *mrb; struct mrb_pool *pool; mrb_ast_node *cells; const char *s, *send; FILE *f; + char *filename; int lineno; int column; - const char *filename; enum mrb_lex_state_enum lstate; int sterm; @@ -59,6 +74,8 @@ struct mrb_parser_state { unsigned int cmdarg_stack; int paren_nest; int lpar_beg; + int in_def, in_single, cmd_start; + mrb_ast_node *locals; mrb_ast_node *pb; char buf[1024]; @@ -66,9 +83,6 @@ struct mrb_parser_state { mrb_ast_node *heredoc; - int in_def, in_single, cmd_start; - mrb_ast_node *locals; - void *ylval; int nerr; @@ -82,22 +96,23 @@ struct mrb_parser_state { jmp_buf jmp; }; -/* parser structure */ struct mrb_parser_state* mrb_parser_new(mrb_state*); const char *mrb_parser_filename(struct mrb_parser_state*, const char*); -int mrb_parser_lineno(struct mrb_parser_state*, int); void mrb_parser_parse(struct mrb_parser_state*); /* utility functions */ -struct mrb_parser_state* mrb_parse_file(mrb_state*,FILE*); -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_file(mrb_state*,FILE*,mrbc_context*); +struct mrb_parser_state* mrb_parse_string(mrb_state*,const char*,mrbc_context*); +struct mrb_parser_state* mrb_parse_nstring(mrb_state*,const char*,int,mrbc_context*); int mrb_generate_code(mrb_state*, mrb_ast_node*); /* program load functions */ mrb_value mrb_load_file(mrb_state*,FILE*); mrb_value mrb_load_string(mrb_state *mrb, const char *path); mrb_value mrb_load_nstring(mrb_state *mrb, const char *path, int len); +mrb_value mrb_load_file_cxt(mrb_state*,FILE*, mrbc_context *cxt); +mrb_value mrb_load_string_cxt(mrb_state *mrb, const char *path, mrbc_context *cxt); +mrb_value mrb_load_nstring_cxt(mrb_state *mrb, const char *path, int len, mrbc_context *cxt); #if defined(__cplusplus) } /* extern "C" { */ diff --git a/src/parse.y b/src/parse.y index 7f40534b9..78d7d7a7e 100644 --- a/src/parse.y +++ b/src/parse.y @@ -4730,7 +4730,6 @@ mrb_parser_new(mrb_state *mrb) p->in_def = p->in_single = FALSE; p->capture_errors = 0; - p->lineno = 1; p->column = 0; #if defined(PARSER_TEST) || defined(PARSER_DEBUG) @@ -4740,33 +4739,56 @@ mrb_parser_new(mrb_state *mrb) return p; } +mrbc_context* +mrbc_context_new(mrb_state *mrb) +{ + mrbc_context *c; + + c = mrb_malloc(mrb, sizeof(mrbc_context)); + memset(c, 0, sizeof(mrbc_context)); + return c; +} + +void +mrbc_context_free(mrb_state *mrb, mrbc_context *cxt) +{ + mrb_free(mrb, cxt->syms); + mrb_free(mrb, cxt->filename); + mrb_free(mrb, cxt); +} + const char* -mrb_parser_filename(parser_state *p, const char *s) +mrbc_filename(mrb_state *mrb, mrbc_context *c, const char *s) { if (s) { - p->filename = strdup(s); + int len = strlen(s); + char *p = mrb_malloc(mrb, len); + + memcpy(p, s, len); + if (c->filename) mrb_free(mrb, c->filename); + c->filename = p; + c->lineno = 1; } - return p->filename; + return c->filename; } -int -mrb_parser_lineno(struct mrb_parser_state *p, int n) +static void +parser_init_cxt(parser_state *p, mrbc_context *cxt) { - if (n <= 0) { - return p->lineno; + if (cxt) { + if (cxt->lineno) p->lineno = cxt->lineno; + if (cxt->filename) p->filename = cxt->filename; } - p->column = 0; - p->lineno = n; - return n; } parser_state* -mrb_parse_file(mrb_state *mrb, FILE *f) +mrb_parse_file(mrb_state *mrb, FILE *f, mrbc_context *c) { parser_state *p; p = mrb_parser_new(mrb); if (!p) return 0; + parser_init_cxt(p, c); p->s = p->send = NULL; p->f = f; @@ -4775,12 +4797,13 @@ mrb_parse_file(mrb_state *mrb, FILE *f) } parser_state* -mrb_parse_nstring(mrb_state *mrb, const char *s, int len) +mrb_parse_nstring(mrb_state *mrb, const char *s, int len, mrbc_context *c) { parser_state *p; p = mrb_parser_new(mrb); if (!p) return 0; + parser_init_cxt(p, c); p->s = s; p->send = s + len; @@ -4789,9 +4812,9 @@ mrb_parse_nstring(mrb_state *mrb, const char *s, int len) } parser_state* -mrb_parse_string(mrb_state *mrb, const char *s) +mrb_parse_string(mrb_state *mrb, const char *s, mrbc_context *c) { - return mrb_parse_nstring(mrb, s, strlen(s)); + return mrb_parse_nstring(mrb, s, strlen(s), c); } static mrb_value @@ -4817,22 +4840,40 @@ load_exec(mrb_state *mrb, parser_state *p) return mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb)); } +mrb_value +mrb_load_file_cxt(mrb_state *mrb, FILE *f, mrbc_context *c) +{ + return load_exec(mrb, mrb_parse_file(mrb, f, c)); +} + mrb_value mrb_load_file(mrb_state *mrb, FILE *f) { - return load_exec(mrb, mrb_parse_file(mrb, f)); + return mrb_load_file_cxt(mrb, f, NULL); +} + +mrb_value +mrb_load_nstring_cxt(mrb_state *mrb, const char *s, int len, mrbc_context *c) +{ + return load_exec(mrb, mrb_parse_nstring(mrb, s, len, c)); } mrb_value mrb_load_nstring(mrb_state *mrb, const char *s, int len) { - return load_exec(mrb, mrb_parse_nstring(mrb, s, len)); + return mrb_load_nstring_cxt(mrb, s, len, NULL); +} + +mrb_value +mrb_load_string_cxt(mrb_state *mrb, const char *s, mrbc_context *c) +{ + return load_exec(mrb, mrb_parse_nstring(mrb, s, strlen(s), c)); } mrb_value mrb_load_string(mrb_state *mrb, const char *s) { - return load_exec(mrb, mrb_parse_nstring(mrb, s, strlen(s))); + return mrb_load_string_cxt(mrb, s, NULL); } void parser_dump(mrb_state *mrb, node *tree, int offset); diff --git a/test/driver.c b/test/driver.c index 6b42d025b..6b1697f14 100644 --- a/test/driver.c +++ b/test/driver.c @@ -42,16 +42,8 @@ main(void) } mrb_init_mrbtest(mrb); - parser = mrb_parse_nstring(mrb, prog, strlen(prog)); - - /* generate bytecode */ - byte_code = mrb_generate_code(mrb, parser->tree); - - /* evaluate the bytecode */ - return_value = mrb_run(mrb, - /* pass a proc for evaulation */ - mrb_proc_new(mrb, mrb->irep[byte_code]), - mrb_top_self(mrb)); + /* evaluate the test */ + return_value = mrb_load_string(mrb, prog); /* did an exception occur? */ if (mrb->exc) { mrb_p(mrb, return_value); diff --git a/tools/mrbc/mrbc.c b/tools/mrbc/mrbc.c index 99fea76d8..9b69244e5 100644 --- a/tools/mrbc/mrbc.c +++ b/tools/mrbc/mrbc.c @@ -172,7 +172,7 @@ main(int argc, char **argv) return n; } - p = mrb_parse_file(mrb, args.rfp); + p = mrb_parse_file(mrb, args.rfp, NULL); if (!p || !p->tree || p->nerr) { cleanup(&args); mrb_close(mrb); diff --git a/tools/mruby/mruby.c b/tools/mruby/mruby.c index 5cf3d8a37..b4b37516b 100644 --- a/tools/mruby/mruby.c +++ b/tools/mruby/mruby.c @@ -165,17 +165,16 @@ main(int argc, char **argv) n = mrb_load_irep(mrb, args.rfp); } else { + mrbc_context *c = mrbc_context_new(mrb); if (args.cmdline) { - p = mrb_parse_string(mrb, (char*)args.cmdline); - } + mrbc_filename(mrb, c, "-e"); + p = mrb_parse_string(mrb, (char*)args.cmdline, c); + } else { - p = mrb_parser_new(mrb); - if (p) { - mrb_parser_filename(p, argv[1]); - p->f = args.rfp; - mrb_parser_parse(p); - } + mrbc_filename(mrb, c, argv[1]); + p = mrb_parse_file(mrb, args.rfp, c); } + mrbc_context_free(mrb, c); if (!p || !p->tree || p->nerr) { cleanup(mrb, &args); return -1; -- cgit v1.2.3 From 39689441e78c59fb92d7563ed40f14287df9b575 Mon Sep 17 00:00:00 2001 From: Yukihiro Matsumoto Date: Tue, 3 Jul 2012 21:13:10 +0900 Subject: add context arg to mrb_parser_parse() --- include/mruby/compile.h | 2 +- src/parse.y | 27 +++++++++++++-------------- tools/mirb/mirb.c | 2 +- 3 files changed, 15 insertions(+), 16 deletions(-) (limited to 'tools') diff --git a/include/mruby/compile.h b/include/mruby/compile.h index 401f52854..c4e480577 100644 --- a/include/mruby/compile.h +++ b/include/mruby/compile.h @@ -98,7 +98,7 @@ struct mrb_parser_state { struct mrb_parser_state* mrb_parser_new(mrb_state*); const char *mrb_parser_filename(struct mrb_parser_state*, const char*); -void mrb_parser_parse(struct mrb_parser_state*); +void mrb_parser_parse(struct mrb_parser_state*,mrbc_context*); /* utility functions */ struct mrb_parser_state* mrb_parse_file(mrb_state*,FILE*,mrbc_context*); diff --git a/src/parse.y b/src/parse.y index 78d7d7a7e..90534cb32 100644 --- a/src/parse.y +++ b/src/parse.y @@ -4669,8 +4669,17 @@ yylex(void *lval, parser_state *p) return t; } +static void +parser_init_cxt(parser_state *p, mrbc_context *cxt) +{ + if (cxt) { + if (cxt->lineno) p->lineno = cxt->lineno; + if (cxt->filename) p->filename = cxt->filename; + } +} + void -mrb_parser_parse(parser_state *p) +mrb_parser_parse(parser_state *p, mrbc_context *c) { node *tree; @@ -4685,6 +4694,7 @@ mrb_parser_parse(parser_state *p) p->in_def = p->in_single = FALSE; p->nerr = p->nwarn = 0; p->sterm = 0; + parser_init_cxt(p, c); yyparse(p); tree = p->tree; @@ -4772,15 +4782,6 @@ mrbc_filename(mrb_state *mrb, mrbc_context *c, const char *s) return c->filename; } -static void -parser_init_cxt(parser_state *p, mrbc_context *cxt) -{ - if (cxt) { - if (cxt->lineno) p->lineno = cxt->lineno; - if (cxt->filename) p->filename = cxt->filename; - } -} - parser_state* mrb_parse_file(mrb_state *mrb, FILE *f, mrbc_context *c) { @@ -4788,11 +4789,10 @@ mrb_parse_file(mrb_state *mrb, FILE *f, mrbc_context *c) p = mrb_parser_new(mrb); if (!p) return 0; - parser_init_cxt(p, c); p->s = p->send = NULL; p->f = f; - mrb_parser_parse(p); + mrb_parser_parse(p, c); return p; } @@ -4803,11 +4803,10 @@ mrb_parse_nstring(mrb_state *mrb, const char *s, int len, mrbc_context *c) p = mrb_parser_new(mrb); if (!p) return 0; - parser_init_cxt(p, c); p->s = s; p->send = s + len; - mrb_parser_parse(p); + mrb_parser_parse(p, c); return p; } diff --git a/tools/mirb/mirb.c b/tools/mirb/mirb.c index 35558acfb..fb61dd272 100644 --- a/tools/mirb/mirb.c +++ b/tools/mirb/mirb.c @@ -196,7 +196,7 @@ main(void) parser->send = ruby_code + strlen(ruby_code); parser->capture_errors = 1; parser->lineno = 1; - mrb_parser_parse(parser); + mrb_parser_parse(parser, NULL); code_block_open = is_code_block_open(parser); if (code_block_open) { -- cgit v1.2.3 From a7b8a94e48b805cd1651807aae09faf2d265b61b Mon Sep 17 00:00:00 2001 From: Yukihiro Matsumoto Date: Tue, 3 Jul 2012 21:29:46 +0900 Subject: refactor mirb code --- tools/mirb/mirb.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'tools') diff --git a/tools/mirb/mirb.c b/tools/mirb/mirb.c index fb61dd272..a9c0f26d2 100644 --- a/tools/mirb/mirb.c +++ b/tools/mirb/mirb.c @@ -133,22 +133,22 @@ main(void) char last_char, ruby_code[1024], last_code_line[1024]; int char_index; struct mrb_parser_state *parser; - mrb_state *mrb_interpreter; - mrb_value mrb_return_value; - int byte_code; + mrb_state *mrb; + mrb_value result; + int n; int code_block_open = FALSE; print_hint(); /* new interpreter instance */ - mrb_interpreter = mrb_open(); - if (mrb_interpreter == NULL) { - fprintf(stderr, "Invalid mrb_interpreter, exiting mirb"); + mrb = mrb_open(); + if (mrb == NULL) { + fprintf(stderr, "Invalid mrb interpreter, exiting mirb"); return EXIT_FAILURE; } /* new parser instance */ - parser = mrb_parser_new(mrb_interpreter); + parser = mrb_parser_new(mrb); memset(ruby_code, 0, sizeof(*ruby_code)); memset(last_code_line, 0, sizeof(*last_code_line)); @@ -209,22 +209,22 @@ main(void) } else { /* generate bytecode */ - byte_code = mrb_generate_code(mrb_interpreter, parser->tree); + n = mrb_generate_code(mrb, parser->tree); /* evaluate the bytecode */ - mrb_return_value = mrb_run(mrb_interpreter, + result = mrb_run(mrb, /* pass a proc for evaulation */ - mrb_proc_new(mrb_interpreter, mrb_interpreter->irep[byte_code]), - mrb_top_self(mrb_interpreter)); + mrb_proc_new(mrb, mrb->irep[n]), + mrb_top_self(mrb)); /* did an exception occur? */ - if (mrb_interpreter->exc) { - mrb_p(mrb_interpreter, mrb_obj_value(mrb_interpreter->exc)); - mrb_interpreter->exc = 0; + if (mrb->exc) { + mrb_p(mrb, mrb_obj_value(mrb->exc)); + mrb->exc = 0; } else { /* no */ printf(" => "); - mrb_p(mrb_interpreter, mrb_return_value); + mrb_p(mrb, result); } } @@ -233,7 +233,7 @@ main(void) } } } - mrb_close(mrb_interpreter); + mrb_close(mrb); return 0; } -- cgit v1.2.3 From a1af20baa9f9a96c2a8cd6a9a1505fe53799e9c9 Mon Sep 17 00:00:00 2001 From: Yukihiro Matsumoto Date: Tue, 3 Jul 2012 21:49:06 +0900 Subject: use mrbc_context; free parser for each iteration; close #312 --- tools/mirb/mirb.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'tools') diff --git a/tools/mirb/mirb.c b/tools/mirb/mirb.c index a9c0f26d2..d46bcff3c 100644 --- a/tools/mirb/mirb.c +++ b/tools/mirb/mirb.c @@ -132,6 +132,7 @@ main(void) { char last_char, ruby_code[1024], last_code_line[1024]; int char_index; + mrbc_context *cxt; struct mrb_parser_state *parser; mrb_state *mrb; mrb_value result; @@ -147,8 +148,8 @@ main(void) return EXIT_FAILURE; } - /* new parser instance */ - parser = mrb_parser_new(mrb); + cxt = mrbc_context_new(mrb); + cxt->capture_errors = 1; memset(ruby_code, 0, sizeof(*ruby_code)); memset(last_code_line, 0, sizeof(*last_code_line)); @@ -192,11 +193,11 @@ main(void) } /* parse code */ + parser = mrb_parser_new(mrb); parser->s = ruby_code; parser->send = ruby_code + strlen(ruby_code); - parser->capture_errors = 1; parser->lineno = 1; - mrb_parser_parse(parser, NULL); + mrb_parser_parse(parser, cxt); code_block_open = is_code_block_open(parser); if (code_block_open) { @@ -227,12 +228,13 @@ main(void) mrb_p(mrb, result); } } - memset(ruby_code, 0, sizeof(*ruby_code)); memset(ruby_code, 0, sizeof(*last_code_line)); } + mrb_pool_close(parser->pool); } } + mrbc_context_free(mrb, cxt); mrb_close(mrb); return 0; -- cgit v1.2.3 From fc27f71289b84f700efc780ae79eea66f212c0c4 Mon Sep 17 00:00:00 2001 From: Yukihiro Matsumoto Date: Tue, 3 Jul 2012 21:52:11 +0900 Subject: add new function mrb_parser_free() --- include/mruby/compile.h | 1 + src/parse.y | 5 +++++ tools/mirb/mirb.c | 2 +- 3 files changed, 7 insertions(+), 1 deletion(-) (limited to 'tools') diff --git a/include/mruby/compile.h b/include/mruby/compile.h index 8f65932ba..42820896e 100644 --- a/include/mruby/compile.h +++ b/include/mruby/compile.h @@ -98,6 +98,7 @@ struct mrb_parser_state { }; struct mrb_parser_state* mrb_parser_new(mrb_state*); +void mrb_parser_free(struct mrb_parser_state*); const char *mrb_parser_filename(struct mrb_parser_state*, const char*); void mrb_parser_parse(struct mrb_parser_state*,mrbc_context*); diff --git a/src/parse.y b/src/parse.y index 63d39ec5f..b0d399a98 100644 --- a/src/parse.y +++ b/src/parse.y @@ -4775,6 +4775,11 @@ mrb_parser_new(mrb_state *mrb) return p; } +void +mrb_parser_free(parser_state *p) { + mrb_pool_close(p->pool); +} + mrbc_context* mrbc_context_new(mrb_state *mrb) { diff --git a/tools/mirb/mirb.c b/tools/mirb/mirb.c index d46bcff3c..8b36cb5a6 100644 --- a/tools/mirb/mirb.c +++ b/tools/mirb/mirb.c @@ -231,7 +231,7 @@ main(void) memset(ruby_code, 0, sizeof(*ruby_code)); memset(ruby_code, 0, sizeof(*last_code_line)); } - mrb_pool_close(parser->pool); + mrb_parser_free(parser); } } mrbc_context_free(mrb, cxt); -- cgit v1.2.3 From 85a0cad905faf6377f308f0e596a031cd143f91d Mon Sep 17 00:00:00 2001 From: Yukihiro Matsumoto Date: Tue, 3 Jul 2012 21:54:08 +0900 Subject: use new function mrb_parser_free() --- src/parse.y | 6 +++--- tools/mrbc/mrbc.c | 2 +- tools/mruby/mruby.c | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) (limited to 'tools') diff --git a/src/parse.y b/src/parse.y index b0d399a98..2b69ac726 100644 --- a/src/parse.y +++ b/src/parse.y @@ -4853,7 +4853,7 @@ load_exec(mrb_state *mrb, parser_state *p) int n; if (!p) { - mrb_pool_close(p->pool); + mrb_parser_free(p); return mrb_nil_value(); } if (p->capture_errors && (!p->tree || p->nerr)) { @@ -4862,11 +4862,11 @@ load_exec(mrb_state *mrb, parser_state *p) n = snprintf(buf, sizeof(buf), "line %d: %s\n", p->error_buffer[0].lineno, p->error_buffer[0].message); mrb->exc = (struct RObject*)mrb_object(mrb_exc_new(mrb, E_SYNTAX_ERROR, buf, n)); - mrb_pool_close(p->pool); + mrb_parser_free(p); return mrb_nil_value(); } n = mrb_generate_code(mrb, p->tree); - mrb_pool_close(p->pool); + mrb_parser_free(p); if (n < 0) { mrb->exc = (struct RObject*)mrb_object(mrb_exc_new(mrb, E_SCRIPT_ERROR, "codegen error", 13)); return mrb_nil_value(); diff --git a/tools/mrbc/mrbc.c b/tools/mrbc/mrbc.c index 9b69244e5..55e2fcf32 100644 --- a/tools/mrbc/mrbc.c +++ b/tools/mrbc/mrbc.c @@ -183,7 +183,7 @@ main(int argc, char **argv) parser_dump(mrb, p->tree, 0); n = mrb_generate_code(mrb, p->tree); - mrb_pool_close(p->pool); + mrb_parser_free(p); if (args.verbose) codedump_all(mrb, n); diff --git a/tools/mruby/mruby.c b/tools/mruby/mruby.c index b4b37516b..3628e79c9 100644 --- a/tools/mruby/mruby.c +++ b/tools/mruby/mruby.c @@ -184,7 +184,7 @@ main(int argc, char **argv) parser_dump(mrb, p->tree, 0); n = mrb_generate_code(mrb, p->tree); - mrb_pool_close(p->pool); + mrb_parser_free(p); } if (n >= 0) { -- cgit v1.2.3 From bbec03bb7af3fb0277a267e4ec5d38b58aa5cb46 Mon Sep 17 00:00:00 2001 From: Yukihiro Matsumoto Date: Fri, 13 Jul 2012 14:35:18 +0900 Subject: add missing (empty) default for swtch; close #364 --- src/class.c | 3 +++ src/codegen.c | 4 ++++ src/parse.y | 7 ++++++- src/range.c | 4 ++++ src/symbol.c | 4 +++- tools/mrbc/mrbc.c | 2 ++ tools/mruby/mruby.c | 2 ++ 7 files changed, 24 insertions(+), 2 deletions(-) (limited to 'tools') diff --git a/src/class.c b/src/class.c index 9003c3bc5..783627a7d 100644 --- a/src/class.c +++ b/src/class.c @@ -620,6 +620,9 @@ mrb_get_args(mrb_state *mrb, const char *format, ...) } } break; + default: + mrb_raise(mrb, E_ARGUMENT_ERROR, "invalide argument specifier %c", c); + break; } } if (!c && argc > i) { diff --git a/src/codegen.c b/src/codegen.c index 00c6b566d..3ef52a0d4 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -207,6 +207,8 @@ genop_peep(codegen_scope *s, mrb_code i, int val) return; } break; + default: + break; } break; case OP_SETIV: @@ -240,6 +242,8 @@ genop_peep(codegen_scope *s, mrb_code i, int val) return; } break; + default: + break; } } genop(s, i); diff --git a/src/parse.y b/src/parse.y index 292e60cfa..13643f68e 100644 --- a/src/parse.y +++ b/src/parse.y @@ -3041,6 +3041,8 @@ backref_error(parser_state *p, node *n) case NODE_BACK_REF: yyerror_i(p, "can't set variable $%c", (int)(intptr_t)n->cdr); break; + default: + break; } } @@ -3722,7 +3724,7 @@ parser_yylex(parser_state *p) } if (isspace(c)) { if (!IS_ARG()) { - int c2 = 0; + int c2; switch (c) { case ' ': c2 = 's'; @@ -3742,6 +3744,9 @@ parser_yylex(parser_state *p) case '\f': c2 = 'f'; break; + default: + c2 = 0; + break; } if (c2) { char buf[256]; diff --git a/src/range.c b/src/range.c index b4d743e5f..703ad12aa 100644 --- a/src/range.c +++ b/src/range.c @@ -31,7 +31,11 @@ range_check(mrb_state *mrb, mrb_value a, mrb_value b) case MRB_TT_FIXNUM: case MRB_TT_FLOAT: return; + default: + break; } + default: + break; } mrb_p(mrb, a); diff --git a/src/symbol.c b/src/symbol.c index 91076e293..baab0fb3c 100644 --- a/src/symbol.c +++ b/src/symbol.c @@ -276,6 +276,7 @@ symname_p(const char *name) case '>': switch (*++m) { case '>': case '=': ++m; break; + default: break; } break; @@ -319,7 +320,8 @@ id: while (is_identchar(*m)) m += 1; if (localid) { switch (*m) { - case '!': case '?': case '=': ++m; + case '!': case '?': case '=': ++m; + default: break; } } break; diff --git a/tools/mrbc/mrbc.c b/tools/mrbc/mrbc.c index 55e2fcf32..8da37832a 100644 --- a/tools/mrbc/mrbc.c +++ b/tools/mrbc/mrbc.c @@ -114,6 +114,8 @@ parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args) } else return -3; return 0; + default: + break; } } else if (args->rfp == NULL) { diff --git a/tools/mruby/mruby.c b/tools/mruby/mruby.c index 3628e79c9..12cdc86e9 100644 --- a/tools/mruby/mruby.c +++ b/tools/mruby/mruby.c @@ -110,6 +110,8 @@ append_cmdline: } else return -3; return 0; + default: + break; } } -- cgit v1.2.3 From 8e1c842b7e17ecb58e25e48e0cac253bceb00f14 Mon Sep 17 00:00:00 2001 From: Yukihiro Matsumoto Date: Fri, 13 Jul 2012 15:36:43 +0900 Subject: simpify mruby/mrbc using context --- include/mruby/compile.h | 2 ++ src/parse.y | 20 ++++++++++++----- tools/mrbc/mrbc.c | 24 ++++++++------------ tools/mruby/mruby.c | 58 ++++++++++++++++++++----------------------------- 4 files changed, 49 insertions(+), 55 deletions(-) (limited to 'tools') diff --git a/include/mruby/compile.h b/include/mruby/compile.h index e3361feed..4dd369adb 100644 --- a/include/mruby/compile.h +++ b/include/mruby/compile.h @@ -22,6 +22,8 @@ typedef struct mrbc_context { char *filename; int lineno; int capture_errors:1; + int dump_result:1; + int no_exec:1; } mrbc_context; mrbc_context* mrbc_context_new(mrb_state *mrb); diff --git a/src/parse.y b/src/parse.y index 8d626b5d8..5a22b3883 100644 --- a/src/parse.y +++ b/src/parse.y @@ -4721,6 +4721,9 @@ parser_update_cxt(parser_state *p, mrbc_context *cxt) } } +void codedump_all(mrb_state*, int); +void parser_dump(mrb_state *mrb, node *tree, int offset); + void mrb_parser_parse(parser_state *p, mrbc_context *c) { @@ -4756,6 +4759,9 @@ mrb_parser_parse(parser_state *p, mrbc_context *c) append(tree, p->tree); } } + if (c && c->dump_result) { + parser_dump(p->mrb, p->tree, 0); + } } parser_state* @@ -4862,7 +4868,7 @@ mrb_parse_string(mrb_state *mrb, const char *s, mrbc_context *c) } static mrb_value -load_exec(mrb_state *mrb, parser_state *p) +load_exec(mrb_state *mrb, parser_state *p, mrbc_context *c) { int n; @@ -4885,13 +4891,17 @@ load_exec(mrb_state *mrb, parser_state *p) mrb->exc = (struct RObject*)mrb_object(mrb_exc_new(mrb, E_SCRIPT_ERROR, "codegen error", 13)); return mrb_nil_value(); } + if (c) { + if (c->dump_result) codedump_all(mrb, n); + if (c->no_exec) return mrb_fixnum_value(n); + } return mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb)); } mrb_value mrb_load_file_cxt(mrb_state *mrb, FILE *f, mrbc_context *c) { - return load_exec(mrb, mrb_parse_file(mrb, f, c)); + return load_exec(mrb, mrb_parse_file(mrb, f, c), c); } mrb_value @@ -4903,7 +4913,7 @@ mrb_load_file(mrb_state *mrb, FILE *f) mrb_value mrb_load_nstring_cxt(mrb_state *mrb, const char *s, int len, mrbc_context *c) { - return load_exec(mrb, mrb_parse_nstring(mrb, s, len, c)); + return load_exec(mrb, mrb_parse_nstring(mrb, s, len, c), c); } mrb_value @@ -4915,7 +4925,7 @@ mrb_load_nstring(mrb_state *mrb, const char *s, int len) mrb_value mrb_load_string_cxt(mrb_state *mrb, const char *s, mrbc_context *c) { - return load_exec(mrb, mrb_parse_nstring(mrb, s, strlen(s), c)); + return mrb_load_nstring_cxt(mrb, s, strlen(s), c); } mrb_value @@ -4924,8 +4934,6 @@ mrb_load_string(mrb_state *mrb, const char *s) return mrb_load_string_cxt(mrb, s, NULL); } -void parser_dump(mrb_state *mrb, node *tree, int offset); - static void dump_prefix(int offset) { diff --git a/tools/mrbc/mrbc.c b/tools/mrbc/mrbc.c index 8da37832a..a70353d63 100644 --- a/tools/mrbc/mrbc.c +++ b/tools/mrbc/mrbc.c @@ -158,7 +158,8 @@ main(int argc, char **argv) mrb_state *mrb = mrb_open(); int n = -1; struct _args args; - struct mrb_parser_state *p; + mrbc_context *c; + mrb_value result; if (mrb == NULL) { fprintf(stderr, "Invalid mrb_state, exiting mrbc"); @@ -166,7 +167,6 @@ main(int argc, char **argv) } n = parse_args(mrb, argc, argv, &args); - if (n < 0 || args.rfp == NULL) { cleanup(&args); usage(argv[0]); @@ -174,22 +174,17 @@ main(int argc, char **argv) return n; } - p = mrb_parse_file(mrb, args.rfp, NULL); - if (!p || !p->tree || p->nerr) { + c = mrbc_context_new(mrb); + if (args.verbose) + c->dump_result = 1; + c->no_exec = 1; + result = mrb_load_file_cxt(mrb, args.rfp, c); + if (mrb_nil_p(result)) { cleanup(&args); mrb_close(mrb); return -1; } - - if (args.verbose) - parser_dump(mrb, p->tree, 0); - - n = mrb_generate_code(mrb, p->tree); - mrb_parser_free(p); - - if (args.verbose) - codedump_all(mrb, n); - + n = mrb_fixnum(result); if (n < 0 || args.check_syntax) { cleanup(&args); mrb_close(mrb); @@ -215,4 +210,3 @@ void mrb_init_mrblib(mrb_state *mrb) { } - diff --git a/tools/mruby/mruby.c b/tools/mruby/mruby.c index 12cdc86e9..65eae57e1 100644 --- a/tools/mruby/mruby.c +++ b/tools/mruby/mruby.c @@ -9,8 +9,6 @@ void mrb_show_version(mrb_state *); void mrb_show_copyright(mrb_state *); -void parser_dump(mrb_state*, struct mrb_ast_node*, int); -void codedump_all(mrb_state*, int); struct _args { FILE *rfp; @@ -149,7 +147,7 @@ main(int argc, char **argv) int n = -1; int i; struct _args args; - struct mrb_parser_state *p; + mrb_value ARGV; if (mrb == NULL) { fprintf(stderr, "Invalid mrb_state, exiting mruby"); @@ -163,50 +161,42 @@ main(int argc, char **argv) return n; } + ARGV = mrb_ary_new(mrb); + for (i = 0; i < args.argc; i++) { + mrb_ary_push(mrb, ARGV, mrb_str_new(mrb, args.argv[i], strlen(args.argv[i]))); + } + mrb_define_global_const(mrb, "ARGV", ARGV); + if (args.mrbfile) { n = mrb_load_irep(mrb, args.rfp); + if (n >= 0) { + if (!args.check_syntax) { + mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb)); + if (mrb->exc) { + mrb_p(mrb, mrb_obj_value(mrb->exc)); + } + } + } } else { mrbc_context *c = mrbc_context_new(mrb); + + if (args.verbose) + c->dump_result = 1; + if (args.check_syntax) + c->no_exec = 1; + if (args.cmdline) { mrbc_filename(mrb, c, "-e"); - p = mrb_parse_string(mrb, (char*)args.cmdline, c); - } + mrb_load_string_cxt(mrb, (char*)args.cmdline, c); + } else { mrbc_filename(mrb, c, argv[1]); - p = mrb_parse_file(mrb, args.rfp, c); + mrb_load_file_cxt(mrb, args.rfp, c); } mrbc_context_free(mrb, c); - if (!p || !p->tree || p->nerr) { - cleanup(mrb, &args); return -1; - } - - if (args.verbose) - parser_dump(mrb, p->tree, 0); - - n = mrb_generate_code(mrb, p->tree); - mrb_parser_free(p); - } - - if (n >= 0) { - mrb_value ARGV = mrb_ary_new(mrb); - for (i = 0; i < args.argc; i++) { - mrb_ary_push(mrb, ARGV, mrb_str_new(mrb, args.argv[i], strlen(args.argv[i]))); - } - mrb_define_global_const(mrb, "ARGV", ARGV); - - if (args.verbose) - codedump_all(mrb, n); - - if (!args.check_syntax) { - mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb)); - if (mrb->exc) { - mrb_p(mrb, mrb_obj_value(mrb->exc)); - } - } } - cleanup(mrb, &args); return n > 0 ? 0 : 1; -- cgit v1.2.3 From e3806922245c15ff9417ca4513c9052ebfa40021 Mon Sep 17 00:00:00 2001 From: Yukihiro Matsumoto Date: Sat, 14 Jul 2012 07:38:43 +0900 Subject: mrb_load_xxx to return undef + mrb_undef_p --- include/mruby.h | 2 +- src/kernel.c | 2 +- src/parse.y | 6 +++--- src/sprintf.c | 8 ++++---- tools/mrbc/mrbc.c | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-) (limited to 'tools') diff --git a/include/mruby.h b/include/mruby.h index a9027d896..1124ba89c 100644 --- a/include/mruby.h +++ b/include/mruby.h @@ -81,7 +81,7 @@ typedef struct mrb_value { #define mrb_symbol(o) (o).value.sym #define mrb_object(o) ((struct RBasic *) (o).value.p) #define FIXNUM_P(o) ((o).tt == MRB_TT_FIXNUM) -#define UNDEF_P(o) ((o).tt == MRB_TT_UNDEF) +#define mrb_undef_p(o) ((o).tt == MRB_TT_UNDEF) #include "mruby/object.h" diff --git a/src/kernel.c b/src/kernel.c index 1040dbe05..240c0dcb3 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -1038,7 +1038,7 @@ mrb_obj_remove_instance_variable(mrb_state *mrb, mrb_value self) mrb_get_args(mrb, "n", &sym); val = mrb_iv_remove(mrb, self, sym); - if (UNDEF_P(val)) { + if (mrb_undef_p(val)) { mrb_name_error(mrb, sym, "instance variable %s not defined", mrb_sym2name(mrb, sym)); } return val; diff --git a/src/parse.y b/src/parse.y index 5a22b3883..40960e313 100644 --- a/src/parse.y +++ b/src/parse.y @@ -4874,7 +4874,7 @@ load_exec(mrb_state *mrb, parser_state *p, mrbc_context *c) if (!p) { mrb_parser_free(p); - return mrb_nil_value(); + return mrb_undef_value(); } if (p->capture_errors && (!p->tree || p->nerr)) { char buf[256]; @@ -4883,13 +4883,13 @@ load_exec(mrb_state *mrb, parser_state *p, mrbc_context *c) p->error_buffer[0].lineno, p->error_buffer[0].message); mrb->exc = (struct RObject*)mrb_object(mrb_exc_new(mrb, E_SYNTAX_ERROR, buf, n)); mrb_parser_free(p); - return mrb_nil_value(); + return mrb_undef_value(); } n = mrb_generate_code(mrb, p->tree); mrb_parser_free(p); if (n < 0) { mrb->exc = (struct RObject*)mrb_object(mrb_exc_new(mrb, E_SCRIPT_ERROR, "codegen error", 13)); - return mrb_nil_value(); + return mrb_undef_value(); } if (c) { if (c->dump_result) codedump_all(mrb, n); diff --git a/src/sprintf.c b/src/sprintf.c index d6104ad48..b597ff343 100644 --- a/src/sprintf.c +++ b/src/sprintf.c @@ -145,7 +145,7 @@ mrb_fix2binstr(mrb_state *mrb, mrb_value x, int base) blen += (l);\ } while (0) -#define GETARG() (!UNDEF_P(nextvalue) ? nextvalue : \ +#define GETARG() (!mrb_undef_p(nextvalue) ? nextvalue : \ posarg == -1 ? \ (mrb_raise(mrb, E_ARGUMENT_ERROR, "unnumbered(%d) mixed with numbered", nextarg), mrb_undef_value()) : \ posarg == -2 ? \ @@ -201,7 +201,7 @@ get_hash(mrb_state *mrb, mrb_value *hash, int argc, const mrb_value *argv) { mrb_value tmp; - if (!UNDEF_P(*hash)) return *hash; + if (!mrb_undef_p(*hash)) return *hash; if (argc != 2) { mrb_raise(mrb, E_ARGUMENT_ERROR, "one hash required"); } @@ -586,7 +586,7 @@ retry: n = 0; GETNUM(n, width); if (*p == '$') { - if (!UNDEF_P(nextvalue)) { + if (!mrb_undef_p(nextvalue)) { mrb_raise(mrb, E_ARGUMENT_ERROR, "value given twice - %d$", n); } nextvalue = GETPOSARG(n); @@ -614,7 +614,7 @@ retry: symname = mrb_str_new(mrb, start + 1, p - start - 1); id = mrb_intern_str(mrb, symname); nextvalue = GETNAMEARG(mrb_symbol_value(id), start, (int)(p - start + 1)); - if (UNDEF_P(nextvalue)) { + if (mrb_undef_p(nextvalue)) { mrb_raise(mrb, E_KEY_ERROR, "key%.*s not found", (int)(p - start + 1), start); } if (term == '}') goto format_s; diff --git a/tools/mrbc/mrbc.c b/tools/mrbc/mrbc.c index a70353d63..a121eaa0e 100644 --- a/tools/mrbc/mrbc.c +++ b/tools/mrbc/mrbc.c @@ -179,7 +179,7 @@ main(int argc, char **argv) c->dump_result = 1; c->no_exec = 1; result = mrb_load_file_cxt(mrb, args.rfp, c); - if (mrb_nil_p(result)) { + if (mrb_undef_p(result)) { cleanup(&args); mrb_close(mrb); return -1; -- cgit v1.2.3 From 59389561715aed3b37d571e34c0c80f17f192788 Mon Sep 17 00:00:00 2001 From: Yukihiro Matsumoto Date: Sat, 14 Jul 2012 07:39:50 +0900 Subject: mrbc: better cleanup before exit --- tools/mrbc/mrbc.c | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) (limited to 'tools') diff --git a/tools/mrbc/mrbc.c b/tools/mrbc/mrbc.c index a121eaa0e..428a3b781 100644 --- a/tools/mrbc/mrbc.c +++ b/tools/mrbc/mrbc.c @@ -144,12 +144,13 @@ parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args) } static void -cleanup(struct _args *args) +cleanup(mrb_state *mrb, struct _args *args) { if (args->rfp) fclose(args->rfp); if (args->wfp) fclose(args->wfp); + mrb_close(mrb); } int @@ -168,9 +169,8 @@ main(int argc, char **argv) n = parse_args(mrb, argc, argv, &args); if (n < 0 || args.rfp == NULL) { - cleanup(&args); + cleanup(mrb, &args); usage(argv[0]); - mrb_close(mrb); return n; } @@ -179,16 +179,14 @@ main(int argc, char **argv) c->dump_result = 1; c->no_exec = 1; result = mrb_load_file_cxt(mrb, args.rfp, c); - if (mrb_undef_p(result)) { - cleanup(&args); - mrb_close(mrb); - return -1; + if (mrb_undef_p(result) || mrb_fixnum(result) < 0) { + cleanup(mrb, &args); + return EXIT_FAILURE; } - n = mrb_fixnum(result); - if (n < 0 || args.check_syntax) { - cleanup(&args); - mrb_close(mrb); - return n; + if (args.check_syntax) { + printf("Syntax OK\n"); + cleanup(mrb, &args); + return EXIT_SUCCESS; } if (args.initname) { if (args.dump_type == DUMP_TYPE_BIN) @@ -200,10 +198,8 @@ main(int argc, char **argv) n = mrb_dump_irep(mrb, n, args.wfp); } - cleanup(&args); - mrb_close(mrb); - - return n; + cleanup(mrb, &args); + return EXIT_SUCCESS; } void -- cgit v1.2.3 From 83ccb715b2e1a4c9c718b38f9f5ab8558438e490 Mon Sep 17 00:00:00 2001 From: Yukihiro Matsumoto Date: Sat, 14 Jul 2012 07:45:24 +0900 Subject: mruby: print Syntax OK on -c --- tools/mruby/mruby.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'tools') diff --git a/tools/mruby/mruby.c b/tools/mruby/mruby.c index 65eae57e1..a8b0b99df 100644 --- a/tools/mruby/mruby.c +++ b/tools/mruby/mruby.c @@ -180,6 +180,7 @@ main(int argc, char **argv) } else { mrbc_context *c = mrbc_context_new(mrb); + mrb_value v; if (args.verbose) c->dump_result = 1; @@ -188,14 +189,16 @@ main(int argc, char **argv) if (args.cmdline) { mrbc_filename(mrb, c, "-e"); - mrb_load_string_cxt(mrb, (char*)args.cmdline, c); + v = mrb_load_string_cxt(mrb, (char*)args.cmdline, c); } else { mrbc_filename(mrb, c, argv[1]); - mrb_load_file_cxt(mrb, args.rfp, c); + v = mrb_load_file_cxt(mrb, args.rfp, c); } mrbc_context_free(mrb, c); - return -1; + if (!mrb->exc && args.check_syntax) { + printf("Syntax OK\n"); + } } cleanup(mrb, &args); -- cgit v1.2.3 From 72f7c51bc0bcef4096db8d57e7e5ab2a342e383f Mon Sep 17 00:00:00 2001 From: Yukihiro Matsumoto Date: Sat, 14 Jul 2012 15:39:53 +0900 Subject: mruby should print error properly --- src/parse.y | 5 ++++- tools/mruby/mruby.c | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) (limited to 'tools') diff --git a/src/parse.y b/src/parse.y index 75a33c5b7..c766b3927 100644 --- a/src/parse.y +++ b/src/parse.y @@ -4871,6 +4871,7 @@ static mrb_value load_exec(mrb_state *mrb, parser_state *p, mrbc_context *c) { int n; + mrb_value v; if (!p) { mrb_parser_free(p); @@ -4900,7 +4901,9 @@ load_exec(mrb_state *mrb, parser_state *p, mrbc_context *c) if (c->dump_result) codedump_all(mrb, n); if (c->no_exec) return mrb_fixnum_value(n); } - return mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb)); + v = mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb)); + if (!mrb->exc) return mrb_undef_value(); + return v; } mrb_value diff --git a/tools/mruby/mruby.c b/tools/mruby/mruby.c index a8b0b99df..d3c22f96d 100644 --- a/tools/mruby/mruby.c +++ b/tools/mruby/mruby.c @@ -196,9 +196,12 @@ main(int argc, char **argv) v = mrb_load_file_cxt(mrb, args.rfp, c); } mrbc_context_free(mrb, c); - if (!mrb->exc && args.check_syntax) { + if (args.check_syntax) { printf("Syntax OK\n"); } + else if (!mrb_undef_p(v) && mrb->exc) { + mrb_p(mrb, mrb_obj_value(mrb->exc)); + } } cleanup(mrb, &args); -- cgit v1.2.3 From c8700c4a2178dc0ab1824e98746d12754611768c Mon Sep 17 00:00:00 2001 From: Yukihiro Matsumoto Date: Sun, 15 Jul 2012 16:23:55 +0900 Subject: remove Syntax OK if syntax isn't ok; close #368 --- tools/mruby/mruby.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'tools') diff --git a/tools/mruby/mruby.c b/tools/mruby/mruby.c index d3c22f96d..abc0bdc5e 100644 --- a/tools/mruby/mruby.c +++ b/tools/mruby/mruby.c @@ -196,11 +196,13 @@ main(int argc, char **argv) v = mrb_load_file_cxt(mrb, args.rfp, c); } mrbc_context_free(mrb, c); - if (args.check_syntax) { - printf("Syntax OK\n"); + if (mrb->exc) { + if (!mrb_undef_p(v)) { + mrb_p(mrb, mrb_obj_value(mrb->exc)); + } } - else if (!mrb_undef_p(v) && mrb->exc) { - mrb_p(mrb, mrb_obj_value(mrb->exc)); + else if (args.check_syntax) { + printf("Syntax OK\n"); } } cleanup(mrb, &args); -- cgit v1.2.3 From 6ea5c6b5172eebd0937710418ad99f82fbcf28e7 Mon Sep 17 00:00:00 2001 From: Yukihiro Matsumoto Date: Tue, 17 Jul 2012 23:46:48 +0900 Subject: make mirb work even when DISABLE_STDIO is set --- tools/mirb/mirb.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'tools') diff --git a/tools/mirb/mirb.c b/tools/mirb/mirb.c index 8b36cb5a6..4159f12bd 100644 --- a/tools/mirb/mirb.c +++ b/tools/mirb/mirb.c @@ -13,6 +13,19 @@ #include #include +#ifndef ENABLE_STDIO +#include +static void +p(mrb_state *mrb, mrb_value obj) +{ + obj = mrb_funcall(mrb, obj, "inspect", 0); + fwrite(RSTRING_PTR(obj), RSTRING_LEN(obj), 1, stdout); + putc('\n', stdout); +} +#else +#define p(mrb,obj) mrb_p(mrb,obj) +#endif + /* Guess if the user might want to enter more * or if he wants an evaluation of his code now */ int @@ -219,13 +232,13 @@ main(void) mrb_top_self(mrb)); /* did an exception occur? */ if (mrb->exc) { - mrb_p(mrb, mrb_obj_value(mrb->exc)); + p(mrb, mrb_obj_value(mrb->exc)); mrb->exc = 0; } else { /* no */ printf(" => "); - mrb_p(mrb, result); + p(mrb, result); } } memset(ruby_code, 0, sizeof(*ruby_code)); -- cgit v1.2.3 From 42cfe5c9f39bf5b32d4b4754c170cfeb947389b4 Mon Sep 17 00:00:00 2001 From: Yukihiro Matsumoto Date: Tue, 17 Jul 2012 23:51:57 +0900 Subject: make mruby work even when DISABLE_STDIO is set --- tools/mruby/mruby.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'tools') diff --git a/tools/mruby/mruby.c b/tools/mruby/mruby.c index abc0bdc5e..26b1e44d5 100644 --- a/tools/mruby/mruby.c +++ b/tools/mruby/mruby.c @@ -7,6 +7,18 @@ #include #include +#ifndef ENABLE_STDIO +static void +p(mrb_state *mrb, mrb_value obj) +{ + obj = mrb_funcall(mrb, obj, "inspect", 0); + fwrite(RSTRING_PTR(obj), RSTRING_LEN(obj), 1, stdout); + putc('\n', stdout); +} +#else +#define p(mrb,obj) mrb_p(mrb,obj) +#endif + void mrb_show_version(mrb_state *); void mrb_show_copyright(mrb_state *); @@ -173,7 +185,7 @@ main(int argc, char **argv) if (!args.check_syntax) { mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb)); if (mrb->exc) { - mrb_p(mrb, mrb_obj_value(mrb->exc)); + p(mrb, mrb_obj_value(mrb->exc)); } } } @@ -198,7 +210,7 @@ main(int argc, char **argv) mrbc_context_free(mrb, c); if (mrb->exc) { if (!mrb_undef_p(v)) { - mrb_p(mrb, mrb_obj_value(mrb->exc)); + p(mrb, mrb_obj_value(mrb->exc)); } } else if (args.check_syntax) { -- cgit v1.2.3 From fdc224ff2fd43edfcb8cbe3d60a57bbe6daf1459 Mon Sep 17 00:00:00 2001 From: Yukihiro Matsumoto Date: Wed, 18 Jul 2012 14:43:14 +0900 Subject: remove unused label; close #381 --- tools/xpcat/xpcat.c | 2 -- 1 file changed, 2 deletions(-) (limited to 'tools') diff --git a/tools/xpcat/xpcat.c b/tools/xpcat/xpcat.c index c9d1abe73..ce3d5854e 100644 --- a/tools/xpcat/xpcat.c +++ b/tools/xpcat/xpcat.c @@ -60,9 +60,7 @@ main(int argc, char *argv[]) } } -done: fclose(outfile); - return EXIT_SUCCESS; } -- cgit v1.2.3 From ef993d75495c95a57863d6d0f900acbe8778336e Mon Sep 17 00:00:00 2001 From: Yuichiro MASUI Date: Sun, 29 Jul 2012 20:58:02 +0900 Subject: Added mrbc support output to stdout --- tools/mrbc/mrbc.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'tools') diff --git a/tools/mrbc/mrbc.c b/tools/mrbc/mrbc.c index 428a3b781..f9810aa4f 100644 --- a/tools/mrbc/mrbc.c +++ b/tools/mrbc/mrbc.c @@ -135,7 +135,10 @@ parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args) if (outfile == NULL) outfile = get_outfilename(infile, args->ext); - if ((args->wfp = fopen(outfile, "wb")) == NULL) { + if (strcmp("-", outfile) == 0) { + args->wfp = stdout; + } + else if ((args->wfp = fopen(outfile, "wb")) == NULL) { printf("%s: Cannot open output file. (%s)\n", *origargv, outfile); return 0; } -- cgit v1.2.3 From 50b9f54a6527477d825049c4f0a8f14f7bb4482c Mon Sep 17 00:00:00 2001 From: Max Anselm Date: Sun, 29 Jul 2012 20:11:08 -0400 Subject: Make void casts explicit. (Forgot a couple files) --- src/parse.y | 20 ++++++++++---------- tools/mruby/mruby.c | 6 +++--- 2 files changed, 13 insertions(+), 13 deletions(-) (limited to 'tools') diff --git a/src/parse.y b/src/parse.y index a2a12b14d..a9afc5c96 100644 --- a/src/parse.y +++ b/src/parse.y @@ -94,7 +94,7 @@ cons_gen(parser_state *p, node *car, node *cdr) p->cells = p->cells->cdr; } else { - c = parser_palloc(p, sizeof(mrb_ast_node)); + c = (node *)parser_palloc(p, sizeof(mrb_ast_node)); } c->car = car; @@ -165,7 +165,7 @@ append_gen(parser_state *p, node *a, node *b) static char* parser_strndup(parser_state *p, const char *s, size_t len) { - char *b = parser_palloc(p, len+1); + char *b = (char *)parser_palloc(p, len+1); memcpy(b, s, len); b[len] = '\0'; @@ -2852,7 +2852,7 @@ singleton : var_ref yyerror(p, "can't define singleton method for ()."); } else { - switch ((enum node_type)$3->car) { + switch ((enum node_type)(int)(intptr_t)$3->car) { case NODE_STR: case NODE_DSTR: case NODE_DREGX: @@ -2968,7 +2968,7 @@ yyerror(parser_state *p, const char *s) } else if (p->nerr < sizeof(p->error_buffer) / sizeof(p->error_buffer[0])) { n = strlen(s); - c = parser_palloc(p, n + 1); + c = (char *)parser_palloc(p, n + 1); memcpy(c, s, n + 1); p->error_buffer[p->nerr].message = c; p->error_buffer[p->nerr].lineno = p->lineno; @@ -3004,7 +3004,7 @@ yywarn(parser_state *p, const char *s) } else if (p->nerr < sizeof(p->warn_buffer) / sizeof(p->warn_buffer[0])) { n = strlen(s); - c = parser_palloc(p, n + 1); + c = (char *)parser_palloc(p, n + 1); memcpy(c, s, n + 1); p->warn_buffer[p->nwarn].message = c; p->warn_buffer[p->nwarn].lineno = p->lineno; @@ -3254,7 +3254,7 @@ scan_hex(const int *start, int len, int *retlen) register unsigned long retval = 0; char *tmp; - while (len-- && *s && (tmp = strchr(hexdigit, *s))) { + while (len-- && *s && (tmp = (char *)strchr(hexdigit, *s))) { retval <<= 4; retval |= (tmp - hexdigit) & 15; s++; @@ -4701,7 +4701,7 @@ parser_update_cxt(parser_state *p, mrbc_context *cxt) i++; n = n->cdr; } - cxt->syms = mrb_realloc(p->mrb, cxt->syms, i*sizeof(mrb_sym)); + cxt->syms = (mrb_sym *)mrb_realloc(p->mrb, cxt->syms, i*sizeof(mrb_sym)); cxt->slen = i; for (i=0, n=n0; n; i++,n=n->cdr) { cxt->syms[i] = (mrb_sym)n->car; @@ -4759,7 +4759,7 @@ mrb_parser_new(mrb_state *mrb) pool = mrb_pool_open(mrb); if (!pool) return 0; - p = mrb_pool_alloc(pool, sizeof(parser_state)); + p = (parser_state *)mrb_pool_alloc(pool, sizeof(parser_state)); if (!p) return 0; memset(p, 0, sizeof(parser_state)); @@ -4793,7 +4793,7 @@ mrbc_context_new(mrb_state *mrb) { mrbc_context *c; - c = mrb_calloc(mrb, 1, sizeof(mrbc_context)); + c = (mrbc_context *)mrb_calloc(mrb, 1, sizeof(mrbc_context)); return c; } @@ -4810,7 +4810,7 @@ mrbc_filename(mrb_state *mrb, mrbc_context *c, const char *s) { if (s) { int len = strlen(s); - char *p = mrb_malloc(mrb, len); + char *p = (char *)mrb_malloc(mrb, len); memcpy(p, s, len); if (c->filename) mrb_free(mrb, c->filename); diff --git a/tools/mruby/mruby.c b/tools/mruby/mruby.c index 26b1e44d5..e6a088672 100644 --- a/tools/mruby/mruby.c +++ b/tools/mruby/mruby.c @@ -86,12 +86,12 @@ append_cmdline: if (!args->cmdline) { char *buf; - buf = mrb_malloc(mrb, strlen(item)+1); + buf = (char *)mrb_malloc(mrb, strlen(item)+1); strcpy(buf, item); args->cmdline = buf; } else { - args->cmdline = mrb_realloc(mrb, args->cmdline, strlen(args->cmdline)+strlen(item)+2); + args->cmdline = (char *)mrb_realloc(mrb, args->cmdline, strlen(args->cmdline)+strlen(item)+2); strcat(args->cmdline, "\n"); strcat(args->cmdline, item); } @@ -133,7 +133,7 @@ append_cmdline: return 0; } } - args->argv = mrb_realloc(mrb, args->argv, sizeof(char*) * (argc + 1)); + args->argv = (char **)mrb_realloc(mrb, args->argv, sizeof(char*) * (argc + 1)); memcpy(args->argv, argv, (argc+1) * sizeof(char*)); args->argc = argc; -- cgit v1.2.3 From 1ff6dfd081664625d71c0f6fcb3c2c1ae58ef1f5 Mon Sep 17 00:00:00 2001 From: Yukihiro Matsumoto Date: Wed, 1 Aug 2012 17:52:14 +0900 Subject: should not leave open code on syntax errors; close #413 --- tools/mirb/mirb.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tools') diff --git a/tools/mirb/mirb.c b/tools/mirb/mirb.c index 4159f12bd..54588d57e 100644 --- a/tools/mirb/mirb.c +++ b/tools/mirb/mirb.c @@ -50,10 +50,10 @@ is_code_block_open(struct mrb_parser_state *parser) code_block_open = TRUE; } else if (strcmp(message, "syntax error, unexpected keyword_end") == 0) { - code_block_open = TRUE; + code_block_open = FALSE; } else if (strcmp(message, "syntax error, unexpected tREGEXP_BEG") == 0) { - code_block_open = TRUE; + code_block_open = FALSE; } return code_block_open; } -- cgit v1.2.3