From f192c96650a9e6d6a539bd6d5ea63dc2066facb9 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 27 Oct 2013 18:38:41 +0900 Subject: [PATCH] parse-from-file facility --- include/picrin.h | 4 +++- src/parse.y | 27 ++++++++++++++++++++++++++- tools/main.c | 2 +- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/include/picrin.h b/include/picrin.h index 65b12a56..9da11089 100644 --- a/include/picrin.h +++ b/include/picrin.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "picconf.h" #include "picrin/value.h" @@ -76,7 +77,8 @@ pic_value pic_intern_cstr(pic_state *, const char *); pic_value pic_str_new(pic_state *, const char *, size_t); pic_value pic_str_new_cstr(pic_state *, const char *); -bool pic_parse(pic_state *, const char *, pic_value *); +bool pic_parse_file(pic_state *, FILE *file, pic_value *); +bool pic_parse_cstr(pic_state *, const char *, pic_value *); pic_value pic_run(pic_state *, struct pic_proc *, pic_value); struct pic_proc *pic_codegen(pic_state *, pic_value); diff --git a/src/parse.y b/src/parse.y index fdf6247d..b51c175e 100644 --- a/src/parse.y +++ b/src/parse.y @@ -21,6 +21,7 @@ void yyerror(struct parser_control *, const char *); int yylex(); int yylex_(); void yylex_init(); +void yyset_in(); void yy_scan_string(); void yylex_destroy(); %} @@ -175,7 +176,31 @@ yylex(YYSTYPE *yylvalp, struct parser_control *p) } bool -pic_parse(pic_state *pic, const char *str, pic_value *v) +pic_parse_file(pic_state *pic, FILE *file, pic_value *v) +{ + struct parser_control p; + + p.pic = pic; + p.incomp = false; + p.yynerrs = 0; + + yylex_init(&p.yyscanner); + yyset_in(file, p.yyscanner); + yyparse(&p); + yylex_destroy(p.yyscanner); + + if (p.yynerrs > 0) { + p.value = pic_undef_value(); + } + + if (! p.incomp) { + *v = p.value; + } + return ! p.incomp; +} + +bool +pic_parse_cstr(pic_state *pic, const char *str, pic_value *v) { struct parser_control p; diff --git a/tools/main.c b/tools/main.c index 0cbf46ff..fac9c432 100644 --- a/tools/main.c +++ b/tools/main.c @@ -95,7 +95,7 @@ main(int argc, char *argv[], char **envp) strcat(code, line); /* read */ - r = pic_parse(pic, code, &v); + r = pic_parse_cstr(pic, code, &v); if (! r) { /* wait for more input */ goto next; }