diff --git a/include/picrin.h b/include/picrin.h index 61cfbb69..8c7cb34f 100644 --- a/include/picrin.h +++ b/include/picrin.h @@ -71,6 +71,12 @@ typedef struct { 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_realloc(pic_state *, void *, size_t); void *pic_calloc(pic_state *, unsigned, size_t); diff --git a/src/init.c b/src/init.c index 24b83265..811a0043 100644 --- a/src/init.c +++ b/src/init.c @@ -33,7 +33,7 @@ pic_load_stdlib(pic_state *pic) } n = pic_parse_file(pic, file, &vs); - if (n <= 0) { + if (n < 0) { fputs("fatal error: built-in.scm broken", stderr); abort(); } diff --git a/src/parse.y b/src/parse.y index 3280ab6b..94adcac9 100644 --- a/src/parse.y +++ b/src/parse.y @@ -67,6 +67,10 @@ program p->incomp = true; p->value = pic_undef_value(); } +| /* empty line */ + { + p->value = pic_nil_value(); + } ; program_data @@ -249,17 +253,17 @@ pic_parse_file(pic_state *pic, FILE *file, pic_value *v) if (p.yynerrs > 0) { p.value = pic_undef_value(); - return -1; + return PIC_PARSER_ERROR; } if (p.incomp) { - return 0; + return PIC_PARSER_INCOMPLETE; } *v = 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) { struct parser_control p; @@ -274,10 +278,10 @@ pic_parse_cstr(pic_state *pic, const char *str, pic_value *v) yylex_destroy(p.yyscanner); if (p.yynerrs > 0) { - return -1; + return PIC_PARSER_ERROR; } if (p.incomp) { - return 0; + return PIC_PARSER_INCOMPLETE; } *v = p.value; diff --git a/tools/main.c b/tools/main.c index 81f2c46f..ff6a5545 100644 --- a/tools/main.c +++ b/tools/main.c @@ -103,11 +103,11 @@ repl(pic_state *pic) /* read */ 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; } code[0] = '\0'; - if (n == -1) { /* parse error */ + if (n == PIC_PARSER_ERROR) { /* parse error */ goto next; }