diff options
-rw-r--r-- | dimension/grammar.epilogue | 30 | ||||
-rw-r--r-- | dimension/parse.h | 1 | ||||
-rw-r--r-- | dimension/realize.c | 14 |
3 files changed, 35 insertions, 10 deletions
diff --git a/dimension/grammar.epilogue b/dimension/grammar.epilogue index e8760c7..ecb910a 100644 --- a/dimension/grammar.epilogue +++ b/dimension/grammar.epilogue @@ -47,6 +47,36 @@ dmnsn_parse(FILE *file, dmnsn_symbol_table *symtable) return astree; } +dmnsn_astree * +dmnsn_parse_string(const char *str, dmnsn_symbol_table *symtable) +{ + const char *filename; + dmnsn_astnode *fnode = dmnsn_find_symbol(symtable, "__file__"); + if (fnode && fnode->type == DMNSN_AST_STRING) { + filename = fnode->ptr; + } else { + filename = "<string>"; + dmnsn_declare_symbol(symtable, "__file__", dmnsn_new_ast_string(filename)); + } + + void *scanner; + dmnsn_astree *astree = dmnsn_new_array(sizeof(dmnsn_astnode)); + + dmnsn_yylex_init(&scanner); + dmnsn_yyset_extra(NULL, scanner); + + void *buffer = dmnsn_yy_make_string_buffer(str, scanner); + dmnsn_yy_push_buffer(buffer, scanner); + + if (yyparse(filename, scanner, astree, symtable) != 0) { + dmnsn_delete_astree(astree); + astree = NULL; + } + + dmnsn_yylex_destroy(scanner); + return astree; +} + const char * dmnsn_token_string(dmnsn_token_type token_type) { diff --git a/dimension/parse.h b/dimension/parse.h index b2658f3..9db4981 100644 --- a/dimension/parse.h +++ b/dimension/parse.h @@ -168,6 +168,7 @@ dmnsn_astnode dmnsn_eval_vector(dmnsn_astnode astnode, * The workhorse -- parse a file */ dmnsn_astree *dmnsn_parse(FILE *file, dmnsn_symbol_table *symtable); +dmnsn_astree *dmnsn_parse_string(const char *str, dmnsn_symbol_table *symtable); /* * Parser internals diff --git a/dimension/realize.c b/dimension/realize.c index 31fb021..810d86e 100644 --- a/dimension/realize.c +++ b/dimension/realize.c @@ -17,8 +17,6 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. * *************************************************************************/ -#define _GNU_SOURCE /* For fmemopen */ - #include "realize.h" #include "parse.h" #include "utility.h" @@ -754,18 +752,14 @@ dmnsn_realize_string(const char *str, dmnsn_symbol_table *symtable) if (!symtable) { symtable = dmnsn_new_symbol_table(); } - if (!dmnsn_find_symbol(symtable, "__file__")) { - dmnsn_declare_symbol(symtable, "__file__", - dmnsn_new_ast_string("<string>")); - } - FILE *file = fmemopen((void *)str, strlen(str), "r"); - if (!file) { + dmnsn_astree *astree = dmnsn_parse_string(str, symtable); + if (!astree) { return NULL; } - dmnsn_scene *scene = dmnsn_realize(file, symtable); + dmnsn_scene *scene = dmnsn_realize_astree(astree); - fclose(file); + dmnsn_delete_astree(astree); return scene; } |