add stdin, stdout, stderr in io module

This commit is contained in:
ValKmjolnir 2023-10-30 23:20:49 +08:00
parent 4c8e1dfe91
commit c35ad2e6fa
4 changed files with 33 additions and 5 deletions

View File

@ -5,9 +5,6 @@ namespace nasal {
const auto file_type_name = "file";
void filehandle_destructor(void* ptr) {
if (static_cast<FILE*>(ptr)==stdin) {
return;
}
fclose(static_cast<FILE*>(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}
};

View File

@ -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[];

View File

@ -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;

View File

@ -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; }();