summaryrefslogtreecommitdiffhomepage
path: root/tools
diff options
context:
space:
mode:
Diffstat (limited to 'tools')
-rw-r--r--tools/mirb/mirb.c127
-rw-r--r--tools/mrbc/mrbc.c33
-rw-r--r--tools/mruby/mruby.c39
3 files changed, 144 insertions, 55 deletions
diff --git a/tools/mirb/mirb.c b/tools/mirb/mirb.c
index 29685cf28..3f96696a7 100644
--- a/tools/mirb/mirb.c
+++ b/tools/mirb/mirb.c
@@ -6,6 +6,7 @@
** immediately. It's a REPL...
*/
+#include <stdlib.h>
#include <string.h>
#include <mruby.h>
@@ -38,7 +39,14 @@ is_code_block_open(struct mrb_parser_state *parser)
int code_block_open = FALSE;
/* check for unterminated string */
- if (parser->sterm) return TRUE;
+ if (parser->lex_strterm) return TRUE;
+
+ /* check for heredoc */
+ if (parser->heredoc_starts_nextline) return TRUE;
+ if (parser->heredoc_end_now) {
+ parser->heredoc_end_now = FALSE;
+ return FALSE;
+ }
/* check if parser error are available */
if (0 < parser->nerr) {
@@ -124,8 +132,70 @@ is_code_block_open(struct mrb_parser_state *parser)
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(0);
+ }
+ else if (strcmp((*argv) + 2, "copyright") == 0) {
+ mrb_show_copyright(mrb);
+ exit(0);
+ }
+ else return -3;
+ default:
+ return -4;
+ }
+ }
+
+ return 0;
+}
+
+static void
+cleanup(mrb_state *mrb, struct _args *args)
+{
+ mrb_close(mrb);
+}
+
/* Print a short remark for the user */
-void print_hint(void)
+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");
@@ -145,7 +215,7 @@ print_cmdline(int code_block_open)
}
int
-main(void)
+main(int argc, char **argv)
{
char ruby_code[1024] = { 0 };
char last_code_line[1024] = { 0 };
@@ -157,19 +227,27 @@ main(void)
struct mrb_parser_state *parser;
mrb_state *mrb;
mrb_value result;
+ struct _args args;
int n;
int code_block_open = FALSE;
int ai;
- print_hint();
-
/* new interpreter instance */
mrb = mrb_open();
if (mrb == NULL) {
- fprintf(stderr, "Invalid mrb interpreter, exiting mirb");
+ fputs("Invalid mrb interpreter, exiting mirb\n", stderr);
return EXIT_FAILURE;
}
+ n = parse_args(mrb, argc, argv, &args);
+ if (n < 0) {
+ cleanup(mrb, &args);
+ usage(argv[0]);
+ return n;
+ }
+
+ print_hint();
+
cxt = mrbc_context_new(mrb);
cxt->capture_errors = 1;
@@ -184,7 +262,7 @@ main(void)
last_code_line[char_index++] = last_char;
}
if (last_char == EOF) {
- printf("\n");
+ fputs("\n", stdout);
break;
}
@@ -233,28 +311,31 @@ main(void)
}
else {
if (0 < parser->nerr) {
- /* syntax error */
- printf("line %d: %s\n", parser->error_buffer[0].lineno, parser->error_buffer[0].message);
+ /* 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);
+ /* generate bytecode */
+ n = mrb_generate_code(mrb, parser);
- /* evaluate the bytecode */
- result = mrb_run(mrb,
+ /* 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(" => ");
- p(mrb, result);
- }
+ /* 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';
diff --git a/tools/mrbc/mrbc.c b/tools/mrbc/mrbc.c
index bc551fffc..b8c1cf286 100644
--- a/tools/mrbc/mrbc.c
+++ b/tools/mrbc/mrbc.c
@@ -1,10 +1,10 @@
-#include "mruby.h"
-#include "mruby/proc.h"
-#include "mruby/dump.h"
-#include "mruby/compile.h"
#include <stdio.h>
-#include <string.h>
#include <stdlib.h>
+#include <string.h>
+#include "mruby.h"
+#include "mruby/compile.h"
+#include "mruby/dump.h"
+#include "mruby/proc.h"
#define RITEBIN_EXT ".mrb"
#define C_EXT ".c"
@@ -19,8 +19,9 @@ struct _args {
char *filename;
char *initname;
char *ext;
- int check_syntax : 1;
- int verbose : 1;
+ mrb_bool check_syntax : 1;
+ mrb_bool verbose : 1;
+ mrb_bool debug_info : 1;
};
static void
@@ -31,6 +32,7 @@ usage(const char *name)
"-c check syntax only",
"-o<outfile> place the output into <outfile>",
"-v print version number, then trun on verbose mode",
+ "-g produce debugging information",
"-B<symbol> binary <symbol> output in C language format",
"--verbose run at verbose mode",
"--version print the version",
@@ -83,6 +85,12 @@ parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args)
switch ((*argv)[1]) {
case 'o':
+ if (outfile) {
+ printf("%s: An output file is already specified. (%s)\n",
+ *origargv, outfile);
+ result = -5;
+ goto exit;
+ }
outfile = get_outfilename((*argv) + 2, "");
break;
case 'B':
@@ -101,6 +109,9 @@ parse_args(mrb_state *mrb, int argc, char **argv, struct _args *args)
mrb_show_version(mrb);
args->verbose = 1;
break;
+ case 'g':
+ args->debug_info = 1;
+ break;
case '-':
if (strcmp((*argv) + 2, "version") == 0) {
mrb_show_version(mrb);
@@ -176,7 +187,7 @@ main(int argc, char **argv)
mrb_value result;
if (mrb == NULL) {
- fprintf(stderr, "Invalid mrb_state, exiting mrbc");
+ fputs("Invalid mrb_state, exiting mrbc\n", stderr);
return EXIT_FAILURE;
}
@@ -198,15 +209,15 @@ main(int argc, char **argv)
return EXIT_FAILURE;
}
if (args.check_syntax) {
- printf("Syntax OK\n");
+ puts("Syntax OK");
cleanup(mrb, &args);
return EXIT_SUCCESS;
}
if (args.initname) {
- n = mrb_bdump_irep(mrb, n, args.wfp, args.initname);
+ n = mrb_dump_irep_cfunc(mrb, n, args.debug_info, args.wfp, args.initname);
}
else {
- n = mrb_dump_irep(mrb, n, args.wfp);
+ n = mrb_dump_irep_binary(mrb, n, args.debug_info, args.wfp);
}
cleanup(mrb, &args);
diff --git a/tools/mruby/mruby.c b/tools/mruby/mruby.c
index 9d6182238..47727e67b 100644
--- a/tools/mruby/mruby.c
+++ b/tools/mruby/mruby.c
@@ -6,6 +6,7 @@
#include "mruby/dump.h"
#include "mruby/variable.h"
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#ifndef ENABLE_STDIO
@@ -26,10 +27,10 @@ void mrb_show_copyright(mrb_state *);
struct _args {
FILE *rfp;
char* cmdline;
- int fname : 1;
- int mrbfile : 1;
- int check_syntax : 1;
- int verbose : 1;
+ mrb_bool fname : 1;
+ mrb_bool mrbfile : 1;
+ mrb_bool check_syntax : 1;
+ mrb_bool verbose : 1;
int argc;
char** argv;
};
@@ -52,7 +53,7 @@ usage(const char *name)
printf("Usage: %s [switches] programfile\n", name);
while(*p)
- printf(" %s\n", *p++);
+ printf(" %s\n", *p++);
}
static int
@@ -125,7 +126,6 @@ append_cmdline:
exit(0);
}
else return -3;
- return 0;
default:
return -4;
}
@@ -189,14 +189,14 @@ showcallinfo(mrb_state *mrb)
if (irep->filename != NULL)
filename = irep->filename;
if (irep->lines != NULL) {
- mrb_code *pc;
+ 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 (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];
}
@@ -211,19 +211,16 @@ showcallinfo(mrb_state *mrb)
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);
+ 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);
+ printf("\t[%d] %s:%d:in %s\n", i, filename, line, method);
}
}
else {
- printf("\t[%d] %s:%d\n",
- i, filename, line);
+ printf("\t[%d] %s:%d\n", i, filename, line);
}
}
}
@@ -238,7 +235,7 @@ main(int argc, char **argv)
mrb_value ARGV;
if (mrb == NULL) {
- fprintf(stderr, "Invalid mrb_state, exiting mruby\n");
+ fputs("Invalid mrb_state, exiting mruby\n", stderr);
return EXIT_FAILURE;
}