1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-28 02:59:54 +00:00

Add stdi and stdout to file io.

This commit is contained in:
Calvin Rose 2017-04-30 11:15:44 -04:00
parent 88138a16b3
commit 78cdf4b184
4 changed files with 34 additions and 1 deletions

View File

@ -38,6 +38,7 @@ int debug_compile_and_run(Gst *vm, GstValue ast, GstValue env) {
/* Try to compile generated AST */ /* Try to compile generated AST */
gst_compiler(&c, vm); gst_compiler(&c, vm);
gst_compiler_usemodule(&c, "std"); gst_compiler_usemodule(&c, "std");
gst_compiler_usemodule(&c, "std.io");
gst_compiler_globals(&c, env); gst_compiler_globals(&c, env);
func = gst_wrap_function(gst_compiler_compile(&c, ast)); func = gst_wrap_function(gst_compiler_compile(&c, ast));
/* Check for compilation errors */ /* Check for compilation errors */

View File

@ -28,6 +28,5 @@
void gst_cache_remove_string(Gst *vm, char *strmem); void gst_cache_remove_string(Gst *vm, char *strmem);
void gst_cache_remove_tuple(Gst *vm, char *tuplemem); void gst_cache_remove_tuple(Gst *vm, char *tuplemem);
void gst_cache_remove_struct(Gst *vm, char *structmem); void gst_cache_remove_struct(Gst *vm, char *structmem);
void gst_cache_remove_userdata(Gst *vm, char *usermem);
#endif /* end of include guard: CACHE_H_LVYZMBLR */ #endif /* end of include guard: CACHE_H_LVYZMBLR */

View File

@ -601,6 +601,30 @@ int gst_stl_close(Gst *vm) {
gst_c_return(vm, gst_wrap_nil()); gst_c_return(vm, gst_wrap_nil());
} }
/* Functions in the io module */
static const GstModuleItem const io_dat[] = {
{"open", gst_stl_open},
{"slurp", gst_stl_slurp},
{"read", gst_stl_read},
{"write", gst_stl_write},
{NULL, NULL}
};
/* Load the io module */
void gst_stlio_load(Gst *vm) {
/* Load the normal c functions */
GstValue module = gst_cmodule_table(vm, io_dat);
GstTable *tab = module.data.table;
/* Wrap stdin and stdout */
FILE **inp = gst_userdata(vm, sizeof(FILE *), &gst_stl_filetype);
FILE **outp = gst_userdata(vm, sizeof(FILE *), &gst_stl_filetype);
*inp = stdin;
*outp = stdout;
gst_table_put(vm, tab, gst_string_cv(vm, "stdin"), gst_wrap_userdata(inp));
gst_table_put(vm, tab, gst_string_cv(vm, "stdout"), gst_wrap_userdata(outp));
gst_module_put(vm, "std.io", module);
}
/****/ /****/
/* Temporary */ /* Temporary */
/****/ /****/
@ -674,5 +698,6 @@ static const GstModuleItem const std_module[] = {
/* Load all libraries */ /* Load all libraries */
void gst_stl_load(Gst *vm) { void gst_stl_load(Gst *vm) {
gst_stlio_load(vm);
gst_module_put(vm, "std", gst_cmodule_struct(vm, std_module)); gst_module_put(vm, "std", gst_cmodule_struct(vm, std_module));
} }

8
libs/parse.gst Normal file
View File

@ -0,0 +1,8 @@
# Make parser
(: parser (fn [in] {
'in in
'line 0
'index 0
'states []
}))