summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-bin-mirb/tools/mirb/mirb.c
diff options
context:
space:
mode:
Diffstat (limited to 'mrbgems/mruby-bin-mirb/tools/mirb/mirb.c')
-rw-r--r--mrbgems/mruby-bin-mirb/tools/mirb/mirb.c102
1 files changed, 57 insertions, 45 deletions
diff --git a/mrbgems/mruby-bin-mirb/tools/mirb/mirb.c b/mrbgems/mruby-bin-mirb/tools/mirb/mirb.c
index 50556e092..320bc30fb 100644
--- a/mrbgems/mruby-bin-mirb/tools/mirb/mirb.c
+++ b/mrbgems/mruby-bin-mirb/tools/mirb/mirb.c
@@ -8,26 +8,36 @@
#include <stdlib.h>
#include <string.h>
-
-#include <mruby.h>
+#include "mruby.h"
#include "mruby/array.h"
-#include <mruby/proc.h>
-#include <mruby/data.h>
-#include <mruby/compile.h>
+#include "mruby/proc.h"
+#include "mruby/compile.h"
+#include "mruby/string.h"
+
#ifdef ENABLE_READLINE
-#include <limits.h>
#include <readline/readline.h>
#include <readline/history.h>
+#define MIRB_ADD_HISTORY(line) add_history(line)
+#define MIRB_READLINE(ch) readline(ch)
+#define MIRB_WRITE_HISTORY(path) write_history(path)
+#define MIRB_READ_HISTORY(path) read_history(path)
+#define MIRB_USING_HISTORY() using_history()
+#elif ENABLE_LINENOISE
+#define ENABLE_READLINE
+#include <linenoise.h>
+#define MIRB_ADD_HISTORY(line) linenoiseHistoryAdd(line)
+#define MIRB_READLINE(ch) linenoise(ch)
+#define MIRB_WRITE_HISTORY(path) linenoiseHistorySave(path)
+#define MIRB_READ_HISTORY(path) linenoiseHistoryLoad(history_path)
+#define MIRB_USING_HISTORY()
#endif
-#include <mruby/string.h>
-
-
+
#ifdef ENABLE_READLINE
+#include <limits.h>
static const char *history_file_name = ".mirb_history";
char history_path[PATH_MAX];
#endif
-
static void
p(mrb_state *mrb, mrb_value obj, int prompt)
{
@@ -46,10 +56,10 @@ p(mrb_state *mrb, mrb_value obj, int prompt)
/* Guess if the user might want to enter more
* or if he wants an evaluation of his code now */
-mrb_bool
+static mrb_bool
is_code_block_open(struct mrb_parser_state *parser)
{
- int code_block_open = FALSE;
+ mrb_bool code_block_open = FALSE;
/* check for heredoc */
if (parser->parsing_heredoc != NULL) return TRUE;
@@ -58,6 +68,9 @@ is_code_block_open(struct mrb_parser_state *parser)
return FALSE;
}
+ /* check for unterminated string */
+ if (parser->lex_strterm) return TRUE;
+
/* check if parser error are available */
if (0 < parser->nerr) {
const char *unexpected_end = "syntax error, unexpected $end";
@@ -80,9 +93,6 @@ is_code_block_open(struct mrb_parser_state *parser)
return code_block_open;
}
- /* check for unterminated string */
- if (parser->lex_strterm) return TRUE;
-
switch (parser->lstate) {
/* all states which need more code */
@@ -219,13 +229,12 @@ cleanup(mrb_state *mrb, struct _args *args)
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");
+ printf("mirb - Embeddable Interactive Ruby Shell\n\n");
}
+#ifndef ENABLE_READLINE
/* Print the command line prompt of the REPL */
-void
+static void
print_cmdline(int code_block_open)
{
if (code_block_open) {
@@ -235,6 +244,9 @@ print_cmdline(int code_block_open)
printf("> ");
}
}
+#endif
+
+void mrb_codedump_all(mrb_state*, struct RProc*);
int
main(int argc, char **argv)
@@ -253,8 +265,9 @@ main(int argc, char **argv)
mrb_value result;
struct _args args;
int n;
- int code_block_open = FALSE;
+ mrb_bool code_block_open = FALSE;
int ai;
+ unsigned int stack_keep = 0;
/* new interpreter instance */
mrb = mrb_open();
@@ -275,12 +288,14 @@ main(int argc, char **argv)
cxt = mrbc_context_new(mrb);
cxt->capture_errors = 1;
+ cxt->lineno = 1;
+ mrbc_filename(mrb, cxt, "(mirb)");
if (args.verbose) cxt->dump_result = 1;
ai = mrb_gc_arena_save(mrb);
#ifdef ENABLE_READLINE
- using_history();
+ MIRB_USING_HISTORY();
home = getenv("HOME");
#ifdef _WIN32
if (!home)
@@ -290,7 +305,7 @@ main(int argc, char **argv)
strcpy(history_path, home);
strcat(history_path, "/");
strcat(history_path, history_file_name);
- read_history(history_path);
+ MIRB_READ_HISTORY(history_path);
}
#endif
@@ -311,41 +326,32 @@ main(int argc, char **argv)
last_code_line[char_index] = '\0';
#else
- char* line = readline(code_block_open ? "* " : "> ");
+ char* line = MIRB_READLINE(code_block_open ? "* " : "> ");
if (line == NULL) {
printf("\n");
break;
}
strncpy(last_code_line, line, sizeof(last_code_line)-1);
- add_history(line);
+ MIRB_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 */
+ if (code_block_open) {
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);
+ if ((strcmp(last_code_line, "quit") == 0) || (strcmp(last_code_line, "exit") == 0)) {
+ break;
}
+ 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;
+ parser->lineno = cxt->lineno;
mrb_parser_parse(parser, cxt);
code_block_open = is_code_block_open(parser);
@@ -359,13 +365,18 @@ main(int argc, char **argv)
}
else {
/* generate bytecode */
- n = mrb_generate_code(mrb, parser);
+ struct RProc *proc = mrb_generate_code(mrb, parser);
+ if (args.verbose) {
+ mrb_codedump_all(mrb, proc);
+ }
+ /* pass a proc for evaulation */
/* evaluate the bytecode */
- result = mrb_run(mrb,
- /* pass a proc for evaulation */
- mrb_proc_new(mrb, mrb->irep[n]),
- mrb_top_self(mrb));
+ result = mrb_context_run(mrb,
+ proc,
+ mrb_top_self(mrb),
+ stack_keep);
+ stack_keep = proc->body.irep->nlocals;
/* did an exception occur? */
if (mrb->exc) {
p(mrb, mrb_obj_value(mrb->exc), 0);
@@ -373,7 +384,7 @@ main(int argc, char **argv)
}
else {
/* no */
- if (!mrb_respond_to(mrb, result, mrb_intern2(mrb, "inspect", 7))){
+ if (!mrb_respond_to(mrb, result, mrb_intern_lit(mrb, "inspect"))){
result = mrb_any_to_s(mrb,result);
}
p(mrb, result, 1);
@@ -384,12 +395,13 @@ main(int argc, char **argv)
mrb_gc_arena_restore(mrb, ai);
}
mrb_parser_free(parser);
+ cxt->lineno++;
}
mrbc_context_free(mrb, cxt);
mrb_close(mrb);
#ifdef ENABLE_READLINE
- write_history(history_path);
+ MIRB_WRITE_HISTORY(history_path);
#endif
return 0;