diff options
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/mirb/Makefile | 13 | ||||
| -rw-r--r-- | tools/mirb/mirb.c | 60 | ||||
| -rw-r--r-- | tools/mrbc/Makefile | 17 | ||||
| -rw-r--r-- | tools/mrbc/mrbc.c | 60 | ||||
| -rw-r--r-- | tools/mruby/Makefile | 17 | ||||
| -rw-r--r-- | tools/mruby/mruby.c | 111 | ||||
| -rw-r--r-- | tools/xpcat/xpcat.c | 2 |
7 files changed, 171 insertions, 109 deletions
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/mirb/mirb.c b/tools/mirb/mirb.c index 59e5046cb..54588d57e 100644 --- a/tools/mirb/mirb.c +++ b/tools/mirb/mirb.c @@ -13,6 +13,19 @@ #include <mruby/data.h> #include <mruby/compile.h> +#ifndef ENABLE_STDIO +#include <mruby/string.h> +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 @@ -37,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; } @@ -132,18 +145,24 @@ 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_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(); - /* new parser instance */ - parser = mrb_parser_new(mrb_interpreter); + mrb = mrb_open(); + if (mrb == NULL) { + fprintf(stderr, "Invalid mrb interpreter, exiting mirb"); + return EXIT_FAILURE; + } + + 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)); @@ -187,11 +206,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); + mrb_parser_parse(parser, cxt); code_block_open = is_code_block_open(parser); if (code_block_open) { @@ -204,31 +223,32 @@ 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) { + p(mrb, mrb_obj_value(mrb->exc)); + mrb->exc = 0; } else { /* no */ printf(" => "); - mrb_p(mrb_interpreter, mrb_return_value); + p(mrb, result); } } - memset(ruby_code, 0, sizeof(*ruby_code)); memset(ruby_code, 0, sizeof(*last_code_line)); } + mrb_parser_free(parser); } } - mrb_close(mrb_interpreter); + mrbc_context_free(mrb, cxt); + mrb_close(mrb); return 0; } 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/mrbc/mrbc.c b/tools/mrbc/mrbc.c index 3553fe646..f9810aa4f 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) { @@ -133,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; } @@ -142,12 +147,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 @@ -156,37 +162,34 @@ 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; - n = parse_args(mrb, argc, argv, &args); + 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) { - cleanup(&args); + cleanup(mrb, &args); usage(argv[0]); - mrb_close(mrb); return n; } - p = mrb_parse_file(mrb, args.rfp); - if (!p || !p->tree || p->nerr) { - cleanup(&args); - mrb_close(mrb); - return -1; - } - - if (args.verbose) - parser_dump(mrb, p->tree, 0); - - n = mrb_generate_code(mrb, p->tree); - mrb_pool_close(p->pool); - + c = mrbc_context_new(mrb); if (args.verbose) - codedump_all(mrb, n); - - if (n < 0 || args.check_syntax) { - cleanup(&args); - mrb_close(mrb); - return n; + c->dump_result = 1; + c->no_exec = 1; + result = mrb_load_file_cxt(mrb, args.rfp, c); + if (mrb_undef_p(result) || mrb_fixnum(result) < 0) { + cleanup(mrb, &args); + return EXIT_FAILURE; + } + if (args.check_syntax) { + printf("Syntax OK\n"); + cleanup(mrb, &args); + return EXIT_SUCCESS; } if (args.initname) { if (args.dump_type == DUMP_TYPE_BIN) @@ -198,14 +201,11 @@ 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 mrb_init_mrblib(mrb_state *mrb) { } - 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)" diff --git a/tools/mruby/mruby.c b/tools/mruby/mruby.c index 8b227df5d..e6a088672 100644 --- a/tools/mruby/mruby.c +++ b/tools/mruby/mruby.c @@ -7,10 +7,20 @@ #include <stdio.h> #include <string.h> +#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 *); -void parser_dump(mrb_state*, struct mrb_ast_node*, int); -void codedump_all(mrb_state*, int); struct _args { FILE *rfp; @@ -51,12 +61,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 +76,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 = (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(argv[0])+2); + args->cmdline = (char *)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 { @@ -103,6 +120,8 @@ parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args) } else return -3; return 0; + default: + break; } } @@ -114,7 +133,7 @@ parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args) 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; @@ -140,7 +159,12 @@ 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"); + return EXIT_FAILURE; + } n = parse_args(mrb, argc, argv, &args); if (n < 0 || (args.cmdline == NULL && args.rfp == NULL)) { @@ -149,51 +173,50 @@ 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); - } - else { - if (args.cmdline) { - p = mrb_parse_string(mrb, (char*)args.cmdline); - } - else { - p = mrb_parser_new(mrb); - if (p) { - mrb_parser_filename(p, argv[1]); - p->f = args.rfp; - mrb_parser_parse(p); + if (n >= 0) { + if (!args.check_syntax) { + mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb)); + if (mrb->exc) { + p(mrb, mrb_obj_value(mrb->exc)); + } } } - 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_pool_close(p->pool); } - - 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); + else { + mrbc_context *c = mrbc_context_new(mrb); + mrb_value v; if (args.verbose) - codedump_all(mrb, n); + c->dump_result = 1; + if (args.check_syntax) + c->no_exec = 1; - 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)); + if (args.cmdline) { + mrbc_filename(mrb, c, "-e"); + v = mrb_load_string_cxt(mrb, (char*)args.cmdline, c); + } + else { + mrbc_filename(mrb, c, argv[1]); + v = mrb_load_file_cxt(mrb, args.rfp, c); + } + mrbc_context_free(mrb, c); + if (mrb->exc) { + if (!mrb_undef_p(v)) { + p(mrb, mrb_obj_value(mrb->exc)); } } + else if (args.check_syntax) { + printf("Syntax OK\n"); + } } - cleanup(mrb, &args); return n > 0 ? 0 : 1; 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; } |
