accept empty lines in repl

This commit is contained in:
Yuichi Nishiwaki 2013-11-13 17:37:05 +09:00
parent d6c377a33b
commit 223e873ff0
4 changed files with 18 additions and 8 deletions

View File

@ -71,6 +71,12 @@ typedef struct {
typedef pic_value (*pic_func_t)(pic_state *); typedef pic_value (*pic_func_t)(pic_state *);
enum pic_parser_res {
PIC_PARSER_INCOMPLETE = -1,
PIC_PARSER_ERROR = -2
/* if parser is successfully done, return the number of exprs (>= 0) */
};
void *pic_alloc(pic_state *, size_t); void *pic_alloc(pic_state *, size_t);
void *pic_realloc(pic_state *, void *, size_t); void *pic_realloc(pic_state *, void *, size_t);
void *pic_calloc(pic_state *, unsigned, size_t); void *pic_calloc(pic_state *, unsigned, size_t);

View File

@ -33,7 +33,7 @@ pic_load_stdlib(pic_state *pic)
} }
n = pic_parse_file(pic, file, &vs); n = pic_parse_file(pic, file, &vs);
if (n <= 0) { if (n < 0) {
fputs("fatal error: built-in.scm broken", stderr); fputs("fatal error: built-in.scm broken", stderr);
abort(); abort();
} }

View File

@ -67,6 +67,10 @@ program
p->incomp = true; p->incomp = true;
p->value = pic_undef_value(); p->value = pic_undef_value();
} }
| /* empty line */
{
p->value = pic_nil_value();
}
; ;
program_data program_data
@ -249,17 +253,17 @@ pic_parse_file(pic_state *pic, FILE *file, pic_value *v)
if (p.yynerrs > 0) { if (p.yynerrs > 0) {
p.value = pic_undef_value(); p.value = pic_undef_value();
return -1; return PIC_PARSER_ERROR;
} }
if (p.incomp) { if (p.incomp) {
return 0; return PIC_PARSER_INCOMPLETE;
} }
*v = p.value; *v = p.value;
return pic_length(pic, p.value); return pic_length(pic, p.value);
} }
int enum pic_parser_res
pic_parse_cstr(pic_state *pic, const char *str, pic_value *v) pic_parse_cstr(pic_state *pic, const char *str, pic_value *v)
{ {
struct parser_control p; struct parser_control p;
@ -274,10 +278,10 @@ pic_parse_cstr(pic_state *pic, const char *str, pic_value *v)
yylex_destroy(p.yyscanner); yylex_destroy(p.yyscanner);
if (p.yynerrs > 0) { if (p.yynerrs > 0) {
return -1; return PIC_PARSER_ERROR;
} }
if (p.incomp) { if (p.incomp) {
return 0; return PIC_PARSER_INCOMPLETE;
} }
*v = p.value; *v = p.value;

View File

@ -103,11 +103,11 @@ repl(pic_state *pic)
/* read */ /* read */
n = pic_parse_cstr(pic, code, &vs); n = pic_parse_cstr(pic, code, &vs);
if (n == 0) { /* wait for more input */ if (n == PIC_PARSER_INCOMPLETE) { /* wait for more input */
goto next; goto next;
} }
code[0] = '\0'; code[0] = '\0';
if (n == -1) { /* parse error */ if (n == PIC_PARSER_ERROR) { /* parse error */
goto next; goto next;
} }