Update
This commit is contained in:
parent
851602ed9d
commit
305dedfdc1
|
@ -2,6 +2,13 @@
|
|||
#define __ABSTRACT_SYNTAX_TREE_CPP__
|
||||
|
||||
int exit_type=0;
|
||||
|
||||
bool abstract_syntax_tree::check()
|
||||
{
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
var abstract_syntax_tree::call_id()
|
||||
{
|
||||
var temp;
|
||||
|
@ -71,6 +78,9 @@ void abstract_syntax_tree::run_root()
|
|||
std::string _name=j->name;
|
||||
if(!scope.search_var(_name))
|
||||
{
|
||||
++j;
|
||||
if(j!=i->children.end())
|
||||
new_var=j->get_value();
|
||||
new_var.set_name(_name);
|
||||
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;
|
||||
else if(i->type==__while)
|
||||
{
|
||||
;
|
||||
scope.add_new_block_scope();
|
||||
i->run_loop();
|
||||
scope.pop_last_block_scope();
|
||||
}
|
||||
else if(i->type==__ifelse)
|
||||
{
|
||||
;
|
||||
scope.add_new_block_scope();
|
||||
i->run_ifelse();
|
||||
scope.pop_last_block_scope();
|
||||
}
|
||||
if(exit_type!=__process_exited_successfully)
|
||||
break;
|
||||
|
@ -107,6 +121,26 @@ void abstract_syntax_tree::run_root()
|
|||
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()
|
||||
{
|
||||
scope.add_new_block_scope();
|
||||
|
@ -125,7 +159,4 @@ void abstract_syntax_tree::run_block(int block_type)
|
|||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
|
|
@ -191,9 +191,12 @@ class abstract_syntax_tree
|
|||
{
|
||||
return children;
|
||||
}
|
||||
bool check();
|
||||
var call_id();
|
||||
var get_value();
|
||||
void run_root();
|
||||
void run_loop();
|
||||
void run_ifelse();
|
||||
void run_func();
|
||||
void run_block(int);
|
||||
};
|
||||
|
|
|
@ -275,8 +275,9 @@ abstract_syntax_tree balloon_parse::definition()
|
|||
get_token();
|
||||
if(this_token.type!=__equal)
|
||||
{
|
||||
++error;
|
||||
std::cout<<">>[Parse-error] line "<<this_token.line<<": expect a \'=\' here."<<std::endl;
|
||||
++warning;
|
||||
std::cout<<">>[Parse-warning] line "<<this_token.line<<": every new var should be initialized."<<std::endl;
|
||||
parse.push(this_token);
|
||||
return new_node;
|
||||
}
|
||||
new_node.add_child(scalar());
|
||||
|
|
|
@ -4,34 +4,108 @@
|
|||
class balloon_scope
|
||||
{
|
||||
private:
|
||||
std::list<var> scope;
|
||||
var error_var;
|
||||
std::list<var> global;
|
||||
std::list<std::list<std::list<var> > > scope_list;
|
||||
public:
|
||||
void set_clear()
|
||||
{
|
||||
scope.clear();
|
||||
global.clear();
|
||||
scope_list.clear();
|
||||
return;
|
||||
}
|
||||
bool search_var(std::string name)
|
||||
{
|
||||
for(std::list<var>::iterator i=scope.begin();i!=scope.end();++i)
|
||||
if(!scope_list.empty())
|
||||
{
|
||||
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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
var& get_var(std::string name)
|
||||
{
|
||||
for(std::list<var>::iterator i=scope.begin();i!=scope.end();++i)
|
||||
if(!scope_list.empty())
|
||||
{
|
||||
std::list<std::list<var> >::iterator i=scope_list.back().end();
|
||||
--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;
|
||||
std::cout<<">>[Runtime-error] \'"<<name<<"\' was not declared in this scope."<<std::endl;
|
||||
}
|
||||
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
|
||||
|
|
|
@ -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.parse_main();
|
||||
if(!pas.get_error())
|
||||
{
|
||||
pas.run_tree();
|
||||
}
|
||||
else
|
||||
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