Add runtime & var
This commit is contained in:
parent
4915b2bbf6
commit
fbaea6b4bd
|
@ -71,6 +71,23 @@ class abstract_syntax_tree
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
void run_tree()
|
||||||
|
{
|
||||||
|
if(!children.empty())
|
||||||
|
{
|
||||||
|
for(auto i=children.begin();i!=children.end();++i)
|
||||||
|
{
|
||||||
|
switch(i->ast_node_type)
|
||||||
|
{
|
||||||
|
case __number:std::cout<<i->var_number<<std::endl;break;
|
||||||
|
case __string:std::cout<<i->var_string<<std::endl;break;
|
||||||
|
case __id:std::cout<<i->var_name<<std::endl;break;
|
||||||
|
default:i->run_tree();break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
void set_node_type(const int type)
|
void set_node_type(const int type)
|
||||||
{
|
{
|
||||||
ast_node_type=type;
|
ast_node_type=type;
|
||||||
|
@ -164,15 +181,15 @@ class abstract_syntax_tree
|
||||||
children.push_back(p);
|
children.push_back(p);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
double return_var_number()
|
double get_var_number()
|
||||||
{
|
{
|
||||||
return var_number;
|
return var_number;
|
||||||
}
|
}
|
||||||
std::string return_var_string()
|
std::string get_var_string()
|
||||||
{
|
{
|
||||||
return var_string;
|
return var_string;
|
||||||
}
|
}
|
||||||
std::string return_var_name()
|
std::string get_var_name()
|
||||||
{
|
{
|
||||||
return var_name;
|
return var_name;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ int main()
|
||||||
resource_programme_process prog;
|
resource_programme_process prog;
|
||||||
nasal_lexer lex;
|
nasal_lexer lex;
|
||||||
nasal_parser pas;
|
nasal_parser pas;
|
||||||
|
runtime rt;
|
||||||
std::string command;
|
std::string command;
|
||||||
std::cout<<">> Nasal interpreter by ValKmjolnir"<<std::endl;
|
std::cout<<">> Nasal interpreter by ValKmjolnir"<<std::endl;
|
||||||
std::cout<<">> Input [help] to find help."<<std::endl;
|
std::cout<<">> Input [help] to find help."<<std::endl;
|
||||||
|
@ -61,6 +62,8 @@ int main()
|
||||||
pas.parse_main_work();
|
pas.parse_main_work();
|
||||||
if(!pas.get_error_num())
|
if(!pas.get_error_num())
|
||||||
pas.print_generated_ast();
|
pas.print_generated_ast();
|
||||||
|
else
|
||||||
|
std::cout<<">>[Abstract_syntax_tree] error(s) occurred,stop."<<std::endl;
|
||||||
}
|
}
|
||||||
else if(command=="run")
|
else if(command=="run")
|
||||||
{
|
{
|
||||||
|
@ -69,9 +72,9 @@ int main()
|
||||||
pas.parse_process(lex.return_list());
|
pas.parse_process(lex.return_list());
|
||||||
pas.parse_main_work();
|
pas.parse_main_work();
|
||||||
if(!pas.get_error_num())
|
if(!pas.get_error_num())
|
||||||
{
|
rt.run_abstract_syntax_tree(pas.get_tree());
|
||||||
;
|
else
|
||||||
}
|
std::cout<<">>[Runtime] error(s) occurred,stop."<<std::endl;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
prog.input_file(command);
|
prog.input_file(command);
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
|
|
||||||
#include "nasal_token_type.h"
|
#include "nasal_token_type.h"
|
||||||
#include "ast.h"
|
#include "ast.h"
|
||||||
|
#include "nasal_runtime.h"
|
||||||
#include "nasal_lexer.h"
|
#include "nasal_lexer.h"
|
||||||
#include "nasal_parser.h"
|
#include "nasal_parser.h"
|
||||||
|
|
||||||
|
|
|
@ -31,6 +31,10 @@ class nasal_parser
|
||||||
{
|
{
|
||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
abstract_syntax_tree& get_tree()
|
||||||
|
{
|
||||||
|
return root;
|
||||||
|
}
|
||||||
void print_generated_ast()
|
void print_generated_ast()
|
||||||
{
|
{
|
||||||
std::cout<<">>[Abstract-syntax-tree]"<<std::endl;
|
std::cout<<">>[Abstract-syntax-tree]"<<std::endl;
|
||||||
|
|
|
@ -0,0 +1,168 @@
|
||||||
|
#ifndef __NASAL_RUNTIME_H__
|
||||||
|
#define __NASAL_RUNTIME_H__
|
||||||
|
#include "ast.h"
|
||||||
|
|
||||||
|
class var
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
int type;
|
||||||
|
std::string name;
|
||||||
|
double number;
|
||||||
|
std::string str;
|
||||||
|
std::list<var> var_list;
|
||||||
|
std::list<var> var_hash;
|
||||||
|
abstract_syntax_tree function;
|
||||||
|
public:
|
||||||
|
var()
|
||||||
|
{
|
||||||
|
type=0;
|
||||||
|
name="";
|
||||||
|
number=0;
|
||||||
|
str="";
|
||||||
|
function.set_clear();
|
||||||
|
}
|
||||||
|
var(const var& p)
|
||||||
|
{
|
||||||
|
type=p.type;
|
||||||
|
name=p.name;
|
||||||
|
number=p.number;
|
||||||
|
str=p.str;
|
||||||
|
function=p.function;
|
||||||
|
}
|
||||||
|
var& operator=(const var& p)
|
||||||
|
{
|
||||||
|
type=p.type;
|
||||||
|
name=p.name;
|
||||||
|
number=p.number;
|
||||||
|
str=p.str;
|
||||||
|
function=p.function;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
void set_type(const int var_type)
|
||||||
|
{
|
||||||
|
type=var_type;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
void set_name(std::string& var_name_str)
|
||||||
|
{
|
||||||
|
name=var_name_str;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
void set_number(const double var_number)
|
||||||
|
{
|
||||||
|
number=var_number;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
void set_string(std::string& s)
|
||||||
|
{
|
||||||
|
str=s;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
void set_function(abstract_syntax_tree& func)
|
||||||
|
{
|
||||||
|
function=func;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
void set_list(std::list<var>& p)
|
||||||
|
{
|
||||||
|
var_list=p;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
void append_list(var& p)
|
||||||
|
{
|
||||||
|
var_list.push_back(p);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
void set_hash(std::list<var>& p)
|
||||||
|
{
|
||||||
|
var_hash=p;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
void append_hash(var& p)
|
||||||
|
{
|
||||||
|
var_hash.push_back(p);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
std::string get_name()
|
||||||
|
{
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
double get_number()
|
||||||
|
{
|
||||||
|
return number;
|
||||||
|
}
|
||||||
|
std::string get_string()
|
||||||
|
{
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
abstract_syntax_tree get_function()
|
||||||
|
{
|
||||||
|
return function;
|
||||||
|
}
|
||||||
|
std::list<var>& get_list()
|
||||||
|
{
|
||||||
|
return var_list;
|
||||||
|
}
|
||||||
|
std::list<var>& get_hash()
|
||||||
|
{
|
||||||
|
return var_hash;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class var_list_manager
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
std::list<var> var_list;
|
||||||
|
public:
|
||||||
|
var_list_manager()
|
||||||
|
{
|
||||||
|
var_list.clear();
|
||||||
|
var null_var;
|
||||||
|
var_list.push_back(null_var);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
void add_var(var& tmp)
|
||||||
|
{
|
||||||
|
for(auto i=var_list.begin();i!=var_list.end();++i)
|
||||||
|
if(i->get_name()==tmp.get_name())
|
||||||
|
{
|
||||||
|
std::cout<<">>[Runtime-error] redeclaration of '"<<tmp.get_name()<<"'."<<std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var_list.push_back(tmp);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
void del_latest_var()
|
||||||
|
{
|
||||||
|
var_list.pop_back();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var& search_var(std::string& str)
|
||||||
|
{
|
||||||
|
for(auto i=var_list.begin();i!=var_list.end();++i)
|
||||||
|
if(i->get_name()==str)
|
||||||
|
return *i;
|
||||||
|
std::cout<<">>[Runtime-error] '"<<str<<"' is not delclared in this scope."<<std::endl;
|
||||||
|
return *var_list.begin();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class runtime
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
abstract_syntax_tree root;
|
||||||
|
public:
|
||||||
|
runtime()
|
||||||
|
{
|
||||||
|
root.set_clear();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
void run_abstract_syntax_tree(abstract_syntax_tree& tmp_tree)
|
||||||
|
{
|
||||||
|
root.set_clear();
|
||||||
|
root=tmp_tree;
|
||||||
|
root.run_tree();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
#endif
|
Loading…
Reference in New Issue