update
This commit is contained in:
parent
f53bbfef07
commit
4ccbce36ce
|
@ -17,9 +17,7 @@
|
|||
#include "nasal_ast.h"
|
||||
#include "nasal_lexer.h"
|
||||
#include "nasal_parse.h"
|
||||
#include "nasal_scalar.h"
|
||||
#include "nasal_gc.h"
|
||||
#include "nasal_sym.h"
|
||||
#include "nasal_runtime.h"
|
||||
|
||||
#endif
|
|
@ -1,6 +1,49 @@
|
|||
#ifndef __NASAL_GC_H__
|
||||
#define __NASAL_GC_H__
|
||||
|
||||
class nasal_function
|
||||
{
|
||||
private:
|
||||
std::list<std::map<std::string,int> > local_scope;
|
||||
abstract_syntax_tree function_root;
|
||||
public:
|
||||
nasal_function();
|
||||
~nasal_function();
|
||||
void set_clear();
|
||||
void set_statement_block(abstract_syntax_tree&);
|
||||
nasal_function& operator=(const nasal_function&);
|
||||
std::list<std::map<std::string,int> >& get_local_scope();
|
||||
abstract_syntax_tree& get_statement_block();
|
||||
};
|
||||
|
||||
class nasal_scalar
|
||||
{
|
||||
private:
|
||||
int type;
|
||||
std::string var_string;
|
||||
double var_number;
|
||||
std::vector<int> var_array;
|
||||
std::map<std::string,int> var_hash;
|
||||
nasal_function var_func;
|
||||
public:
|
||||
nasal_scalar();
|
||||
nasal_scalar(const nasal_scalar&);
|
||||
nasal_scalar& operator=(const nasal_scalar&);
|
||||
void set_clear();
|
||||
void set_type(int);
|
||||
void set_number(double);
|
||||
void set_string(std::string&);
|
||||
void vector_push(int);
|
||||
int vector_pop();
|
||||
void hash_add_new_member(std::string,int);
|
||||
void hash_delete_member(std::string);
|
||||
int hash_get_member(std::string);
|
||||
int get_type();
|
||||
double get_number();
|
||||
std::string get_string();
|
||||
nasal_function& get_function();
|
||||
};
|
||||
|
||||
struct gc_unit
|
||||
{
|
||||
// collected: If gc collected this item,it'll be set to true.Otherwise it is false.
|
||||
|
@ -68,6 +111,8 @@ class gc_manager
|
|||
for(int i=0;i<memory_size;++i)
|
||||
if((memory[i].refcnt<=0) && (!memory[i].collected))
|
||||
{
|
||||
// need more details here
|
||||
// especially vector and hash
|
||||
memory[i].set_clear();
|
||||
free_space.push_back(i);
|
||||
std::cout<<">> [Gc] collected ";
|
||||
|
@ -144,7 +189,154 @@ class gc_manager
|
|||
}
|
||||
};
|
||||
gc_manager nasal_gc;
|
||||
// this object is used in "nasal_sym.h" and "nasal_runtime.h"
|
||||
// this object is used in "nasal_runtime.h"
|
||||
// because there must be only one gc when running a program
|
||||
|
||||
nasal_function::nasal_function()
|
||||
{
|
||||
local_scope.clear();
|
||||
function_root.set_clear();
|
||||
return;
|
||||
}
|
||||
nasal_function::~nasal_function()
|
||||
{
|
||||
local_scope.clear();
|
||||
function_root.set_clear();
|
||||
return;
|
||||
}
|
||||
void nasal_function::set_clear()
|
||||
{
|
||||
local_scope.clear();
|
||||
function_root.set_clear();
|
||||
return;
|
||||
}
|
||||
void nasal_function::set_statement_block(abstract_syntax_tree& func_block)
|
||||
{
|
||||
function_root=func_block;
|
||||
return;
|
||||
}
|
||||
nasal_function& nasal_function::operator=(const nasal_function& tmp)
|
||||
{
|
||||
local_scope=tmp.local_scope;
|
||||
function_root=tmp.function_root;
|
||||
return *this;
|
||||
}
|
||||
std::list<std::map<std::string,int> >& nasal_function::get_local_scope()
|
||||
{
|
||||
return local_scope;
|
||||
}
|
||||
abstract_syntax_tree& nasal_function::get_statement_block()
|
||||
{
|
||||
return function_root;
|
||||
}
|
||||
|
||||
nasal_scalar::nasal_scalar()
|
||||
{
|
||||
type=scalar_nil;
|
||||
var_string="";
|
||||
var_number=0;
|
||||
var_array.clear();
|
||||
var_hash.clear();
|
||||
return;
|
||||
}
|
||||
nasal_scalar::nasal_scalar(const nasal_scalar& tmp)
|
||||
{
|
||||
type=tmp.type;
|
||||
var_string=tmp.var_string;
|
||||
var_number=tmp.var_number;
|
||||
var_array =tmp.var_array;
|
||||
var_hash =tmp.var_hash;
|
||||
var_func =tmp.var_func;
|
||||
return;
|
||||
}
|
||||
nasal_scalar& nasal_scalar::operator=(const nasal_scalar& tmp)
|
||||
{
|
||||
type=tmp.type;
|
||||
var_string=tmp.var_string;
|
||||
var_number=tmp.var_number;
|
||||
var_array =tmp.var_array;
|
||||
var_hash =tmp.var_hash;
|
||||
var_func =tmp.var_func;
|
||||
return *this;
|
||||
}
|
||||
void nasal_scalar::set_clear()
|
||||
{
|
||||
type=scalar_nil;
|
||||
var_string.clear();
|
||||
var_number=0;
|
||||
var_array.clear();
|
||||
var_hash.clear();
|
||||
var_func.set_clear();
|
||||
return;
|
||||
}
|
||||
void nasal_scalar::set_type(int tmp_type)
|
||||
{
|
||||
// scalar_function is the last enum in enum::scalar_type
|
||||
type=tmp_type>scalar_function? scalar_nil:tmp_type;
|
||||
return;
|
||||
}
|
||||
void nasal_scalar::set_number(double tmp_number)
|
||||
{
|
||||
var_number=tmp_number;
|
||||
return;
|
||||
}
|
||||
void nasal_scalar::set_string(std::string& tmp_str)
|
||||
{
|
||||
var_string=tmp_str;
|
||||
return;
|
||||
}
|
||||
void nasal_scalar::vector_push(int addr)
|
||||
{
|
||||
var_array.push_back(addr);
|
||||
return;
|
||||
}
|
||||
int nasal_scalar::vector_pop()
|
||||
{
|
||||
int ret=var_array.back();
|
||||
var_array.pop_back();
|
||||
return ret;
|
||||
}
|
||||
void nasal_scalar::hash_add_new_member(std::string member_name,int addr)
|
||||
{
|
||||
// if hash has a new function member
|
||||
// this function will get a value named 'me' in its own scope
|
||||
// and the 'me' points to the hash that has the function
|
||||
if(var_hash.find(member_name)!=var_hash.end())
|
||||
std::cout<<">> [Runtime] "<<member_name<<" exists."<<std::endl;
|
||||
else
|
||||
var_hash[member_name]=addr;
|
||||
return;
|
||||
}
|
||||
void nasal_scalar::hash_delete_member(std::string member_name)
|
||||
{
|
||||
if(var_hash.find(member_name)!=var_hash.end())
|
||||
{
|
||||
nasal_gc.reference_delete(var_hash[member_name]);
|
||||
var_hash.erase(member_name);
|
||||
}
|
||||
return;
|
||||
}
|
||||
int nasal_scalar::hash_get_member(std::string member_name)
|
||||
{
|
||||
if(var_hash.find(member_name)!=var_hash.end())
|
||||
return var_hash[member_name];
|
||||
return -1;
|
||||
}
|
||||
int nasal_scalar::get_type()
|
||||
{
|
||||
return type;
|
||||
}
|
||||
double nasal_scalar::get_number()
|
||||
{
|
||||
return var_number;
|
||||
}
|
||||
std::string nasal_scalar::get_string()
|
||||
{
|
||||
return var_string;
|
||||
}
|
||||
nasal_function& nasal_scalar::get_function()
|
||||
{
|
||||
return var_func;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
@ -333,7 +333,7 @@ class nasal_lexer
|
|||
if(!check_numerable_string(token_str))
|
||||
{
|
||||
++error;
|
||||
std::cout<<">> [Lexer-error] line "<<line<<": "<<token_str<<" is not a numerable string."<<std::endl;
|
||||
std::cout<<">> [Lexer] line "<<line<<": "<<token_str<<" is not a numerable string."<<std::endl;
|
||||
token_str="0";
|
||||
}
|
||||
token new_token;
|
||||
|
@ -384,7 +384,7 @@ class nasal_lexer
|
|||
if(ptr==res.end() || *ptr!=str_begin)
|
||||
{
|
||||
++error;
|
||||
std::cout<<">> [Lexer-error] line "<<line<<": this string must have a \' "<<str_begin<<" \' as its end."<<std::endl;
|
||||
std::cout<<">> [Lexer] line "<<line<<": this string must have a \' "<<str_begin<<" \' as its end."<<std::endl;
|
||||
--ptr;
|
||||
}
|
||||
else
|
||||
|
@ -429,7 +429,7 @@ class nasal_lexer
|
|||
else
|
||||
{
|
||||
++error;
|
||||
std::cout<<">> [Lexer-error] line "<<line<<": unknown char."<<std::endl;
|
||||
std::cout<<">> [Lexer] line "<<line<<": unknown char."<<std::endl;
|
||||
++ptr;
|
||||
}
|
||||
}
|
||||
|
@ -535,7 +535,7 @@ class nasal_lexer
|
|||
else
|
||||
{
|
||||
++error;
|
||||
std::cout<<">> [Lexer-error] line "<<detail_token.line<<": unknown operator \'"<<i->str<<"\'."<<std::endl;
|
||||
std::cout<<">> [Lexer] line "<<detail_token.line<<": unknown operator \'"<<i->str<<"\'."<<std::endl;
|
||||
detail_token.type=__unknown_operator;
|
||||
}
|
||||
detail_token_list.push_back(detail_token);
|
||||
|
|
|
@ -799,6 +799,7 @@ abstract_syntax_tree nasal_parse::multive_calculation()
|
|||
this->get_token();
|
||||
if((this_token.type==__sub_operator) || (this_token.type==__nor_operator))
|
||||
{
|
||||
// unary calculation
|
||||
calc_node.set_clear();
|
||||
calc_node.set_node_line(this_token.line);
|
||||
calc_node.set_node_type(this_token.type);
|
||||
|
|
|
@ -5,7 +5,7 @@ class nasal_runtime
|
|||
{
|
||||
private:
|
||||
// local hash_map will be used when running
|
||||
sym_hash_map global_scope;
|
||||
std::list<std::map<std::string,int> > global_scope;
|
||||
|
||||
// see detail of each enum type in function error_interrupt(const int)
|
||||
enum runtime_error_type
|
||||
|
@ -14,26 +14,26 @@ class nasal_runtime
|
|||
__incorrect_head_of_func,
|
||||
__stack_overflow,
|
||||
};
|
||||
void error_interrupt (const int);
|
||||
void delete_total_scope(std::list<std::map<std::string,int> >&);
|
||||
void delete_last_scope (std::list<std::map<std::string,int> >&);
|
||||
void func_proc (std::list<std::map<std::string,int> >&,abstract_syntax_tree&);
|
||||
void call_identifier (abstract_syntax_tree&);
|
||||
void calculation (abstract_syntax_tree&);
|
||||
void assignment (abstract_syntax_tree&);
|
||||
void definition (abstract_syntax_tree&);
|
||||
void loop (abstract_syntax_tree&);
|
||||
void conditional (abstract_syntax_tree&);
|
||||
void error_interrupt (const int);
|
||||
void vector_generation (abstract_syntax_tree&);
|
||||
void hash_generation (abstract_syntax_tree&);
|
||||
void call_identifier (abstract_syntax_tree&);
|
||||
void calculation (abstract_syntax_tree&);
|
||||
void assignment (abstract_syntax_tree&);
|
||||
void definition (abstract_syntax_tree&);
|
||||
void loop_expr (abstract_syntax_tree&);
|
||||
void conditional (abstract_syntax_tree&);
|
||||
void func_proc (std::list<std::map<std::string,int> >&,abstract_syntax_tree&);
|
||||
public:
|
||||
nasal_runtime()
|
||||
{
|
||||
global_scope.set_clear();
|
||||
global_scope.clear();
|
||||
nasal_gc.gc_init();
|
||||
return;
|
||||
}
|
||||
~nasal_runtime()
|
||||
{
|
||||
global_scope.set_clear();
|
||||
global_scope.clear();
|
||||
nasal_gc.gc_init();
|
||||
return;
|
||||
}
|
||||
|
@ -56,24 +56,36 @@ void nasal_runtime::error_interrupt(const int type)
|
|||
return;
|
||||
}
|
||||
|
||||
void nasal_runtime::delete_total_scope(std::list<std::map<std::string,int> >& scope)
|
||||
void nasal_runtime::vector_generation(abstract_syntax_tree& node)
|
||||
{
|
||||
for(std::list<std::map<std::string,int> >::iterator i=scope.begin();i!=scope.end();++i)
|
||||
for(std::map<std::string,int>::iterator j=i->begin();j!=i->end();++j)
|
||||
nasal_gc.reference_delete(j->second);
|
||||
scope.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
void nasal_runtime::delete_last_scope(std::list<std::map<std::string,int> >& scope)
|
||||
void nasal_runtime::hash_generation(abstract_syntax_tree& node)
|
||||
{
|
||||
return;
|
||||
}
|
||||
void nasal_runtime::call_identifier(abstract_syntax_tree& node)
|
||||
{
|
||||
return;
|
||||
}
|
||||
void nasal_runtime::calculation(abstract_syntax_tree& node)
|
||||
{
|
||||
return;
|
||||
}
|
||||
void nasal_runtime::assignment(abstract_syntax_tree& node)
|
||||
{
|
||||
return;
|
||||
}
|
||||
void nasal_runtime::definition(abstract_syntax_tree& node)
|
||||
{
|
||||
return;
|
||||
}
|
||||
void nasal_runtime::loop_expr(abstract_syntax_tree& node)
|
||||
{
|
||||
return;
|
||||
}
|
||||
void nasal_runtime::conditional(abstract_syntax_tree& node)
|
||||
{
|
||||
if(scope.empty())
|
||||
return;
|
||||
std::list<std::map<std::string,int> >::iterator iter=scope.end();
|
||||
--iter;
|
||||
for(std::map<std::string,int>::iterator i=iter->begin();i!=iter->end();++i)
|
||||
nasal_gc.reference_delete(i->second);
|
||||
scope.pop_back();
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -92,11 +104,11 @@ void nasal_runtime::func_proc(std::list<std::map<std::string,int> >& local_scope
|
|||
;
|
||||
// only number or string
|
||||
else if(node_type==__id)
|
||||
;
|
||||
this->call_identifier(*iter);
|
||||
else if(node_type==__vector)
|
||||
;
|
||||
this->vector_generation(*iter);
|
||||
else if(node_type==__hash)
|
||||
;
|
||||
this->hash_generation(*iter);
|
||||
else if(node_type==__function)
|
||||
;
|
||||
else if(node_type==__add_operator || node_type==__sub_operator ||
|
||||
|
@ -114,15 +126,7 @@ void nasal_runtime::func_proc(std::list<std::map<std::string,int> >& local_scope
|
|||
;
|
||||
else if(node_type==__conditional)
|
||||
;
|
||||
else if(node_type==__while)
|
||||
;
|
||||
else if(node_type==__for)
|
||||
;
|
||||
else if(node_type==__foreach)
|
||||
;
|
||||
else if(node_type==__forindex)
|
||||
;
|
||||
else if(node_type==__return)
|
||||
else if((node_type==__while) || (node_type==__for) || (node_type==__foreach) || (node_type==__forindex))
|
||||
;
|
||||
}
|
||||
return;
|
||||
|
@ -132,7 +136,7 @@ void nasal_runtime::main_proc(abstract_syntax_tree& root)
|
|||
{
|
||||
time_t begin_time,end_time;
|
||||
begin_time=std::time(NULL);
|
||||
global_scope.set_clear();
|
||||
global_scope.clear();
|
||||
nasal_gc.gc_init();
|
||||
if(root.get_node_type()!=__root)
|
||||
{
|
||||
|
@ -146,16 +150,16 @@ void nasal_runtime::main_proc(abstract_syntax_tree& root)
|
|||
if(node_type==__number || node_type==__string)
|
||||
;
|
||||
else if(node_type==__id)
|
||||
{
|
||||
for(std::list<abstract_syntax_tree>::iterator i=iter->get_children().begin();i!=iter->get_children().end();++i)
|
||||
;
|
||||
}
|
||||
this->call_identifier(*iter);
|
||||
else if(node_type==__vector)
|
||||
;
|
||||
this->vector_generation(*iter);
|
||||
else if(node_type==__hash)
|
||||
;
|
||||
this->hash_generation(*iter);
|
||||
else if(node_type==__function)
|
||||
;
|
||||
{
|
||||
nasal_scalar temp_function;
|
||||
temp_function.set_type(scalar_function);
|
||||
}
|
||||
else if(node_type==__add_operator || node_type==__sub_operator ||
|
||||
node_type==__mul_operator || node_type==__div_operator ||
|
||||
node_type==__link_operator ||
|
||||
|
@ -171,13 +175,7 @@ void nasal_runtime::main_proc(abstract_syntax_tree& root)
|
|||
;
|
||||
else if(node_type==__conditional)
|
||||
;
|
||||
else if(node_type==__while)
|
||||
;
|
||||
else if(node_type==__for)
|
||||
;
|
||||
else if(node_type==__foreach)
|
||||
;
|
||||
else if(node_type==__forindex)
|
||||
else if((node_type==__while) || (node_type==__for) || (node_type==__foreach) || (node_type==__forindex))
|
||||
;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue