forked from xxq250/Nasal-Interpreter
misc
This commit is contained in:
167
misc/ast.cpp
Normal file
167
misc/ast.cpp
Normal file
@@ -0,0 +1,167 @@
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <list>
|
||||
|
||||
class abstract_syntax_tree
|
||||
{
|
||||
private:
|
||||
int ast_node_type;
|
||||
double var_number;
|
||||
std::string var_string;
|
||||
std::string var_name;
|
||||
std::list<abstract_syntax_tree> children;
|
||||
public:
|
||||
abstract_syntax_tree()
|
||||
{
|
||||
ast_node_type=0;
|
||||
var_number=0;
|
||||
var_string="";
|
||||
var_name="";
|
||||
children.clear();
|
||||
return;
|
||||
}
|
||||
abstract_syntax_tree(const abstract_syntax_tree& p)
|
||||
{
|
||||
ast_node_type=p.ast_node_type;
|
||||
var_number=p.var_number;
|
||||
var_string=p.var_string;
|
||||
var_name=p.var_name;
|
||||
children=p.children;
|
||||
return;
|
||||
}
|
||||
abstract_syntax_tree& operator=(const abstract_syntax_tree& p)
|
||||
{
|
||||
ast_node_type=p.ast_node_type;
|
||||
var_number=p.var_number;
|
||||
var_string=p.var_string;
|
||||
var_name=p.var_name;
|
||||
children=p.children;
|
||||
return *this;
|
||||
}
|
||||
void print_tree(const int n)
|
||||
{
|
||||
std::cout<<n<<" ";
|
||||
if(!children.empty())
|
||||
{
|
||||
for(auto i=children.begin();i!=children.end();++i)
|
||||
i->print_tree(n+1);
|
||||
}
|
||||
return;
|
||||
}
|
||||
void set_node_type(const int type)
|
||||
{
|
||||
ast_node_type=type;
|
||||
return;
|
||||
}
|
||||
void set_var_string(std::string& str)
|
||||
{
|
||||
var_string=str;
|
||||
return;
|
||||
}
|
||||
void set_var_number(std::string& str)
|
||||
{
|
||||
if(str=="nil")
|
||||
{
|
||||
var_number=0;
|
||||
return;
|
||||
}
|
||||
if((int)str.length()>2 && (str[1]=='x' || str[1]=='o'))
|
||||
{
|
||||
if(str[1]=='x')
|
||||
{
|
||||
int num=0;
|
||||
int pw=1;
|
||||
for(int i=(int)str.length()-1;i>1;--i)
|
||||
{
|
||||
if('0'<=str[i] && str[i]<='9')
|
||||
num+=(str[i]-'0')*pw;
|
||||
else if('a'<=str[i] && str[i]<='f')
|
||||
num+=(10+str[i]-'a')*pw;
|
||||
else if('A'<=str[i] && str[i]<='F')
|
||||
num+=(10+str[i]-'A')*pw;
|
||||
pw<<=4;
|
||||
}
|
||||
var_number=(double)num;
|
||||
}
|
||||
else
|
||||
{
|
||||
int num=0;
|
||||
int pw=1;
|
||||
for(int i=(int)str.length()-1;i>1;--i)
|
||||
{
|
||||
num+=(str[i]-'0')*pw;
|
||||
pw<<=3;
|
||||
}
|
||||
var_number=(double)num;
|
||||
}
|
||||
return;
|
||||
}
|
||||
int dot_place=-1;
|
||||
for(int i=0;i<(int)str.length();++i)
|
||||
if(str[i]=='.')
|
||||
{
|
||||
dot_place=i;
|
||||
break;
|
||||
}
|
||||
if(dot_place==-1)
|
||||
{
|
||||
var_number=0;
|
||||
double pw=1;
|
||||
for(int i=(int)str.length()-1;i>=0;--i)
|
||||
{
|
||||
var_number+=(str[i]-'0')*pw;
|
||||
pw*=10;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var_number=0;
|
||||
double pw=0.1;
|
||||
for(int i=dot_place+1;i<(int)str.length();++i)
|
||||
{
|
||||
var_number+=(str[i]-'0')*pw;
|
||||
pw/=10;
|
||||
}
|
||||
pw=1;
|
||||
for(int i=dot_place-1;i>=0;--i)
|
||||
{
|
||||
var_number+=(str[i]-'0')*pw;
|
||||
pw*=10;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
void set_var_name(std::string& str)
|
||||
{
|
||||
var_name=str;
|
||||
return;
|
||||
}
|
||||
void add_child(abstract_syntax_tree& p)
|
||||
{
|
||||
children.push_back(p);
|
||||
return;
|
||||
}
|
||||
double return_var_number()
|
||||
{
|
||||
return var_number;
|
||||
}
|
||||
std::string return_var_string()
|
||||
{
|
||||
return var_string;
|
||||
}
|
||||
std::string return_var_name()
|
||||
{
|
||||
return var_name;
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
abstract_syntax_tree a,b;
|
||||
a.add_child(a);
|
||||
a.add_child(a);
|
||||
a.add_child(a);
|
||||
b=a;
|
||||
b.print_tree(1);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user