Update
This commit is contained in:
parent
851602ed9d
commit
305dedfdc1
|
@ -2,6 +2,13 @@
|
||||||
#define __ABSTRACT_SYNTAX_TREE_CPP__
|
#define __ABSTRACT_SYNTAX_TREE_CPP__
|
||||||
|
|
||||||
int exit_type=0;
|
int exit_type=0;
|
||||||
|
|
||||||
|
bool abstract_syntax_tree::check()
|
||||||
|
{
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
var abstract_syntax_tree::call_id()
|
var abstract_syntax_tree::call_id()
|
||||||
{
|
{
|
||||||
var temp;
|
var temp;
|
||||||
|
@ -71,6 +78,9 @@ void abstract_syntax_tree::run_root()
|
||||||
std::string _name=j->name;
|
std::string _name=j->name;
|
||||||
if(!scope.search_var(_name))
|
if(!scope.search_var(_name))
|
||||||
{
|
{
|
||||||
|
++j;
|
||||||
|
if(j!=i->children.end())
|
||||||
|
new_var=j->get_value();
|
||||||
new_var.set_name(_name);
|
new_var.set_name(_name);
|
||||||
scope.add_new_var(new_var);
|
scope.add_new_var(new_var);
|
||||||
}
|
}
|
||||||
|
@ -89,11 +99,15 @@ void abstract_syntax_tree::run_root()
|
||||||
std::cout<<i->call_id().get_name()<<std::endl;
|
std::cout<<i->call_id().get_name()<<std::endl;
|
||||||
else if(i->type==__while)
|
else if(i->type==__while)
|
||||||
{
|
{
|
||||||
;
|
scope.add_new_block_scope();
|
||||||
|
i->run_loop();
|
||||||
|
scope.pop_last_block_scope();
|
||||||
}
|
}
|
||||||
else if(i->type==__ifelse)
|
else if(i->type==__ifelse)
|
||||||
{
|
{
|
||||||
;
|
scope.add_new_block_scope();
|
||||||
|
i->run_ifelse();
|
||||||
|
scope.pop_last_block_scope();
|
||||||
}
|
}
|
||||||
if(exit_type!=__process_exited_successfully)
|
if(exit_type!=__process_exited_successfully)
|
||||||
break;
|
break;
|
||||||
|
@ -107,6 +121,26 @@ void abstract_syntax_tree::run_root()
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void abstract_syntax_tree::run_loop()
|
||||||
|
{
|
||||||
|
scope.add_new_local_scope();
|
||||||
|
|
||||||
|
abstract_syntax_tree condition=children.front();
|
||||||
|
abstract_syntax_tree blk=children.back();
|
||||||
|
while(condition.check())
|
||||||
|
blk.run_block(__loop);
|
||||||
|
scope.pop_last_local_scope();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void abstract_syntax_tree::run_ifelse()
|
||||||
|
{
|
||||||
|
scope.add_new_local_scope();
|
||||||
|
|
||||||
|
scope.pop_last_local_scope();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
void abstract_syntax_tree::run_func()
|
void abstract_syntax_tree::run_func()
|
||||||
{
|
{
|
||||||
scope.add_new_block_scope();
|
scope.add_new_block_scope();
|
||||||
|
@ -119,13 +153,10 @@ void abstract_syntax_tree::run_func()
|
||||||
void abstract_syntax_tree::run_block(int block_type)
|
void abstract_syntax_tree::run_block(int block_type)
|
||||||
{
|
{
|
||||||
scope.add_new_local_scope();
|
scope.add_new_local_scope();
|
||||||
|
|
||||||
scope.pop_last_local_scope();
|
scope.pop_last_local_scope();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -191,9 +191,12 @@ class abstract_syntax_tree
|
||||||
{
|
{
|
||||||
return children;
|
return children;
|
||||||
}
|
}
|
||||||
|
bool check();
|
||||||
var call_id();
|
var call_id();
|
||||||
var get_value();
|
var get_value();
|
||||||
void run_root();
|
void run_root();
|
||||||
|
void run_loop();
|
||||||
|
void run_ifelse();
|
||||||
void run_func();
|
void run_func();
|
||||||
void run_block(int);
|
void run_block(int);
|
||||||
};
|
};
|
||||||
|
|
|
@ -275,8 +275,9 @@ abstract_syntax_tree balloon_parse::definition()
|
||||||
get_token();
|
get_token();
|
||||||
if(this_token.type!=__equal)
|
if(this_token.type!=__equal)
|
||||||
{
|
{
|
||||||
++error;
|
++warning;
|
||||||
std::cout<<">>[Parse-error] line "<<this_token.line<<": expect a \'=\' here."<<std::endl;
|
std::cout<<">>[Parse-warning] line "<<this_token.line<<": every new var should be initialized."<<std::endl;
|
||||||
|
parse.push(this_token);
|
||||||
return new_node;
|
return new_node;
|
||||||
}
|
}
|
||||||
new_node.add_child(scalar());
|
new_node.add_child(scalar());
|
||||||
|
|
|
@ -4,34 +4,108 @@
|
||||||
class balloon_scope
|
class balloon_scope
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
std::list<var> scope;
|
std::list<var> global;
|
||||||
var error_var;
|
std::list<std::list<std::list<var> > > scope_list;
|
||||||
public:
|
public:
|
||||||
void set_clear()
|
void set_clear()
|
||||||
{
|
{
|
||||||
scope.clear();
|
global.clear();
|
||||||
|
scope_list.clear();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
bool search_var(std::string name)
|
bool search_var(std::string name)
|
||||||
{
|
{
|
||||||
for(std::list<var>::iterator i=scope.begin();i!=scope.end();++i)
|
if(!scope_list.empty())
|
||||||
if(i->get_name()==name)
|
{
|
||||||
return true;
|
std::list<std::list<var> >::iterator i=scope_list.back().end();
|
||||||
|
--i;
|
||||||
|
for(;;--i)
|
||||||
|
{
|
||||||
|
for(std::list<var>::iterator j=i->begin();j!=i->end();++j)
|
||||||
|
if(j->get_name()==name)
|
||||||
|
return true;
|
||||||
|
if(i==scope_list.back().begin())
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(!global.empty())
|
||||||
|
{
|
||||||
|
for(std::list<var>::iterator i=global.begin();i!=global.end();++i)
|
||||||
|
if(i->get_name()==name)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
void add_var(var t)
|
void add_new_var(var t)
|
||||||
{
|
{
|
||||||
scope.push_back(t);
|
if(!scope_list.empty())
|
||||||
|
{
|
||||||
|
std::list<std::list<var> >::iterator i=scope_list.back().end();
|
||||||
|
--i;
|
||||||
|
i->push_back(t);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
global.push_back(t);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var& get_var(std::string name)
|
var& get_var(std::string name)
|
||||||
{
|
{
|
||||||
for(std::list<var>::iterator i=scope.begin();i!=scope.end();++i)
|
if(!scope_list.empty())
|
||||||
if(i->get_name()==name)
|
{
|
||||||
return *i;
|
std::list<std::list<var> >::iterator i=scope_list.back().end();
|
||||||
std::cout<<">>[Runtime-error] \'"<<name<<"\' was not declared in this scope."<<std::endl;
|
--i;
|
||||||
|
// get the last scope block(std::list<std::list<var> >)
|
||||||
|
for(;;--i)
|
||||||
|
{
|
||||||
|
for(std::list<var>::iterator j=i->begin();j!=i->end();++j)
|
||||||
|
if(j->get_name()==name)
|
||||||
|
return *j;
|
||||||
|
if(i==scope_list.back().begin())
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(!global.empty())
|
||||||
|
{
|
||||||
|
for(std::list<var>::iterator i=global.begin();i!=global.end();++i)
|
||||||
|
if(i->get_name()==name)
|
||||||
|
return *i;
|
||||||
|
}
|
||||||
return error_var;
|
return error_var;
|
||||||
}
|
}
|
||||||
|
void add_new_block_scope()
|
||||||
|
{
|
||||||
|
std::list<std::list<var> > new_list;
|
||||||
|
scope_list.push_back(new_list);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
void add_new_local_scope()
|
||||||
|
{
|
||||||
|
if(!scope_list.empty())
|
||||||
|
{
|
||||||
|
std::list<std::list<std::list<var> > >::iterator i=scope_list.end();
|
||||||
|
--i;
|
||||||
|
std::list<var> new_list;
|
||||||
|
i->push_back(new_list);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
void pop_last_block_scope()
|
||||||
|
{
|
||||||
|
if(!scope_list.empty())
|
||||||
|
scope_list.pop_back();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
void pop_last_local_scope()
|
||||||
|
{
|
||||||
|
if(!scope_list.empty())
|
||||||
|
{
|
||||||
|
std::list<std::list<var> > temp=scope_list.back();
|
||||||
|
temp.pop_back();
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
balloon_scope scope;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
var a=1;
|
||||||
|
var sum=0;
|
||||||
|
while(a!=100)
|
||||||
|
{
|
||||||
|
sum+=a;
|
||||||
|
a+=1;
|
||||||
|
}
|
||||||
|
print(sum);
|
|
@ -86,9 +86,7 @@ int main()
|
||||||
pas.get_detail_token_stream(lex.get_detail_token());
|
pas.get_detail_token_stream(lex.get_detail_token());
|
||||||
pas.parse_main();
|
pas.parse_main();
|
||||||
if(!pas.get_error())
|
if(!pas.get_error())
|
||||||
{
|
|
||||||
pas.run_tree();
|
pas.run_tree();
|
||||||
}
|
|
||||||
else
|
else
|
||||||
std::cout<<">>[Parse] error(s) found,stop."<<std::endl;
|
std::cout<<">>[Parse] error(s) found,stop."<<std::endl;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,19 @@
|
||||||
|
var a=1;
|
||||||
|
var b=2;
|
||||||
|
var c="string";
|
||||||
|
var d;
|
||||||
|
var e=[];
|
||||||
|
var f={};
|
||||||
|
var g=[0,'str',[],{}];
|
||||||
|
var h={
|
||||||
|
va1:1,
|
||||||
|
va2:"str",
|
||||||
|
va3:'s',
|
||||||
|
va4:[],
|
||||||
|
va5:{fuck:0}
|
||||||
|
};
|
||||||
|
a=2;
|
||||||
|
var i=func(v1,v2,v3){return [0,0,0];};
|
||||||
|
1073741824;
|
||||||
|
"str";
|
||||||
|
a;
|
Loading…
Reference in New Issue