diff options
Diffstat (limited to 'src/interpreter.c')
-rw-r--r-- | src/interpreter.c | 46 |
1 files changed, 44 insertions, 2 deletions
diff --git a/src/interpreter.c b/src/interpreter.c index 6b1ebe1..27626a8 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -3,11 +3,20 @@ #include <stdio.h> #include <string.h> -static void context_set(hashmap_t context, const char *name, int val) { +typedef void *YY_BUFFER_STATE; +extern FILE *yyin; +extern ASTNode *ast_root; +extern int yyparse(void); +extern void yyrestart (FILE *); +extern YY_BUFFER_STATE yy_scan_string(const char *); +extern void yy_delete_buffer(YY_BUFFER_STATE); + + +void context_set(hashmap_t context, const char *name, int val) { hashmap_insert(context, name, val); } -static int context_get(hashmap_t context, const char *name) { +int context_get(hashmap_t context, const char *name) { int *val = hashmap_get(context, name); if (val) return *val; return 0; @@ -111,3 +120,36 @@ void exec_stmt(hashmap_t context, ASTNode *node) { } } } + +int exec_file (hashmap_t context, const char *path) { + yyin = fopen(path, "r"); + if (!yyin) { + perror(path); + return -1; + } + yyrestart(yyin); + if (!yyparse()) { + exec_stmt(context, ast_root); + ast_free(ast_root); + } else { + fprintf(stderr, "Parse error in %s\n", path); + fclose(yyin); + return -1; + } + fclose(yyin); + return 0; +} + +int exec_str (hashmap_t context, const char *str) { + YY_BUFFER_STATE buf = yy_scan_string(str); + if (!yyparse()) { + exec_stmt(context, ast_root); + ast_free(ast_root); + } else { + fprintf(stderr, "Parse error\n"); + yy_delete_buffer(buf); + return -1; + } + yy_delete_buffer(buf); + return 0; +} |