From 6ef22d3228d7f85213c8332702169ccbed1314f2 Mon Sep 17 00:00:00 2001 From: ValKmjolnir Date: Fri, 23 Sep 2022 21:45:08 +0800 Subject: [PATCH] :rocket: use `std::ostream` to unify `nas_ref` output --- README.md | 4 +-- doc/README_zh.md | 4 +-- nasal_builtin.h | 21 ++++++------- nasal_gc.h | 78 +++++++++++++++++++++++------------------------- nasal_import.h | 2 ++ 5 files changed, 52 insertions(+), 57 deletions(-) diff --git a/README.md b/README.md index 5153a15..65b2581 100644 --- a/README.md +++ b/README.md @@ -509,8 +509,8 @@ nas_ref builtin_print(nas_ref* local,nasal_gc& gc) case vm_nil: std::cout<<"nil"; break; case vm_num: std::cout<"; break; } diff --git a/doc/README_zh.md b/doc/README_zh.md index aadf3d0..570c846 100644 --- a/doc/README_zh.md +++ b/doc/README_zh.md @@ -489,8 +489,8 @@ nas_ref builtin_print(nas_ref* local,nasal_gc& gc) case vm_nil: std::cout<<"nil"; break; case vm_num: std::cout<"; break; } diff --git a/nasal_builtin.h b/nasal_builtin.h index 1e0f4db..7677432 100644 --- a/nasal_builtin.h +++ b/nasal_builtin.h @@ -8,6 +8,8 @@ #include #else #pragma warning (disable:4566) // i know i'm using utf-8, fuck you +#define _CRT_SECURE_NO_DEPRECATE 1 +#define _CRT_NONSTDC_NO_DEPRECATE 1 #include #include #endif @@ -29,20 +31,17 @@ #define environ (*_NSGetEnviron()) #endif -void print_core(std::vector& elems) -{ - for(auto& i:elems) - i.print(); -} nas_ref builtin_print(nas_ref* local,nasal_gc& gc) { - print_core(local[1].vec().elems); + for(auto& i:local[1].vec().elems) + std::cout<"); - fout<"); #else diff --git a/nasal_gc.h b/nasal_gc.h index 909ae6d..21f1d7a 100644 --- a/nasal_gc.h +++ b/nasal_gc.h @@ -86,7 +86,7 @@ struct nas_ref // number and string can be translated to each other f64 tonum(); string tostr(); - void print(); + friend std::ostream& operator<<(std::ostream&,nas_ref&); bool objchk(u32); inline nas_ref* addr(); inline u32 ret (); @@ -107,7 +107,7 @@ struct nas_vec std::vector elems; nas_vec():printed(false){} - void print(); + friend std::ostream& operator<<(std::ostream&,nas_vec&); usize size(){return elems.size();} nas_ref get_val(const i32); nas_ref* get_mem(const i32); @@ -119,7 +119,7 @@ struct nas_hash std::unordered_map elems; nas_hash():printed(false){} - void print(); + friend std::ostream& operator<<(std::ostream&,nas_hash&); usize size(){return elems.size();} nas_ref get_val(const string&); nas_ref* get_mem(const string&); @@ -268,23 +268,21 @@ nas_ref* nas_vec::get_mem(const i32 n) return nullptr; return &elems[n>=0?n:n+size]; } -void nas_vec::print() +std::ostream& operator<<(std::ostream& out,nas_vec& vec) { - if(!elems.size() || printed) + if(!vec.elems.size() || vec.printed) { - std::cout<<(elems.size()?"[..]":"[]"); - return; + out<<(vec.elems.size()?"[..]":"[]"); + return out; } - printed=true; + vec.printed=true; usize iter=0; - usize size=elems.size(); - std::cout<<'['; - for(auto& i:elems) - { - i.print(); - std::cout<<",]"[(++iter)==size]; - } - printed=false; + usize size=vec.elems.size(); + out<<'['; + for(auto& i:vec.elems) + out<"; break; - case vm_co: std::cout<<""; break; + case vm_none: out<<"undefined"; break; + case vm_nil: out<<"nil"; break; + case vm_num: out<"; break; + case vm_co: out<<""; break; } + return out; } bool nas_ref::objchk(u32 objtype) { diff --git a/nasal_import.h b/nasal_import.h index da25ba9..bebcd9a 100644 --- a/nasal_import.h +++ b/nasal_import.h @@ -4,6 +4,8 @@ #ifndef _MSC_VER #include #else +#define _CRT_SECURE_NO_DEPRECATE 1 +#define _CRT_NONSTDC_NO_DEPRECATE 1 #include #endif