This is a test program for mruby mirb. Currently, I'm sure the only work in "Renesas GR-PEACH".

Dependencies:   mbed-src mruby-mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 ** mirb - Embeddable Interactive Ruby Shell
00003 **
00004 ** This program takes code from the user in
00005 ** an interactive way and executes it
00006 ** immediately. It's a REPL...
00007 */
00008 
00009 #include "mbed.h"
00010  
00011 Serial pc(USBTX, USBRX);
00012 
00013 #include <stdlib.h>
00014 #include <string.h>
00015 #include <stdio.h>
00016 #include <ctype.h>
00017 
00018 #ifdef ENABLE_READLINE
00019 #include <readline/readline.h>
00020 #include <readline/history.h>
00021 #define MIRB_ADD_HISTORY(line) add_history(line)
00022 #define MIRB_READLINE(ch) readline(ch)
00023 #define MIRB_WRITE_HISTORY(path) write_history(path)
00024 #define MIRB_READ_HISTORY(path) read_history(path)
00025 #define MIRB_USING_HISTORY() using_history()
00026 #elif defined(ENABLE_LINENOISE)
00027 #define ENABLE_READLINE
00028 #include <linenoise.h>
00029 #define MIRB_ADD_HISTORY(line) linenoiseHistoryAdd(line)
00030 #define MIRB_READLINE(ch) linenoise(ch)
00031 #define MIRB_WRITE_HISTORY(path) linenoiseHistorySave(path)
00032 #define MIRB_READ_HISTORY(path) linenoiseHistoryLoad(history_path)
00033 #define MIRB_USING_HISTORY()
00034 #endif
00035 
00036 #include "mruby.h"
00037 #include "mruby/array.h"
00038 #include "mruby/proc.h"
00039 #include "mruby/compile.h"
00040 #include "mruby/string.h"
00041 
00042 #ifdef ENABLE_READLINE
00043 
00044 static const char history_file_name[] = ".mirb_history";
00045 
00046 static char *
00047 get_history_path(mrb_state *mrb)
00048 {
00049   char *path = NULL;
00050   const char *home = getenv("HOME");
00051 
00052 #ifdef _WIN32
00053   if (home != NULL) {
00054     home = getenv("USERPROFILE");
00055   }
00056 #endif
00057 
00058   if (home != NULL) {
00059     int len = snprintf(NULL, 0, "%s/%s", home, history_file_name);
00060     if (len >= 0) {
00061       size_t size = len + 1;
00062       path = (char *)mrb_malloc_simple(mrb, size);
00063       if (path != NULL) {
00064         int n = snprintf(path, size, "%s/%s", home, history_file_name);
00065         if (n != len) {
00066           mrb_free(mrb, path);
00067           path = NULL;
00068         }
00069       }
00070     }
00071   }
00072 
00073   return path;
00074 }
00075 
00076 #endif
00077 
00078 static void
00079 p(mrb_state *mrb, mrb_value obj, int prompt)
00080 {
00081   obj = mrb_funcall(mrb, obj, "inspect", 0);
00082   if (prompt) {
00083     if (!mrb->exc) {
00084       pc.printf(" => ");
00085     }
00086     else {
00087       obj = mrb_funcall(mrb, mrb_obj_value(mrb->exc), "inspect", 0);
00088     }
00089   }
00090   fwrite(RSTRING_PTR(obj), RSTRING_LEN(obj), 1, stdout);
00091   putc('\n', stdout);
00092 }
00093 
00094 /* Guess if the user might want to enter more
00095  * or if he wants an evaluation of his code now */
00096 static mrb_bool
00097 is_code_block_open(struct mrb_parser_state *parser)
00098 {
00099   mrb_bool code_block_open = FALSE;
00100 
00101   /* check for heredoc */
00102   if (parser->parsing_heredoc != NULL) return TRUE;
00103   if (parser->heredoc_end_now) {
00104     parser->heredoc_end_now = FALSE;
00105     return FALSE;
00106   }
00107 
00108   /* check for unterminated string */
00109   if (parser->lex_strterm) return TRUE;
00110 
00111   /* check if parser error are available */
00112   if (0 < parser->nerr) {
00113     const char unexpected_end[] = "syntax error, unexpected $end";
00114     const char *message = parser->error_buffer[0].message;
00115 
00116     /* a parser error occur, we have to check if */
00117     /* we need to read one more line or if there is */
00118     /* a different issue which we have to show to */
00119     /* the user */
00120 
00121     if (strncmp(message, unexpected_end, sizeof(unexpected_end) - 1) == 0) {
00122       code_block_open = TRUE;
00123     }
00124     else if (strcmp(message, "syntax error, unexpected keyword_end") == 0) {
00125       code_block_open = FALSE;
00126     }
00127     else if (strcmp(message, "syntax error, unexpected tREGEXP_BEG") == 0) {
00128       code_block_open = FALSE;
00129     }
00130     return code_block_open;
00131   }
00132 
00133   switch (parser->lstate) {
00134 
00135   /* all states which need more code */
00136 
00137   case EXPR_BEG:
00138     /* beginning of a statement, */
00139     /* that means previous line ended */
00140     code_block_open = FALSE;
00141     break;
00142   case EXPR_DOT:
00143     /* a message dot was the last token, */
00144     /* there has to come more */
00145     code_block_open = TRUE;
00146     break;
00147   case EXPR_CLASS:
00148     /* a class keyword is not enough! */
00149     /* we need also a name of the class */
00150     code_block_open = TRUE;
00151     break;
00152   case EXPR_FNAME:
00153     /* a method name is necessary */
00154     code_block_open = TRUE;
00155     break;
00156   case EXPR_VALUE:
00157     /* if, elsif, etc. without condition */
00158     code_block_open = TRUE;
00159     break;
00160 
00161   /* now all the states which are closed */
00162 
00163   case EXPR_ARG:
00164     /* an argument is the last token */
00165     code_block_open = FALSE;
00166     break;
00167 
00168   /* all states which are unsure */
00169 
00170   case EXPR_CMDARG:
00171     break;
00172   case EXPR_END:
00173     /* an expression was ended */
00174     break;
00175   case EXPR_ENDARG:
00176     /* closing parenthese */
00177     break;
00178   case EXPR_ENDFN:
00179     /* definition end */
00180     break;
00181   case EXPR_MID:
00182     /* jump keyword like break, return, ... */
00183     break;
00184   case EXPR_MAX_STATE:
00185     /* don't know what to do with this token */
00186     break;
00187   default:
00188     /* this state is unexpected! */
00189     break;
00190   }
00191 
00192   return code_block_open;
00193 }
00194 
00195 
00196 #if defined(__cplusplus)
00197 extern "C" {
00198 #endif
00199 void mrb_show_version(mrb_state *);
00200 void mrb_show_copyright(mrb_state *);
00201 #if defined(__cplusplus)
00202 }
00203 #endif
00204 
00205 struct _args {
00206   mrb_bool verbose      : 1;
00207   int argc;
00208   char** argv;
00209 };
00210 
00211 static void
00212 usage(const char *name)
00213 {
00214   static const char *const usage_msg[] = {
00215   "switches:",
00216   "-v           print version number, then run in verbose mode",
00217   "--verbose    run in verbose mode",
00218   "--version    print the version",
00219   "--copyright  print the copyright",
00220   NULL
00221   };
00222   const char *const *p = usage_msg;
00223 
00224   pc.printf("Usage: %s [switches]\n", name);
00225   while (*p)
00226     pc.printf("  %s\n", *p++);
00227 }
00228 
00229 static int
00230 parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args)
00231 {
00232   static const struct _args args_zero = { 0 };
00233 
00234   *args = args_zero;
00235 
00236   for (argc--,argv++; argc > 0; argc--,argv++) {
00237     char *item;
00238     if (argv[0][0] != '-') break;
00239 
00240     item = argv[0] + 1;
00241     switch (*item++) {
00242     case 'v':
00243       if (!args->verbose) mrb_show_version(mrb);
00244       args->verbose = TRUE;
00245       break;
00246     case '-':
00247       if (strcmp((*argv) + 2, "version") == 0) {
00248         mrb_show_version(mrb);
00249         exit(EXIT_SUCCESS);
00250       }
00251       else if (strcmp((*argv) + 2, "verbose") == 0) {
00252         args->verbose = TRUE;
00253         break;
00254       }
00255       else if (strcmp((*argv) + 2, "copyright") == 0) {
00256         mrb_show_copyright(mrb);
00257         exit(EXIT_SUCCESS);
00258       }
00259     default:
00260       return EXIT_FAILURE;
00261     }
00262   }
00263   return EXIT_SUCCESS;
00264 }
00265 
00266 static void
00267 cleanup(mrb_state *mrb, struct _args *args)
00268 {
00269   mrb_close(mrb);
00270 }
00271 
00272 /* Print a short remark for the user */
00273 static void
00274 print_hint(void)
00275 {
00276   pc.printf("mirb - Embeddable Interactive Ruby Shell\n\n");
00277 }
00278 
00279 #ifndef ENABLE_READLINE
00280 /* Print the command line prompt of the REPL */
00281 static void
00282 print_cmdline(int code_block_open)
00283 {
00284   if (code_block_open) {
00285     pc.printf("* ");
00286   }
00287   else {
00288     pc.printf("> ");
00289   }
00290 }
00291 #endif
00292 
00293 #if defined(__cplusplus)
00294 extern "C" {
00295 #endif
00296 void mrb_codedump_all(mrb_state*, struct RProc*);
00297 #if defined(__cplusplus)
00298 }
00299 #endif
00300 
00301 static int
00302 check_keyword(const char *buf, const char *word)
00303 {
00304   const char *p = buf;
00305   size_t len = strlen(word);
00306 
00307   /* skip preceding spaces */
00308   while (*p && isspace((unsigned char)*p)) {
00309     p++;
00310   }
00311   /* check keyword */
00312   if (strncmp(p, word, len) != 0) {
00313     return 0;
00314   }
00315   p += len;
00316   /* skip trailing spaces */
00317   while (*p) {
00318     if (!isspace((unsigned char)*p)) return 0;
00319     p++;
00320   }
00321   return 1;
00322 }
00323 
00324 char *mirb_argv[] = {
00325     "mirb"
00326 };
00327 
00328 int
00329 main(void)
00330 {
00331   int argc = 1;
00332   char **argv = mirb_argv;
00333   char ruby_code[1024] = { 0 };
00334   char last_code_line[1024] = { 0 };
00335 #ifndef ENABLE_READLINE
00336   int last_char;
00337   int char_index;
00338 #else
00339   char *history_path;
00340 #endif
00341   mrbc_context *cxt;
00342   struct mrb_parser_state *parser;
00343   mrb_state *mrb;
00344   mrb_value result;
00345   struct _args args;
00346   int n;
00347   mrb_bool code_block_open = FALSE;
00348   int ai;
00349   unsigned int stack_keep = 0;
00350 
00351   /* new interpreter instance */
00352   mrb = mrb_open();
00353   if (mrb == NULL) {
00354     pc.printf("Invalid mrb interpreter, exiting mirb\n");
00355     return EXIT_FAILURE;
00356   }
00357   mrb_define_global_const(mrb, "ARGV", mrb_ary_new_capa(mrb, 0));
00358 
00359   n = parse_args(mrb, argc, argv, &args);
00360   if (n == EXIT_FAILURE) {
00361     cleanup(mrb, &args);
00362     usage(argv[0]);
00363     return n;
00364   }
00365 
00366 #ifdef ENABLE_READLINE
00367   history_path = get_history_path(mrb);
00368   if (history_path == NULL) {
00369     pc.printf("failed to get history path\n");
00370     mrb_close(mrb);
00371     return EXIT_FAILURE;
00372   }
00373 
00374   MIRB_USING_HISTORY();
00375   MIRB_READ_HISTORY(history_path);
00376 #endif
00377 
00378   print_hint();
00379 
00380   cxt = mrbc_context_new(mrb);
00381   cxt->capture_errors = TRUE;
00382   cxt->lineno = 1;
00383   mrbc_filename(mrb, cxt, "(mirb)");
00384   if (args.verbose) cxt->dump_result = TRUE;
00385 
00386   ai = mrb_gc_arena_save(mrb);
00387 
00388   while (TRUE) {
00389 #ifndef ENABLE_READLINE
00390     print_cmdline(code_block_open);
00391 
00392     char_index = 0;
00393     while ((last_char = pc.getc()) != '\n') {
00394       pc.printf("%c", last_char);
00395       if (last_char == EOF) break;
00396       if (char_index > sizeof(last_code_line)-2) {
00397         pc.printf("input string too long\n");
00398         continue;
00399       }
00400       if (last_char == '\x08') {
00401         if (char_index > 0) char_index--;
00402         continue;
00403       } else {
00404         last_code_line[char_index++] = last_char;
00405       }
00406     }
00407     pc.printf("\n");
00408     
00409     if (last_char == EOF) {
00410       pc.printf("\n");
00411       break;
00412     }
00413 
00414     last_code_line[char_index++] = '\n';
00415     last_code_line[char_index] = '\0';
00416 #else
00417     char* line = MIRB_READLINE(code_block_open ? "* " : "> ");
00418     if (line == NULL) {
00419       printf("\n");
00420       break;
00421     }
00422     if (strlen(line) > sizeof(last_code_line)-2) {
00423       pc.printf("input string too long\n");
00424       continue;
00425     }
00426     strcpy(last_code_line, line);
00427     strcat(last_code_line, "\n");
00428     MIRB_ADD_HISTORY(line);
00429     free(line);
00430 #endif
00431 
00432     if (code_block_open) {
00433       if (strlen(ruby_code)+strlen(last_code_line) > sizeof(ruby_code)-1) {
00434         pc.printf("concatenated input string too long\n");
00435         continue;
00436       }
00437       strcat(ruby_code, last_code_line);
00438     }
00439     else {
00440       if (check_keyword(last_code_line, "quit") || check_keyword(last_code_line, "exit")) {
00441         break;
00442       }
00443       strcpy(ruby_code, last_code_line);
00444     }
00445 
00446     /* parse code */
00447     parser = mrb_parser_new(mrb);
00448     if (parser == NULL) {
00449       pc.printf("create parser state error\n");
00450       break;
00451     }
00452     parser->s = ruby_code;
00453     parser->send = ruby_code + strlen(ruby_code);
00454     parser->lineno = cxt->lineno;
00455     mrb_parser_parse(parser, cxt);
00456     code_block_open = is_code_block_open(parser);
00457 
00458     if (code_block_open) {
00459       /* no evaluation of code */
00460     }
00461     else {
00462       if (0 < parser->nerr) {
00463         /* syntax error */
00464         pc.printf("line %d: %s\n", parser->error_buffer[0].lineno, parser->error_buffer[0].message);
00465       }
00466       else {
00467         /* generate bytecode */
00468         struct RProc *proc = mrb_generate_code(mrb, parser);
00469         if (proc == NULL) {
00470           pc.printf("codegen error\n");
00471           mrb_parser_free(parser);
00472           break;
00473         }
00474 
00475         if (args.verbose) {
00476           mrb_codedump_all(mrb, proc);
00477         }
00478         /* pass a proc for evaulation */
00479         /* evaluate the bytecode */
00480         result = mrb_context_run(mrb,
00481             proc,
00482             mrb_top_self(mrb),
00483             stack_keep);
00484         stack_keep = proc->body.irep->nlocals;
00485         /* did an exception occur? */
00486         if (mrb->exc) {
00487           p(mrb, mrb_obj_value(mrb->exc), 0);
00488           mrb->exc = 0;
00489         }
00490         else {
00491           /* no */
00492           if (!mrb_respond_to(mrb, result, mrb_intern_lit(mrb, "inspect"))){
00493             result = mrb_any_to_s(mrb, result);
00494           }
00495           p(mrb, result, 1);
00496         }
00497       }
00498       ruby_code[0] = '\0';
00499       last_code_line[0] = '\0';
00500       mrb_gc_arena_restore(mrb, ai);
00501     }
00502     mrb_parser_free(parser);
00503     cxt->lineno++;
00504   }
00505   
00506 #ifdef ENABLE_READLINE
00507   MIRB_WRITE_HISTORY(history_path);
00508   mrb_free(mrb, history_path);
00509 #endif
00510 
00511   mrbc_context_free(mrb, cxt);
00512   mrb_close(mrb);
00513   
00514   return 0;
00515 }