From 5fa829ea6be5961bfbe5de829a780c5122a78992 Mon Sep 17 00:00:00 2001 From: fennecdjay Date: Wed, 9 Oct 2019 20:31:04 +0200 Subject: [PATCH] :art: Test compile.c --- include/compile.h | 5 +---- src/compile.c | 14 +++++++------- tests/import/compile_file.c | 26 ++++++++++++++++++++++++++ tests/import/compile_string.c | 21 +++++++++++++++++++++ tests/import/get_module.c | 33 +++++++++++++++++++++++++++++++++ tests/import/rm_me.gw | 1 + 6 files changed, 89 insertions(+), 11 deletions(-) create mode 100644 tests/import/compile_file.c create mode 100644 tests/import/compile_string.c create mode 100644 tests/import/get_module.c create mode 100644 tests/import/rm_me.gw diff --git a/include/compile.h b/include/compile.h index 7e864988..acb61b92 100644 --- a/include/compile.h +++ b/include/compile.h @@ -1,9 +1,6 @@ #ifndef __COMPILE #define __COMPILE -//m_bool check_filename(struct Gwion_* vm, const m_str filename); -m_bool check_string(struct Gwion_* vm, const m_str filename, const m_str data); -//m_bool check_file(struct Gwion_* vm, const m_str filename, FILE* file); m_uint compile_filename(struct Gwion_* vm, const m_str filename); m_uint compile_string(struct Gwion_* vm, const m_str filename, const m_str data); -//m_uint compile_file(struct Gwion_* vm, const m_str filename, FILE* file); +m_uint compile_file(struct Gwion_* vm, const m_str filename, FILE* file); #endif diff --git a/src/compile.c b/src/compile.c index e17d5c1f..04615afe 100644 --- a/src/compile.c +++ b/src/compile.c @@ -53,7 +53,7 @@ static inline void compiler_error(MemPool p, const struct Compiler* c) { static void compiler_clean(MemPool p, const struct Compiler* c) { if(c->name) xfree(c->name); - if(c->file) + if(c->type == COMPILE_MSTR && c->file) fclose(c->file); if(c->ast) free_ast(p, c->ast); @@ -64,9 +64,11 @@ static m_bool _compiler_open(struct Compiler* c) { m_str name = c->name; c->name = realpath(name, NULL); xfree(name); - return c->name ? !!(c->file = fopen(c->name, "r")) : -1; - } else if(c->type == COMPILE_MSTR) - return (c->file = fmemopen(c->data, strlen(c->data) + 1, "r")) ? 1 : - 1; + return c->name ? !!(c->file = fopen(c->name, "r")) : GW_ERROR; + } else if(c->type == COMPILE_MSTR) { + c->file = fmemopen(c->data, strlen(c->data), "r"); + return c->file ? GW_OK : GW_ERROR; + } return GW_OK; } @@ -127,9 +129,7 @@ m_uint compile_string(struct Gwion_* vm, const m_str filename, const m_str data) return compile(vm, &c); } -/* m_uint compile_file(struct Gwion_* vm, const m_str filename, FILE* file) { - struct Compiler c = { .base=filename, .type=COMPILE_MSTR, .file=file }; + struct Compiler c = { .base=filename, .type=COMPILE_FILE, .file=file }; return compile(vm, &c); } -*/ diff --git a/tests/import/compile_file.c b/tests/import/compile_file.c new file mode 100644 index 00000000..32e91d27 --- /dev/null +++ b/tests/import/compile_file.c @@ -0,0 +1,26 @@ +#include "gwion_util.h" +#include "gwion_ast.h" +#include "oo.h" +#include "vm.h" +#include "env.h" +#include "type.h" +#include "object.h" +#include "instr.h" +#include "gwion.h" +#include "value.h" +#include "operator.h" +#include "import.h" +#include "gwi.h" +#include "compile.h" + +#define _XOPEN_SOURCE 500 +#include +GWION_IMPORT(compile_string) { + DECL_OB(FILE *,file, = fopen("rm_me.gw", "w+")) + fprintf(file, "1;"); + rewind(file); + const m_bool ret = compile_file(gwi->gwion, __FILE__, file) ? + GW_OK : GW_ERROR; + fclose(file); + return ret; +} diff --git a/tests/import/compile_string.c b/tests/import/compile_string.c new file mode 100644 index 00000000..3d0cb0ee --- /dev/null +++ b/tests/import/compile_string.c @@ -0,0 +1,21 @@ +#include "gwion_util.h" +#include "gwion_ast.h" +#include "oo.h" +#include "vm.h" +#include "env.h" +#include "type.h" +#include "object.h" +#include "instr.h" +#include "gwion.h" +#include "value.h" +#include "operator.h" +#include "import.h" +#include "gwi.h" +#include "compile.h" + +#define _XOPEN_SOURCE 500 +#include +GWION_IMPORT(compile_string) { + return compile_string(gwi->gwion, __FILE__, "1;") ? + GW_OK : GW_ERROR; +} diff --git a/tests/import/get_module.c b/tests/import/get_module.c new file mode 100644 index 00000000..2d58570d --- /dev/null +++ b/tests/import/get_module.c @@ -0,0 +1,33 @@ +#include +#include +#include "gwion_util.h" +#include "gwion_ast.h" +#include "oo.h" +#include "env.h" +#include "vm.h" +#include "type.h" +#include "value.h" +#include "object.h" +#include "driver.h" +#include "gwion.h" +#include "instr.h" +#include "operator.h" +#include "import.h" +#include "gwi.h" +#include "plug.h" + +GWMODSTR(dummy_module); + +GWMODINI(dummy_module) { + puts(__func__); + return NULL; +} +GWMODEND(dummy_module) { + puts(__func__); +} + +GWION_IMPORT(dummy_module) { + GWI_OB(get_module(gwi->gwion, "dummy")) + get_module(gwi->gwion, "non_existant_module"); + return GW_OK; +} diff --git a/tests/import/rm_me.gw b/tests/import/rm_me.gw new file mode 100644 index 00000000..69126156 --- /dev/null +++ b/tests/import/rm_me.gw @@ -0,0 +1 @@ +1; \ No newline at end of file -- 2.43.0