mirror of
https://github.com/ValKmjolnir/Nasal-Interpreter.git
synced 2026-07-26 12:59:05 +08:00
Update
This commit is contained in:
@@ -0,0 +1,65 @@
|
|||||||
|
#ifndef __ABSTRACT_SYNTAX_TREE_CPP__
|
||||||
|
#define __ABSTRACT_SYNTAX_TREE_CPP__
|
||||||
|
|
||||||
|
balloon_scope global;
|
||||||
|
var abstract_syntax_tree::get_value()
|
||||||
|
{
|
||||||
|
var temp;
|
||||||
|
temp.set_type(type);
|
||||||
|
if(type==__number)
|
||||||
|
temp.set_number(number);
|
||||||
|
else if(type==__string)
|
||||||
|
temp.set_string(str);
|
||||||
|
else if(type==__array)
|
||||||
|
{
|
||||||
|
var new_var;
|
||||||
|
}
|
||||||
|
else if(type==__hash)
|
||||||
|
{
|
||||||
|
var new_var;
|
||||||
|
}
|
||||||
|
else if(type==__function)
|
||||||
|
temp.set_function(*this);
|
||||||
|
if(name.length()!=0)
|
||||||
|
temp.set_name(name);
|
||||||
|
return temp;
|
||||||
|
}
|
||||||
|
void abstract_syntax_tree::run_root()
|
||||||
|
{
|
||||||
|
global.set_clear();
|
||||||
|
int beg_time,end_time;
|
||||||
|
int exit_type=__process_exited_successfully;
|
||||||
|
beg_time=time(NULL);
|
||||||
|
for(std::list<abstract_syntax_tree>::iterator i=children.begin();i!=children.end();++i)
|
||||||
|
{
|
||||||
|
if(i->type==__definition)
|
||||||
|
{
|
||||||
|
var new_var;
|
||||||
|
std::list<abstract_syntax_tree>::iterator j=i->children.begin();
|
||||||
|
std::string _name=j->name;
|
||||||
|
if(!global.search_var(_name))
|
||||||
|
{
|
||||||
|
new_var.set_name(_name);
|
||||||
|
global.add_var(new_var);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::cout<<">>[Runtime-error] redeclaration of \'"<<_name<<"\'."<<std::endl;
|
||||||
|
exit_type=__redeclaration;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end_time=time(NULL);
|
||||||
|
std::cout<<">>[Runtime] process exited after "<<end_time-beg_time<<" sec(s) with returned state \'";
|
||||||
|
print_exit_type(exit_type);
|
||||||
|
std::cout<<"\'."<<std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,9 +1,11 @@
|
|||||||
#ifndef __ABSTRACT_SYNTAX_TREE_H__
|
#ifndef __ABSTRACT_SYNTAX_TREE_H__
|
||||||
#define __ABSTRACT_SYNTAX_TREE_H__
|
#define __ABSTRACT_SYNTAX_TREE_H__
|
||||||
|
|
||||||
|
class var;
|
||||||
|
|
||||||
class abstract_syntax_tree
|
class abstract_syntax_tree
|
||||||
{
|
{
|
||||||
private:
|
protected:
|
||||||
int type;
|
int type;
|
||||||
double number;
|
double number;
|
||||||
std::string str;
|
std::string str;
|
||||||
@@ -189,6 +191,8 @@ class abstract_syntax_tree
|
|||||||
{
|
{
|
||||||
return children;
|
return children;
|
||||||
}
|
}
|
||||||
|
var get_value();
|
||||||
|
void run_root();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -6,11 +6,15 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <stack>
|
#include <stack>
|
||||||
|
#include <ctime>
|
||||||
|
|
||||||
#include "balloon_type.h"
|
#include "balloon_type.h"
|
||||||
#include "abstract_syntax_tree.h"
|
#include "abstract_syntax_tree.h"
|
||||||
#include "balloon_var.h"
|
#include "balloon_var.h"
|
||||||
#include "balloon_lexer.h"
|
#include "balloon_lexer.h"
|
||||||
#include "balloon_parse.h"
|
#include "balloon_parse.h"
|
||||||
|
#include "balloon_scope.h"
|
||||||
|
|
||||||
|
#include "abstract_syntax_tree.cpp"
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -75,6 +75,12 @@ class balloon_parse
|
|||||||
root.print_tree(1);
|
root.print_tree(1);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
void run_tree()
|
||||||
|
{
|
||||||
|
std::cout<<">>[Runtime] process begins at addr:"<<(void*)(&root)<<"."<<std::endl;
|
||||||
|
root.run_root();
|
||||||
|
return;
|
||||||
|
}
|
||||||
abstract_syntax_tree ret();
|
abstract_syntax_tree ret();
|
||||||
abstract_syntax_tree choose();
|
abstract_syntax_tree choose();
|
||||||
abstract_syntax_tree loop();
|
abstract_syntax_tree loop();
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
#ifndef __BALLOON_SCOPE_H__
|
||||||
|
#define __BALLOON_SCOPE_H__
|
||||||
|
|
||||||
|
class balloon_scope
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
std::list<var> scope;
|
||||||
|
var error_var;
|
||||||
|
public:
|
||||||
|
void set_clear()
|
||||||
|
{
|
||||||
|
scope.clear();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
bool search_var(std::string name)
|
||||||
|
{
|
||||||
|
for(std::list<var>::iterator i=scope.begin();i!=scope.end();++i)
|
||||||
|
if(i->get_name()==name)
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
void add_var(var t)
|
||||||
|
{
|
||||||
|
scope.push_back(t);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var& get_var(std::string name)
|
||||||
|
{
|
||||||
|
for(std::list<var>::iterator i=scope.begin();i!=scope.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;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -138,5 +138,23 @@ void print_detail_token(int type)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum runtime_error_type
|
||||||
|
{
|
||||||
|
__process_exited_successfully,
|
||||||
|
__redeclaration
|
||||||
|
};
|
||||||
|
|
||||||
|
void print_exit_type(int type)
|
||||||
|
{
|
||||||
|
std::string context;
|
||||||
|
switch(type)
|
||||||
|
{
|
||||||
|
case __process_exited_successfully: context="success";break;
|
||||||
|
case __redeclaration: context="redeclare";break;
|
||||||
|
default: context="unknown";break;
|
||||||
|
}
|
||||||
|
std::cout<<context;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -56,6 +56,7 @@ class var
|
|||||||
void set_name(std::string);
|
void set_name(std::string);
|
||||||
void set_number(double);
|
void set_number(double);
|
||||||
void set_string(std::string);
|
void set_string(std::string);
|
||||||
|
void set_function(abstract_syntax_tree&);
|
||||||
void append_array(var);
|
void append_array(var);
|
||||||
void append_hash(var);
|
void append_hash(var);
|
||||||
std::string get_name();
|
std::string get_name();
|
||||||
@@ -63,6 +64,7 @@ class var
|
|||||||
std::string get_string();
|
std::string get_string();
|
||||||
var& get_array_member(int);
|
var& get_array_member(int);
|
||||||
var& get_hash_member(std::string);
|
var& get_hash_member(std::string);
|
||||||
|
abstract_syntax_tree& get_function();
|
||||||
};
|
};
|
||||||
|
|
||||||
var error_var;
|
var error_var;
|
||||||
@@ -86,6 +88,11 @@ void var::set_string(std::string _str)
|
|||||||
str=_str;
|
str=_str;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
void var::set_function(abstract_syntax_tree& p)
|
||||||
|
{
|
||||||
|
function=p;
|
||||||
|
return;
|
||||||
|
}
|
||||||
void var::append_array(var _new_var)
|
void var::append_array(var _new_var)
|
||||||
{
|
{
|
||||||
balloon_array.push_back(_new_var);
|
balloon_array.push_back(_new_var);
|
||||||
@@ -130,4 +137,8 @@ var& var::get_hash_member(std::string _name)
|
|||||||
std::cout<<">>[Runtime-error] hash \'"<<name<<"\' does not have a member named \'"<<_name<<"\'"<<std::endl;
|
std::cout<<">>[Runtime-error] hash \'"<<name<<"\' does not have a member named \'"<<_name<<"\'"<<std::endl;
|
||||||
return error_var;
|
return error_var;
|
||||||
}
|
}
|
||||||
|
abstract_syntax_tree& var::get_function()
|
||||||
|
{
|
||||||
|
return function;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -83,6 +83,12 @@ 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())
|
||||||
|
{
|
||||||
|
pas.run_tree();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
std::cout<<">>[Parse] error(s) found,stop."<<std::endl;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
std::cout<<">>[Lexer] error(s) found,stop."<<std::endl;
|
std::cout<<">>[Lexer] error(s) found,stop."<<std::endl;
|
||||||
|
|||||||
Reference in New Issue
Block a user