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 +++++ 1 file changed, 5 insertions(+) (limited to 'tools/mirb') 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)); -- 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/mirb') 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 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/mirb') 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/mirb') 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/mirb') 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/mirb') 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 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/mirb') 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 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/mirb') 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