optimize source code
This commit is contained in:
parent
56289b5d22
commit
5d13261516
17
README.md
17
README.md
|
@ -760,18 +760,19 @@ nasal_ref builtin_chr(std::vector<nasal_ref>&,nasal_gc&);
|
|||
Then complete this function using C++:
|
||||
|
||||
```C++
|
||||
nasal_ref builtin_print(std::vector<nasal_ref>& local_scope,nasal_gc& gc)
|
||||
nasal_ref builtin_print(std::vector<nasal_ref>& local,nasal_gc& gc)
|
||||
{
|
||||
// get arguments by using builtin_find
|
||||
// find value with index begin from 1
|
||||
// because local_scope[0] is reserved for value 'me'
|
||||
nasal_ref vec_addr=local_scope[1];
|
||||
nasal_ref vec=local[1];
|
||||
// main process
|
||||
// also check number of arguments and type here
|
||||
// if get a type error,use builtin_err and return nullptr
|
||||
for(auto i:vec_addr.vec()->elems)
|
||||
for(auto i:vec.vec()->elems)
|
||||
switch(i.type)
|
||||
{
|
||||
case vm_none: std::cout<<"undefined"; break;
|
||||
case vm_nil: std::cout<<"nil"; break;
|
||||
case vm_num: std::cout<<i.num(); break;
|
||||
case vm_str: std::cout<<*i.str(); break;
|
||||
|
@ -841,9 +842,9 @@ this is a fatal error.
|
|||
So use builtin_alloc in builtin functions like this:
|
||||
|
||||
```C++
|
||||
nasal_ref builtin_keys(std::vector<nasal_ref>& local_scope,nasal_gc& gc)
|
||||
nasal_ref builtin_keys(std::vector<nasal_ref>& local,nasal_gc& gc)
|
||||
{
|
||||
nasal_ref hash_addr=local_scope[1];
|
||||
nasal_ref hash_addr=local[1];
|
||||
if(hash_addr.type!=vm_hash)
|
||||
{
|
||||
builtin_err("keys","\"hash\" must be hash");
|
||||
|
@ -851,15 +852,15 @@ nasal_ref builtin_keys(std::vector<nasal_ref>& local_scope,nasal_gc& gc)
|
|||
}
|
||||
|
||||
// push vector into local scope to avoid being sweeped
|
||||
local_scope.push_back(gc.gc_alloc(vm_vec));
|
||||
std::vector<nasal_ref>& vec=local_scope.back().vec()->elems;
|
||||
local.push_back(gc.gc_alloc(vm_vec));
|
||||
std::vector<nasal_ref>& vec=local.back().vec()->elems;
|
||||
for(auto iter:hash_addr.hash()->elems)
|
||||
{
|
||||
nasal_ref str_addr=gc.builtin_alloc(vm_str);
|
||||
*str_addr.str()=iter.first;
|
||||
vec.push_back(str_addr);
|
||||
}
|
||||
return local_scope.back();
|
||||
return local.back();
|
||||
}
|
||||
```
|
||||
|
||||
|
|
880
nasal_builtin.h
880
nasal_builtin.h
File diff suppressed because it is too large
Load Diff
|
@ -197,10 +197,10 @@ struct opcode
|
|||
class nasal_codegen
|
||||
{
|
||||
private:
|
||||
int error;
|
||||
uint32_t error;
|
||||
uint16_t fileindex;
|
||||
int in_forindex;
|
||||
int in_foreach;
|
||||
uint32_t in_forindex;
|
||||
uint32_t in_foreach;
|
||||
std::unordered_map<double,int> number_table;
|
||||
std::unordered_map<std::string,int> string_table;
|
||||
std::vector<double> num_res_table;
|
||||
|
@ -253,7 +253,7 @@ private:
|
|||
void block_gen(const nasal_ast&);
|
||||
void ret_gen(const nasal_ast&);
|
||||
public:
|
||||
int get_error(){return error;}
|
||||
uint32_t get_error(){return error;}
|
||||
void main_progress(const nasal_ast&,const std::vector<std::string>&);
|
||||
void print_op(int);
|
||||
void print_byte_code();
|
||||
|
@ -1191,12 +1191,12 @@ void nasal_codegen::block_gen(const nasal_ast& ast)
|
|||
|
||||
void nasal_codegen::ret_gen(const nasal_ast& ast)
|
||||
{
|
||||
for(int i=0;i<in_foreach;++i)
|
||||
for(uint32_t i=0;i<in_foreach;++i)
|
||||
{
|
||||
gen(op_pop,0,0);
|
||||
gen(op_cntpop,0,0);
|
||||
}
|
||||
for(int i=0;i<in_forindex;++i)
|
||||
for(uint32_t i=0;i<in_forindex;++i)
|
||||
{
|
||||
gen(op_pop,0,0);
|
||||
gen(op_cntpop,0,0);
|
||||
|
|
45
nasal_gc.h
45
nasal_gc.h
|
@ -104,15 +104,15 @@ struct nasal_hash// 56 bytes
|
|||
nasal_ref* get_mem(std::string&);
|
||||
};
|
||||
|
||||
struct nasal_func// 120 bytes
|
||||
struct nasal_func// 112 bytes
|
||||
{
|
||||
int32_t dynpara; // dynamic parameter name index in hash
|
||||
uint32_t entry; // pc will set to entry-1 to call this function
|
||||
std::vector<nasal_ref> local; // local scope with default value(nasal_ref)
|
||||
std::vector<nasal_ref> upvalue;
|
||||
std::unordered_map<std::string,int> key_table;// parameter name hash
|
||||
int32_t dynpara; // dynamic parameter name index in hash
|
||||
uint32_t entry; // pc will set to entry-1 to call this function
|
||||
std::vector<nasal_ref> local; // local scope with default value(nasal_ref)
|
||||
std::vector<nasal_ref> upvalue; // closure
|
||||
std::unordered_map<std::string,int> key_table; // parameter name table
|
||||
|
||||
nasal_func();
|
||||
nasal_func():dynpara(-1){}
|
||||
void clear();
|
||||
};
|
||||
|
||||
|
@ -178,6 +178,7 @@ void nasal_vec::print()
|
|||
case vm_vec: i.vec()->print(); break;
|
||||
case vm_hash: i.hash()->print(); break;
|
||||
case vm_func: std::cout<<"func(..){..}";break;
|
||||
case vm_obj: std::cout<<"<object>"; break;
|
||||
}
|
||||
std::cout<<",]"[(++iter)==elems.size()];
|
||||
}
|
||||
|
@ -253,6 +254,7 @@ void nasal_hash::print()
|
|||
case vm_vec: tmp.vec()->print(); break;
|
||||
case vm_hash: tmp.hash()->print(); break;
|
||||
case vm_func: std::cout<<"func(..){..}";break;
|
||||
case vm_obj: std::cout<<"<object>"; break;
|
||||
}
|
||||
std::cout<<",}"[(++iter)==elems.size()];
|
||||
}
|
||||
|
@ -261,11 +263,6 @@ void nasal_hash::print()
|
|||
}
|
||||
|
||||
/*functions of nasal_func*/
|
||||
nasal_func::nasal_func()
|
||||
{
|
||||
dynpara=-1;
|
||||
return;
|
||||
}
|
||||
void nasal_func::clear()
|
||||
{
|
||||
dynpara=-1;
|
||||
|
@ -403,20 +400,20 @@ void nasal_gc::gc_init(const std::vector<std::string>& strs)
|
|||
for(uint8_t i=vm_str;i<vm_type_size;++i)
|
||||
for(uint32_t j=0;j<increment[i];++j)
|
||||
{
|
||||
nasal_ref tmp={i,new nasal_val(i)};
|
||||
memory.push_back(tmp.value.gcobj);
|
||||
free_list[i].push(tmp.value.gcobj);
|
||||
nasal_val* tmp=new nasal_val(i);
|
||||
memory.push_back(tmp);
|
||||
free_list[i].push(tmp);
|
||||
}
|
||||
|
||||
stack_top=val_stack; // set stack_top to val_stack
|
||||
stack_top=val_stack; // set stack_top to val_stack
|
||||
|
||||
zero={vm_num,(double)0}; // init constant 0
|
||||
one ={vm_num,(double)1}; // init constant 1
|
||||
nil.type=vm_nil; // init constant nil
|
||||
nil ={vm_nil,(double)0}; // init constant nil
|
||||
|
||||
// init constant strings
|
||||
str_addrs.resize(strs.size());
|
||||
for(int i=0;i<strs.size();++i)
|
||||
for(uint32_t i=0;i<strs.size();++i)
|
||||
{
|
||||
str_addrs[i]={vm_str,new nasal_val(vm_str)};
|
||||
*str_addrs[i].str()=strs[i];
|
||||
|
@ -448,9 +445,9 @@ nasal_ref nasal_gc::gc_alloc(uint8_t type)
|
|||
if(free_list[type].empty())
|
||||
for(uint32_t i=0;i<increment[type];++i)
|
||||
{
|
||||
nasal_ref tmp={type,new nasal_val(type)};
|
||||
memory.push_back(tmp.value.gcobj);
|
||||
free_list[type].push(tmp.value.gcobj);
|
||||
nasal_val* tmp=new nasal_val(type);
|
||||
memory.push_back(tmp);
|
||||
free_list[type].push(tmp);
|
||||
}
|
||||
nasal_ref ret={type,free_list[type].front()};
|
||||
ret.value.gcobj->mark=GC_UNCOLLECTED;
|
||||
|
@ -466,9 +463,9 @@ nasal_ref nasal_gc::builtin_alloc(uint8_t type)
|
|||
if(free_list[type].empty())
|
||||
for(uint32_t i=0;i<increment[type];++i)
|
||||
{
|
||||
nasal_ref tmp={type,new nasal_val(type)};
|
||||
memory.push_back(tmp.value.gcobj);
|
||||
free_list[type].push(tmp.value.gcobj);
|
||||
nasal_val* tmp=new nasal_val(type);
|
||||
memory.push_back(tmp);
|
||||
free_list[type].push(tmp);
|
||||
}
|
||||
nasal_ref ret={type,free_list[type].front()};
|
||||
ret.value.gcobj->mark=GC_UNCOLLECTED;
|
||||
|
|
Loading…
Reference in New Issue