add test file 'turingmachine.nas' & change output format of ast & bug fixed
This commit is contained in:
parent
70a43c2f03
commit
46716620e3
1
makefile
1
makefile
|
@ -29,5 +29,6 @@ test:nasal
|
||||||
./nasal -t test/quick_sort.nas
|
./nasal -t test/quick_sort.nas
|
||||||
./nasal test/scalar.nas
|
./nasal test/scalar.nas
|
||||||
./nasal test/trait.nas
|
./nasal test/trait.nas
|
||||||
|
./nasal -t test/turingmachine.nas
|
||||||
./nasal -t test/ycombinator.nas
|
./nasal -t test/ycombinator.nas
|
||||||
|
|
21
nasal_ast.h
21
nasal_ast.h
|
@ -138,7 +138,7 @@ public:
|
||||||
nasal_ast(const uint32_t l=0,const uint32_t t=ast_null):_line(l),_type(t){}
|
nasal_ast(const uint32_t l=0,const uint32_t t=ast_null):_line(l),_type(t){}
|
||||||
nasal_ast(const nasal_ast&);
|
nasal_ast(const nasal_ast&);
|
||||||
nasal_ast(nasal_ast&&);
|
nasal_ast(nasal_ast&&);
|
||||||
void print(const int);
|
void print(int,bool);
|
||||||
void clear();
|
void clear();
|
||||||
|
|
||||||
nasal_ast& operator=(const nasal_ast&);
|
nasal_ast& operator=(const nasal_ast&);
|
||||||
|
@ -209,10 +209,11 @@ void nasal_ast::clear()
|
||||||
_child.clear();
|
_child.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void nasal_ast::print(const int depth)
|
void nasal_ast::print(int depth,bool last=false)
|
||||||
{
|
{
|
||||||
for(int i=0;i<depth;++i)
|
static std::vector<std::string> intentation={};
|
||||||
std::cout<<"| ";
|
for(auto& i:intentation)
|
||||||
|
std::cout<<i;
|
||||||
std::cout<<ast_name[_type];
|
std::cout<<ast_name[_type];
|
||||||
if(
|
if(
|
||||||
_type==ast_str ||
|
_type==ast_str ||
|
||||||
|
@ -224,8 +225,16 @@ void nasal_ast::print(const int depth)
|
||||||
else if(_type==ast_num || _type==ast_file)
|
else if(_type==ast_num || _type==ast_file)
|
||||||
std::cout<<":"<<_num;
|
std::cout<<":"<<_num;
|
||||||
std::cout<<'\n';
|
std::cout<<'\n';
|
||||||
for(auto& i:_child)
|
if(last && depth)
|
||||||
i.print(depth+1);
|
intentation.back()=" ";
|
||||||
|
else if(!last && depth)
|
||||||
|
intentation.back()="│ ";
|
||||||
|
for(uint32_t i=0;i<_child.size();++i)
|
||||||
|
{
|
||||||
|
intentation.push_back(i==_child.size()-1?"└─":"├─");
|
||||||
|
_child[i].print(depth+1,i==_child.size()-1);
|
||||||
|
intentation.pop_back();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -83,7 +83,7 @@ void nasal_dbg::stepinfo()
|
||||||
uint32_t begin,end;
|
uint32_t begin,end;
|
||||||
uint32_t line=bytecode[pc].line==0?0:bytecode[pc].line-1;
|
uint32_t line=bytecode[pc].line==0?0:bytecode[pc].line-1;
|
||||||
src.load(files[bytecode[pc].fidx]);
|
src.load(files[bytecode[pc].fidx]);
|
||||||
printf("source code:\n");
|
printf("\nsource code:\n");
|
||||||
begin=(line>>3)==0?0:((line>>3)<<3);
|
begin=(line>>3)==0?0:((line>>3)<<3);
|
||||||
end=(1+(line>>3))<<3;
|
end=(1+(line>>3))<<3;
|
||||||
for(uint32_t i=begin;i<end && i<src.size();++i)
|
for(uint32_t i=begin;i<end && i<src.size();++i)
|
||||||
|
|
|
@ -48,14 +48,14 @@ public:
|
||||||
void err(const char* stage,const std::string& info)
|
void err(const char* stage,const std::string& info)
|
||||||
{
|
{
|
||||||
++error;
|
++error;
|
||||||
std::cout<<"["<<stage<<"] "<<file<<": "<<info<<'\n';
|
std::cout<<"["<<stage<<"] "<<info<<'\n';
|
||||||
}
|
}
|
||||||
void err(const char* stage,uint32_t line,uint32_t column,const std::string& info)
|
void err(const char* stage,uint32_t line,uint32_t column,const std::string& info)
|
||||||
{
|
{
|
||||||
++error;
|
++error;
|
||||||
if(!line)
|
if(!line)
|
||||||
{
|
{
|
||||||
err(stage,info);
|
std::cout<<"["<<stage<<"] "<<file<<": "<<info<<'\n';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
std::cout<<"["<<stage<<"] "<<file<<":"<<line<<":"<<column<<" "<<info<<"\n"<<res[line-1]<<'\n';
|
std::cout<<"["<<stage<<"] "<<file<<":"<<line<<":"<<column<<" "<<info<<"\n"<<res[line-1]<<'\n';
|
||||||
|
@ -68,7 +68,7 @@ public:
|
||||||
++error;
|
++error;
|
||||||
if(!line)
|
if(!line)
|
||||||
{
|
{
|
||||||
err(stage,info);
|
std::cout<<"["<<stage<<"] "<<file<<": "<<info<<'\n';
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
std::cout<<"["<<stage<<"] "<<file<<":"<<line<<" "<<info<<"\n"<<res[line-1]<<'\n';
|
std::cout<<"["<<stage<<"] "<<file<<":"<<line<<" "<<info<<"\n"<<res[line-1]<<'\n';
|
||||||
|
|
|
@ -0,0 +1,51 @@
|
||||||
|
import("lib.nas");
|
||||||
|
|
||||||
|
var table=[
|
||||||
|
['q0','0','1','R','q1'],
|
||||||
|
['q1','1','1','R','q1'],
|
||||||
|
['q1','0','0','S','q2'],
|
||||||
|
['q2','0','1','R','q3'],
|
||||||
|
['q3',' ',' ','S','q3']
|
||||||
|
];
|
||||||
|
var prt=func(state,pointer,paper,act=nil){
|
||||||
|
print(state,':',pointer,':',act!=nil?act:'','\n\t');
|
||||||
|
var s='';
|
||||||
|
foreach(var i;paper)
|
||||||
|
s~=i;
|
||||||
|
s~='\n\t';
|
||||||
|
for(var i=0;i<pointer;i+=1)
|
||||||
|
for(var j=0;j<size(paper[i]);j+=1)
|
||||||
|
s~=' ';
|
||||||
|
print(s,'^\n');
|
||||||
|
}
|
||||||
|
var run=func(table,node,start,stop){
|
||||||
|
var paper=['0','1','0','1','0','a'];
|
||||||
|
var pointer=0;
|
||||||
|
foreach(var action;table){
|
||||||
|
if(!contains(node,action[0]))
|
||||||
|
node[action[0]]=nil;
|
||||||
|
if(!contains(node,action[4]))
|
||||||
|
node[action[4]]=nil;
|
||||||
|
}
|
||||||
|
print("nodes: ",keys(node),'\n');
|
||||||
|
if(!contains(node,start))
|
||||||
|
die(start~" is not a valid node");
|
||||||
|
if(!contains(node,stop))
|
||||||
|
die(stop~" is not a valid node");
|
||||||
|
var state=start;
|
||||||
|
prt(state,pointer,paper);
|
||||||
|
while(state!=stop){
|
||||||
|
foreach(var action;table)
|
||||||
|
if(action[0]==state and (action[1]==paper[pointer] or action[1]==' ')){
|
||||||
|
paper[pointer]=action[2]==' '?paper[pointer]:action[2];
|
||||||
|
if(action[3]=='L') pointer-=1;
|
||||||
|
elsif(action[3]=='R') pointer+=1;
|
||||||
|
elsif(action[3]!='S') die("invalid action <"~action[3]~'>');
|
||||||
|
state=action[4];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
prt(state,pointer,paper,action);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
run(table,{},'q0','q3');
|
Loading…
Reference in New Issue