diff --git a/src/io_lib.cpp b/src/io_lib.cpp index 848ba13..5af223a 100644 --- a/src/io_lib.cpp +++ b/src/io_lib.cpp @@ -5,9 +5,6 @@ namespace nasal { const auto file_type_name = "file"; void filehandle_destructor(void* ptr) { - if (static_cast(ptr)==stdin) { - return; - } fclose(static_cast(ptr)); } @@ -207,6 +204,25 @@ var builtin_eof(context* ctx, gc* ngc) { )); } +var builtin_stdin(context* ctx, gc* ngc) { + auto file_descriptor = ngc->alloc(vm_obj); + file_descriptor.ghost().set(file_type_name, nullptr, stdin); + return file_descriptor; +} + +var builtin_stdout(context* ctx, gc* ngc) { + auto file_descriptor = ngc->alloc(vm_obj); + file_descriptor.ghost().set(file_type_name, nullptr, stdout); + return file_descriptor; +} + +var builtin_stderr(context* ctx, gc* ngc) { + auto file_descriptor = ngc->alloc(vm_obj); + file_descriptor.ghost().set(file_type_name, nullptr, stderr); + return file_descriptor; +} + + nasal_builtin_table io_lib_native[] = { {"__readfile", builtin_readfile}, {"__fout", builtin_fout}, @@ -220,6 +236,9 @@ nasal_builtin_table io_lib_native[] = { {"__readln", builtin_readln}, {"__stat", builtin_stat}, {"__eof", builtin_eof}, + {"__stdin", builtin_stdin}, + {"__stdout", builtin_stdout}, + {"__stderr", builtin_stderr}, {nullptr, nullptr} }; diff --git a/src/io_lib.h b/src/io_lib.h index a4dc136..e1f4e97 100644 --- a/src/io_lib.h +++ b/src/io_lib.h @@ -32,6 +32,9 @@ var builtin_tell(context*, gc*); var builtin_readln(context*, gc*); var builtin_stat(context*, gc*); var builtin_eof(context*, gc*); +var builtin_stdin(context*, gc*); +var builtin_stdout(context*, gc*); +var builtin_stderr(context*, gc*); extern nasal_builtin_table io_lib_native[]; diff --git a/src/nasal_codegen.cpp b/src/nasal_codegen.cpp index bbb7971..04443e6 100644 --- a/src/nasal_codegen.cpp +++ b/src/nasal_codegen.cpp @@ -1168,7 +1168,7 @@ void codegen::block_gen(code_block* node) { switch(tmp->get_type()) { case expr_type::ast_null: break; case expr_type::ast_id: - if (need_repl_output) { + if (need_repl_output && local.empty()) { repl_mode_info_output_gen(tmp); } else { check_id_exist((identifier*)tmp); @@ -1178,7 +1178,7 @@ void codegen::block_gen(code_block* node) { case expr_type::ast_num: case expr_type::ast_str: case expr_type::ast_bool: - if (need_repl_output) { + if (need_repl_output && local.empty()) { repl_mode_info_output_gen(tmp); } break; diff --git a/std/io.nas b/std/io.nas index cfca4ad..d2a950a 100644 --- a/std/io.nas +++ b/std/io.nas @@ -66,3 +66,9 @@ var stat = func(filename) { var eof = func(filehandle) { return __eof(filehandle); } + +var stdin = func() { return __stdin; }(); + +var stdout = func() { return __stdout;}(); + +var stderr = func() { return __stderr; }();