1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
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/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 unterminated string */
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) {
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;
}
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);
}
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 */
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;
}
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;
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;
}
strncat(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_parser_free(parser);
mrb_gc_arena_restore(mrb, ai);
}
}
mrbc_context_free(mrb, cxt);
mrb_close(mrb);
return 0;
}
|