🎨 improve code style
This commit is contained in:
parent
a37ce6aadd
commit
cd21540710
18
nasal_ast.h
18
nasal_ast.h
|
@ -145,6 +145,15 @@ const char* ast_name[]={
|
|||
};
|
||||
|
||||
class ast {
|
||||
public:
|
||||
ast(const ast&) = delete;
|
||||
ast& operator=(const ast&) = delete;
|
||||
|
||||
ast(ast&&) = default;
|
||||
ast& operator=(ast&&) = default;
|
||||
private:
|
||||
void print(u32,bool,std::vector<string>&) const;
|
||||
|
||||
private:
|
||||
span loc;
|
||||
u32 nd_type;
|
||||
|
@ -155,13 +164,8 @@ private:
|
|||
public:
|
||||
ast(const span& s,const u32 t)
|
||||
: loc(s),nd_type(t),nd_num(0),nd_str("") {}
|
||||
ast(const ast&) = default;
|
||||
ast(ast&&) = default;
|
||||
ast& operator=(const ast&) = default;
|
||||
ast& operator=(ast&&) = default;
|
||||
|
||||
void dump() const;
|
||||
void print(u32,bool,std::vector<string>&) const;
|
||||
void clear();
|
||||
|
||||
ast& operator[](usize n) {return nd_child[n];}
|
||||
|
@ -169,7 +173,6 @@ public:
|
|||
usize size() const {return nd_child.size();}
|
||||
|
||||
void add(ast&& node) {nd_child.push_back(std::move(node));}
|
||||
void add(const ast& node) {nd_child.push_back(node);}
|
||||
void set_begin(const u32,const u32);
|
||||
void set_end(const u32,const u32);
|
||||
void set_type(const u32 t) {nd_type=t;}
|
||||
|
@ -177,14 +180,15 @@ public:
|
|||
void set_num(const f64 n) {nd_num=n;}
|
||||
|
||||
u32 line() const {return loc.end_line;}
|
||||
u32 col() const {return loc.end_column;}
|
||||
u32 type() const {return nd_type;}
|
||||
f64 num() const {return nd_num;}
|
||||
const string& str() const {return nd_str;}
|
||||
const string& file() const {return loc.file;}
|
||||
const span& location() const {return loc;}
|
||||
|
||||
const std::vector<ast>& child() const {return nd_child;}
|
||||
std::vector<ast>& child() {return nd_child;}
|
||||
|
||||
void update_span();
|
||||
void update_span(const span&);
|
||||
};
|
||||
|
|
|
@ -41,10 +41,12 @@ private:
|
|||
void regist_str(const string&);
|
||||
void find_symbol(const ast&);
|
||||
void add_sym(const string&);
|
||||
i32 local_find(const string&);
|
||||
i32 global_find(const string&);
|
||||
i32 upvalue_find(const string&);
|
||||
i32 local_find(const string&);
|
||||
i32 global_find(const string&);
|
||||
i32 upvalue_find(const string&);
|
||||
|
||||
void gen(u8,u32,u32);
|
||||
|
||||
void num_gen(const ast&);
|
||||
void str_gen(const ast&);
|
||||
void bool_gen(const ast&);
|
||||
|
@ -78,6 +80,7 @@ private:
|
|||
void calc_gen(const ast&);
|
||||
void block_gen(const ast&);
|
||||
void ret_gen(const ast&);
|
||||
|
||||
public:
|
||||
codegen(error& e):fileindex(0),err(e),file(nullptr) {}
|
||||
const error& compile(const parse&,const linker&);
|
||||
|
|
33
nasal_gc.h
33
nasal_gc.h
|
@ -18,6 +18,7 @@
|
|||
#include <vector>
|
||||
#include <unordered_map>
|
||||
#include <chrono>
|
||||
#include <algorithm>
|
||||
|
||||
#include "nasal.h"
|
||||
#include "nasal_err.h"
|
||||
|
@ -713,26 +714,31 @@ void gc::clear() {
|
|||
}
|
||||
|
||||
void gc::info() {
|
||||
using std::left;
|
||||
using std::setw;
|
||||
using std::setfill;
|
||||
const char* name[]={"str ","vec ","hash ","func ","upval","obj ","co "};
|
||||
std::clog<<"\ngarbage collector info (gc count|alloc count|memory size)\n";
|
||||
std::clog<<"\ngc info (gc count|alloc count|memory size)\n";
|
||||
|
||||
u32 maxlen=0;
|
||||
usize ident=0;
|
||||
for(u8 i=0;i<gc_type_size;++i) {
|
||||
u32 len=std::to_string(gcnt[i]).length();
|
||||
maxlen=maxlen<len?len:maxlen;
|
||||
len=std::to_string(acnt[i]).length();
|
||||
maxlen=maxlen<len?len:maxlen;
|
||||
len=std::to_string(size[i]).length();
|
||||
maxlen=maxlen<len?len:maxlen;
|
||||
usize len=std::max({
|
||||
std::to_string(gcnt[i]).length(),
|
||||
std::to_string(acnt[i]).length(),
|
||||
std::to_string(size[i]).length()
|
||||
});
|
||||
ident=ident<len?len:ident;
|
||||
}
|
||||
|
||||
double total=0;
|
||||
for(u8 i=0;i<gc_type_size;++i) {
|
||||
if (gcnt[i] || acnt[i] || size[i]) {
|
||||
total+=gcnt[i];
|
||||
std::clog<<" "<<name[i]<<" | "<<std::left<<std::setw(maxlen)<<std::setfill(' ')<<gcnt[i];
|
||||
std::clog<<" | "<<std::left<<std::setw(maxlen)<<std::setfill(' ')<<acnt[i];
|
||||
std::clog<<" | "<<std::left<<std::setw(maxlen)<<std::setfill(' ')<<size[i]<<"\n";
|
||||
std::clog<<" "<<name[i];
|
||||
std::clog<<" | "<<left<<setw(ident)<<setfill(' ')<<gcnt[i];
|
||||
std::clog<<" | "<<left<<setw(ident)<<setfill(' ')<<acnt[i];
|
||||
std::clog<<" | "<<left<<setw(ident)<<setfill(' ')<<size[i];
|
||||
std::clog<<"\n";
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -745,14 +751,15 @@ void gc::info() {
|
|||
}
|
||||
|
||||
var gc::alloc(u8 type) {
|
||||
using clk=std::chrono::high_resolution_clock;
|
||||
const u8 index=type-vm_str;
|
||||
++acnt[index];
|
||||
if (unused[index].empty()) {
|
||||
++gcnt[index];
|
||||
auto begin=std::chrono::high_resolution_clock::now();
|
||||
auto begin=clk::now();
|
||||
mark();
|
||||
sweep();
|
||||
worktime+=(std::chrono::high_resolution_clock::now()-begin).count();
|
||||
worktime+=(clk::now()-begin).count();
|
||||
}
|
||||
if (unused[index].empty()) {
|
||||
extend(type);
|
||||
|
|
|
@ -154,53 +154,57 @@ void codestream::set(const f64* numbuff,const string* strbuff,const string* file
|
|||
}
|
||||
|
||||
void codestream::dump(std::ostream& out) const {
|
||||
using std::setw;
|
||||
using std::setfill;
|
||||
using std::hex;
|
||||
using std::dec;
|
||||
auto op=code.op;
|
||||
auto num=code.num;
|
||||
out<<std::hex<<"0x"
|
||||
<<std::setw(6)<<std::setfill('0')<<index<<" "
|
||||
<<std::setw(2)<<std::setfill('0')<<(u32)op<<" "
|
||||
<<std::setw(2)<<std::setfill('0')<<((num>>16)&0xff)<<" "
|
||||
<<std::setw(2)<<std::setfill('0')<<((num>>8)&0xff)<<" "
|
||||
<<std::setw(2)<<std::setfill('0')<<(num&0xff)<<" "
|
||||
<<opname[op]<<" "<<std::dec;
|
||||
out<<hex<<"0x"
|
||||
<<setw(6)<<setfill('0')<<index<<" "
|
||||
<<setw(2)<<setfill('0')<<(u32)op<<" "
|
||||
<<setw(2)<<setfill('0')<<((num>>16)&0xff)<<" "
|
||||
<<setw(2)<<setfill('0')<<((num>>8)&0xff)<<" "
|
||||
<<setw(2)<<setfill('0')<<(num&0xff)<<" "
|
||||
<<opname[op]<<" "<<dec;
|
||||
switch(op) {
|
||||
case op_addeq: case op_subeq: case op_muleq: case op_diveq:
|
||||
case op_lnkeq: case op_meq: case op_btandeq: case op_btoreq:
|
||||
case op_btxoreq:
|
||||
out<<std::hex<<"0x"<<num<<std::dec<<" sp-"<<num;break;
|
||||
out<<hex<<"0x"<<num<<dec<<" sp-"<<num;break;
|
||||
case op_addeqc:case op_subeqc: case op_muleqc:case op_diveqc:
|
||||
out<<std::hex<<"0x"<<num<<std::dec<<" ("<<nums[num]<<")";break;
|
||||
out<<hex<<"0x"<<num<<dec<<" ("<<nums[num]<<")";break;
|
||||
case op_lnkeqc:
|
||||
out<<std::hex<<"0x"<<num<<std::dec<<" ("<<rawstr(strs[num],16)<<")";break;
|
||||
out<<hex<<"0x"<<num<<dec<<" ("<<rawstr(strs[num],16)<<")";break;
|
||||
case op_addecp:case op_subecp:case op_mulecp:case op_divecp:
|
||||
out<<std::hex<<"0x"<<num<<std::dec<<" ("<<nums[num]<<") sp-1";break;
|
||||
out<<hex<<"0x"<<num<<dec<<" ("<<nums[num]<<") sp-1";break;
|
||||
case op_lnkecp:
|
||||
out<<std::hex<<"0x"<<num<<std::dec<<" ("<<rawstr(strs[num],16)<<") sp-1";break;
|
||||
out<<hex<<"0x"<<num<<dec<<" ("<<rawstr(strs[num],16)<<") sp-1";break;
|
||||
case op_addc: case op_subc: case op_mulc: case op_divc:
|
||||
case op_lessc: case op_leqc: case op_grtc: case op_geqc:
|
||||
case op_pnum:
|
||||
out<<std::hex<<"0x"<<num<<std::dec<<" ("<<nums[num]<<")";break;
|
||||
out<<hex<<"0x"<<num<<dec<<" ("<<nums[num]<<")";break;
|
||||
case op_callvi:case op_newv: case op_callfv:
|
||||
case op_intg: case op_intl:
|
||||
case op_findex:case op_feach:
|
||||
case op_newf: case op_jmp: case op_jt: case op_jf:
|
||||
case op_callg: case op_mcallg: case op_loadg:
|
||||
case op_calll: case op_mcalll: case op_loadl:
|
||||
out<<std::hex<<"0x"<<num<<std::dec;break;
|
||||
out<<hex<<"0x"<<num<<dec;break;
|
||||
case op_callb:
|
||||
out<<std::hex<<"0x"<<num<<" <"<<builtin[num].name
|
||||
<<"@0x"<<(u64)builtin[num].func<<std::dec<<">";break;
|
||||
out<<hex<<"0x"<<num<<" <"<<builtin[num].name
|
||||
<<"@0x"<<(u64)builtin[num].func<<dec<<">";break;
|
||||
case op_upval: case op_mupval: case op_loadu:
|
||||
out<<std::hex<<"0x"<<((num>>16)&0xffff)
|
||||
<<"[0x"<<(num&0xffff)<<"]"<<std::dec;break;
|
||||
out<<hex<<"0x"<<((num>>16)&0xffff)
|
||||
<<"[0x"<<(num&0xffff)<<"]"<<dec;break;
|
||||
case op_happ: case op_pstr:
|
||||
case op_lnkc:
|
||||
case op_callh: case op_mcallh:
|
||||
case op_para: case op_deft: case op_dyn:
|
||||
out<<std::hex<<"0x"<<num<<std::dec<<" ("<<rawstr(strs[num],16)<<")";break;
|
||||
out<<hex<<"0x"<<num<<dec<<" ("<<rawstr(strs[num],16)<<")";break;
|
||||
default:
|
||||
if (files) {
|
||||
out<<std::hex<<"0x"<<num<<std::dec;
|
||||
out<<hex<<"0x"<<num<<dec;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -1097,9 +1097,12 @@ ast parse::ret_expr() {
|
|||
ast node(toks[ptr].loc,ast_ret);
|
||||
match(tok::ret);
|
||||
tok type=toks[ptr].type;
|
||||
if (type==tok::tknil || type==tok::num || type==tok::str || type==tok::id ||
|
||||
type==tok::func || type==tok::sub || type==tok::opnot || type==tok::lcurve ||
|
||||
type==tok::lbracket || type==tok::lbrace || type==tok::tktrue || type==tok::tkfalse
|
||||
if (type==tok::tknil || type==tok::num ||
|
||||
type==tok::str || type==tok::id ||
|
||||
type==tok::func || type==tok::sub ||
|
||||
type==tok::opnot || type==tok::lcurve ||
|
||||
type==tok::lbracket || type==tok::lbrace ||
|
||||
type==tok::tktrue || type==tok::tkfalse
|
||||
) {
|
||||
node.add(calc());
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue