diff options
| author | Masaki Muranaka <[email protected]> | 2013-04-11 13:15:12 +0900 |
|---|---|---|
| committer | Masaki Muranaka <[email protected]> | 2013-04-11 15:22:07 +0900 |
| commit | 9d8143f9302f86348df5d0a75ebbb433c8330354 (patch) | |
| tree | 187250db57b68633c7b28bbe401333d774e65bfe /mrbgems | |
| parent | 3806b53db5ff4e73df4d4b7600ba0fbdbef6e0d5 (diff) | |
| download | mruby-9d8143f9302f86348df5d0a75ebbb433c8330354.tar.gz mruby-9d8143f9302f86348df5d0a75ebbb433c8330354.zip | |
Move mirb and mruby to mrbgems.
Diffstat (limited to 'mrbgems')
| -rw-r--r-- | mrbgems/mruby-tool-mirb/mrbgem.rake | 5 | ||||
| -rw-r--r-- | mrbgems/mruby-tool-mirb/tools/mirb/mirb.c | 350 | ||||
| -rw-r--r-- | mrbgems/mruby-tool-mruby/mrbgem.rake | 5 | ||||
| -rw-r--r-- | mrbgems/mruby-tool-mruby/tools/mruby/mruby.c | 307 |
4 files changed, 667 insertions, 0 deletions
diff --git a/mrbgems/mruby-tool-mirb/mrbgem.rake b/mrbgems/mruby-tool-mirb/mrbgem.rake new file mode 100644 index 000000000..d4296319e --- /dev/null +++ b/mrbgems/mruby-tool-mirb/mrbgem.rake @@ -0,0 +1,5 @@ +MRuby::Gem::Specification.new('mruby-tool-mirb') do |spec| + spec.license = 'MIT' + spec.authors = 'mruby developers' + spec.bins = %w(mirb) +end diff --git a/mrbgems/mruby-tool-mirb/tools/mirb/mirb.c b/mrbgems/mruby-tool-mirb/tools/mirb/mirb.c new file mode 100644 index 000000000..cde0b0d4b --- /dev/null +++ b/mrbgems/mruby-tool-mirb/tools/mirb/mirb.c @@ -0,0 +1,350 @@ +/* +** mirb - Embeddable Interactive Ruby Shell +** +** This program takes code from the user in +** an interactive way and executes it +** immediately. It's a REPL... +*/ + +#include <stdlib.h> +#include <string.h> + +#include <mruby.h> +#include "mruby/array.h" +#include <mruby/proc.h> +#include <mruby/data.h> +#include <mruby/compile.h> +#ifdef ENABLE_READLINE +#include <readline/readline.h> +#include <readline/history.h> +#endif + +#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 +is_code_block_open(struct mrb_parser_state *parser) +{ + int code_block_open = FALSE; + + /* check for heredoc */ + if (parser->parsing_heredoc != NULL) return TRUE; + if (parser->heredoc_end_now) { + parser->heredoc_end_now = FALSE; + return FALSE; + } + + /* check if parser error are available */ + if (0 < parser->nerr) { + const char *unexpected_end = "syntax error, unexpected $end"; + const char *message = parser->error_buffer[0].message; + + /* a parser error occur, we have to check if */ + /* we need to read one more line or if there is */ + /* a different issue which we have to show to */ + /* the user */ + + if (strncmp(message, unexpected_end, strlen(unexpected_end)) == 0) { + code_block_open = TRUE; + } + else if (strcmp(message, "syntax error, unexpected keyword_end") == 0) { + code_block_open = FALSE; + } + else if (strcmp(message, "syntax error, unexpected tREGEXP_BEG") == 0) { + code_block_open = FALSE; + } + return code_block_open; + } + + /* check for unterminated string */ + if (parser->lex_strterm) return TRUE; + + switch (parser->lstate) { + + /* all states which need more code */ + + case EXPR_BEG: + /* an expression was just started, */ + /* we can't end it like this */ + code_block_open = TRUE; + break; + case EXPR_DOT: + /* a message dot was the last token, */ + /* there has to come more */ + code_block_open = TRUE; + break; + case EXPR_CLASS: + /* a class keyword is not enough! */ + /* we need also a name of the class */ + code_block_open = TRUE; + break; + case EXPR_FNAME: + /* a method name is necessary */ + code_block_open = TRUE; + break; + case EXPR_VALUE: + /* if, elsif, etc. without condition */ + code_block_open = TRUE; + break; + + /* now all the states which are closed */ + + case EXPR_ARG: + /* an argument is the last token */ + code_block_open = FALSE; + break; + + /* all states which are unsure */ + + case EXPR_CMDARG: + break; + case EXPR_END: + /* an expression was ended */ + break; + case EXPR_ENDARG: + /* closing parenthese */ + break; + case EXPR_ENDFN: + /* definition end */ + break; + case EXPR_MID: + /* jump keyword like break, return, ... */ + break; + case EXPR_MAX_STATE: + /* don't know what to do with this token */ + break; + default: + /* this state is unexpected! */ + break; + } + + return code_block_open; +} + +void mrb_show_version(mrb_state *); +void mrb_show_copyright(mrb_state *); + +struct _args { + int argc; + char** argv; +}; + +static void +usage(const char *name) +{ + static const char *const usage_msg[] = { + "switches:", + "--version print the version", + "--copyright print the copyright", + NULL + }; + const char *const *p = usage_msg; + + printf("Usage: %s [switches]\n", name); + while(*p) + printf(" %s\n", *p++); +} + +static int +parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args) +{ + static const struct _args args_zero = { 0 }; + + *args = args_zero; + + for (argc--,argv++; argc > 0; argc--,argv++) { + char *item; + if (argv[0][0] != '-') break; + + item = argv[0] + 1; + switch (*item++) { + case '-': + if (strcmp((*argv) + 2, "version") == 0) { + mrb_show_version(mrb); + exit(EXIT_SUCCESS); + } + else if (strcmp((*argv) + 2, "copyright") == 0) { + mrb_show_copyright(mrb); + exit(EXIT_SUCCESS); + } + default: + return EXIT_FAILURE; + } + } + return EXIT_SUCCESS; +} + +static void +cleanup(mrb_state *mrb, struct _args *args) +{ + mrb_close(mrb); +} + +/* Print a short remark for the user */ +static void +print_hint(void) +{ + printf("mirb - Embeddable Interactive Ruby Shell\n"); + printf("\nThis is a very early version, please test and report errors.\n"); + printf("Thanks :)\n\n"); +} + +/* Print the command line prompt of the REPL */ +void +print_cmdline(int code_block_open) +{ + if (code_block_open) { + printf("* "); + } + else { + printf("> "); + } +} + +int +main(int argc, char **argv) +{ + char ruby_code[1024] = { 0 }; + char last_code_line[1024] = { 0 }; +#ifndef ENABLE_READLINE + int last_char; + int char_index; +#endif + mrbc_context *cxt; + struct mrb_parser_state *parser; + mrb_state *mrb; + mrb_value result; + struct _args args; + int n; + int code_block_open = FALSE; + int ai; + + /* new interpreter instance */ + mrb = mrb_open(); + if (mrb == NULL) { + fputs("Invalid mrb interpreter, exiting mirb\n", stderr); + return EXIT_FAILURE; + } + mrb_define_global_const(mrb, "ARGV", mrb_ary_new_capa(mrb, 0)); + + n = parse_args(mrb, argc, argv, &args); + if (n == EXIT_FAILURE) { + cleanup(mrb, &args); + usage(argv[0]); + return n; + } + + print_hint(); + + cxt = mrbc_context_new(mrb); + cxt->capture_errors = 1; + + ai = mrb_gc_arena_save(mrb); + while (TRUE) { +#ifndef ENABLE_READLINE + print_cmdline(code_block_open); + + char_index = 0; + while ((last_char = getchar()) != '\n') { + if (last_char == EOF) break; + last_code_line[char_index++] = last_char; + } + if (last_char == EOF) { + fputs("\n", stdout); + break; + } + + last_code_line[char_index] = '\0'; +#else + char* line = readline(code_block_open ? "* " : "> "); + if(line == NULL) { + printf("\n"); + break; + } + strncpy(last_code_line, line, sizeof(last_code_line)-1); + add_history(line); + free(line); +#endif + + if ((strcmp(last_code_line, "quit") == 0) || (strcmp(last_code_line, "exit") == 0)) { + if (!code_block_open) { + break; + } + else{ + /* count the quit/exit commands as strings if in a quote block */ + strcat(ruby_code, "\n"); + strcat(ruby_code, last_code_line); + } + } + else { + if (code_block_open) { + strcat(ruby_code, "\n"); + strcat(ruby_code, last_code_line); + } + else { + strcpy(ruby_code, last_code_line); + } + } + + /* parse code */ + parser = mrb_parser_new(mrb); + parser->s = ruby_code; + parser->send = ruby_code + strlen(ruby_code); + parser->lineno = 1; + mrb_parser_parse(parser, cxt); + code_block_open = is_code_block_open(parser); + + if (code_block_open) { + /* no evaluation of code */ + } + else { + if (0 < parser->nerr) { + /* syntax error */ + printf("line %d: %s\n", parser->error_buffer[0].lineno, parser->error_buffer[0].message); + } + else { + /* generate bytecode */ + n = mrb_generate_code(mrb, parser); + + /* evaluate the bytecode */ + result = mrb_run(mrb, + /* pass a proc for evaulation */ + mrb_proc_new(mrb, mrb->irep[n]), + mrb_top_self(mrb)); + /* did an exception occur? */ + if (mrb->exc) { + p(mrb, mrb_obj_value(mrb->exc)); + mrb->exc = 0; + } + else { + /* no */ + printf(" => "); + if (!mrb_respond_to(mrb,result,mrb_intern(mrb,"inspect"))){ + result = mrb_any_to_s(mrb,result); + } + p(mrb, result); + } + } + ruby_code[0] = '\0'; + last_code_line[0] = '\0'; + mrb_gc_arena_restore(mrb, ai); + } + mrb_parser_free(parser); + } + mrbc_context_free(mrb, cxt); + mrb_close(mrb); + + return 0; +} diff --git a/mrbgems/mruby-tool-mruby/mrbgem.rake b/mrbgems/mruby-tool-mruby/mrbgem.rake new file mode 100644 index 000000000..6e6d57b00 --- /dev/null +++ b/mrbgems/mruby-tool-mruby/mrbgem.rake @@ -0,0 +1,5 @@ +MRuby::Gem::Specification.new('mruby-tool-mruby') do |spec| + spec.license = 'MIT' + spec.authors = 'mruby developers' + spec.bins = %w(mruby) +end diff --git a/mrbgems/mruby-tool-mruby/tools/mruby/mruby.c b/mrbgems/mruby-tool-mruby/tools/mruby/mruby.c new file mode 100644 index 000000000..b8d82b270 --- /dev/null +++ b/mrbgems/mruby-tool-mruby/tools/mruby/mruby.c @@ -0,0 +1,307 @@ +#include "mruby.h" +#include "mruby/proc.h" +#include "mruby/array.h" +#include "mruby/string.h" +#include "mruby/compile.h" +#include "mruby/dump.h" +#include "mruby/variable.h" +#include <stdio.h> +#include <stdlib.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 *); + +struct _args { + FILE *rfp; + char* cmdline; + mrb_bool fname : 1; + mrb_bool mrbfile : 1; + mrb_bool check_syntax : 1; + mrb_bool verbose : 1; + int argc; + char** argv; +}; + +static void +usage(const char *name) +{ + static const char *const usage_msg[] = { + "switches:", + "-b load and execute RiteBinary (mrb) file", + "-c check syntax only", + "-e 'command' one line of script", + "-v print version number, then run in verbose mode", + "--verbose run in verbose mode", + "--version print the version", + "--copyright print the copyright", + NULL + }; + const char *const *p = usage_msg; + + printf("Usage: %s [switches] programfile\n", name); + while(*p) + printf(" %s\n", *p++); +} + +static int +parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args) +{ + char **origargv = argv; + static const struct _args args_zero = { 0 }; + + *args = args_zero; + + for (argc--,argv++; argc > 0; argc--,argv++) { + char *item; + if (argv[0][0] != '-') break; + + if (strlen(*argv) <= 1) { + argc--; argv++; + args->rfp = stdin; + break; + } + + item = argv[0] + 1; + switch (*item++) { + case 'b': + args->mrbfile = 1; + break; + case 'c': + args->check_syntax = 1; + break; + case 'e': + if (item[0]) { + goto append_cmdline; + } + else if (argc > 1) { + argc--; argv++; + item = argv[0]; +append_cmdline: + if (!args->cmdline) { + char *buf; + + buf = (char *)mrb_malloc(mrb, strlen(item)+1); + strcpy(buf, item); + args->cmdline = buf; + } + else { + args->cmdline = (char *)mrb_realloc(mrb, args->cmdline, strlen(args->cmdline)+strlen(item)+2); + strcat(args->cmdline, "\n"); + strcat(args->cmdline, item); + } + } + else { + printf("%s: No code specified for -e\n", *origargv); + return EXIT_SUCCESS; + } + break; + case 'v': + if (!args->verbose) mrb_show_version(mrb); + args->verbose = 1; + break; + case '-': + if (strcmp((*argv) + 2, "version") == 0) { + mrb_show_version(mrb); + exit(EXIT_SUCCESS); + } + else if (strcmp((*argv) + 2, "verbose") == 0) { + args->verbose = 1; + break; + } + else if (strcmp((*argv) + 2, "copyright") == 0) { + mrb_show_copyright(mrb); + exit(EXIT_SUCCESS); + } + default: + return EXIT_FAILURE; + } + } + + if (args->rfp == NULL && args->cmdline == NULL) { + if (*argv == NULL) args->rfp = stdin; + else { + args->rfp = fopen(argv[0], args->mrbfile ? "rb" : "r"); + if (args->rfp == NULL) { + printf("%s: Cannot open program file. (%s)\n", *origargv, *argv); + return 0; + } + args->fname = 1; + args->cmdline = argv[0]; + argc--; argv++; + } + } + args->argv = (char **)mrb_realloc(mrb, args->argv, sizeof(char*) * (argc + 1)); + memcpy(args->argv, argv, (argc+1) * sizeof(char*)); + args->argc = argc; + + return EXIT_SUCCESS; +} + +static void +cleanup(mrb_state *mrb, struct _args *args) +{ + if (args->rfp && args->rfp != stdin) + fclose(args->rfp); + if (args->cmdline && !args->fname) + mrb_free(mrb, args->cmdline); + if (args->argv) + mrb_free(mrb, args->argv); + mrb_close(mrb); +} + +static void +showcallinfo(mrb_state *mrb) +{ + mrb_callinfo *ci; + mrb_int ciidx; + const char *filename, *method, *sep; + int i, line; + + printf("trace:\n"); + ciidx = mrb_fixnum(mrb_obj_iv_get(mrb, mrb->exc, mrb_intern(mrb, "ciidx"))); + if (ciidx >= mrb->ciend - mrb->cibase) + ciidx = 10; /* ciidx is broken... */ + + for (i = ciidx; i >= 0; i--) { + ci = &mrb->cibase[i]; + filename = "(unknown)"; + line = -1; + + if (MRB_PROC_CFUNC_P(ci->proc)) { + continue; + } + else { + mrb_irep *irep = ci->proc->body.irep; + if (irep->filename != NULL) + filename = irep->filename; + if (irep->lines != NULL) { + mrb_code *pc; + + if (i+1 <= ciidx) { + pc = mrb->cibase[i+1].pc; + } + else { + pc = (mrb_code*)mrb_voidp(mrb_obj_iv_get(mrb, mrb->exc, mrb_intern(mrb, "lastpc"))); + } + if (irep->iseq <= pc && pc < irep->iseq + irep->ilen) { + line = irep->lines[pc - irep->iseq - 1]; + } + } + } + if (line == -1) continue; + if (ci->target_class == ci->proc->target_class) + sep = "."; + else + sep = "#"; + + method = mrb_sym2name(mrb, ci->mid); + if (method) { + const char *cn = mrb_class_name(mrb, ci->proc->target_class); + + if (cn) { + printf("\t[%d] %s:%d:in %s%s%s\n", i, filename, line, cn, sep, method); + } + else { + printf("\t[%d] %s:%d:in %s\n", i, filename, line, method); + } + } + else { + printf("\t[%d] %s:%d\n", i, filename, line); + } + } +} + +int +main(int argc, char **argv) +{ + mrb_state *mrb = mrb_open(); + int n = -1; + int i; + struct _args args; + mrb_value ARGV; + + if (mrb == NULL) { + fputs("Invalid mrb_state, exiting mruby\n", stderr); + return EXIT_FAILURE; + } + + n = parse_args(mrb, argc, argv, &args); + if (n == EXIT_FAILURE || (args.cmdline == NULL && args.rfp == NULL)) { + cleanup(mrb, &args); + usage(argv[0]); + return n; + } + + ARGV = mrb_ary_new_capa(mrb, args.argc); + 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_read_irep_file(mrb, args.rfp); + if (n < 0) { + fprintf(stderr, "failed to load mrb file: %s\n", args.cmdline); + } + else if (!args.check_syntax) { + mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb)); + n = 0; + if (mrb->exc) { + showcallinfo(mrb); + p(mrb, mrb_obj_value(mrb->exc)); + n = -1; + } + } + } + else { + mrbc_context *c = mrbc_context_new(mrb); + mrb_sym zero_sym = mrb_intern2(mrb, "$0", 2); + mrb_value v; + + if (args.verbose) + c->dump_result = 1; + if (args.check_syntax) + c->no_exec = 1; + + if (args.rfp) { + char *cmdline; + cmdline = args.cmdline ? args.cmdline : "-"; + mrbc_filename(mrb, c, cmdline); + mrb_gv_set(mrb, zero_sym, mrb_str_new_cstr(mrb, cmdline)); + v = mrb_load_file_cxt(mrb, args.rfp, c); + } + else { + mrbc_filename(mrb, c, "-e"); + mrb_gv_set(mrb, zero_sym, mrb_str_new(mrb, "-e", 2)); + v = mrb_load_string_cxt(mrb, args.cmdline, c); + } + + mrbc_context_free(mrb, c); + if (mrb->exc) { + if (!mrb_undef_p(v)) { + showcallinfo(mrb); + p(mrb, mrb_obj_value(mrb->exc)); + } + n = -1; + } + else if (args.check_syntax) { + printf("Syntax OK\n"); + } + } + cleanup(mrb, &args); + + return n == 0 ? EXIT_SUCCESS : EXIT_FAILURE; +} |
