Initial reworking of chaiscript parser to use the new chaioop parser

This commit is contained in:
Jonathan Turner
2009-06-30 18:17:15 +00:00
parent 2f27dc55fc
commit ee44ae0ca0
5 changed files with 1337 additions and 286 deletions

View File

@@ -8,40 +8,8 @@
namespace chaiscript
{
struct ParserError {
std::string reason;
langkit::TokenPtr location;
ParserError(const std::string &why, const langkit::TokenPtr where) : reason(why), location(where){ }
};
struct EvalError : public std::runtime_error {
std::string reason;
langkit::TokenPtr location;
EvalError(const std::string &why, const langkit::TokenPtr where)
: std::runtime_error("Eval error: \"" + why + "\" in '"
+ where->filename + "' line: " + boost::lexical_cast<std::string>(where->start.line+1)),
reason(why), location(where) { }
virtual ~EvalError() throw() {}
};
struct ReturnValue {
dispatchkit::Boxed_Value retval;
langkit::TokenPtr location;
ReturnValue(const dispatchkit::Boxed_Value &return_value, const langkit::TokenPtr where) : retval(return_value), location(where) { }
};
struct BreakLoop {
langkit::TokenPtr location;
BreakLoop(const langkit::TokenPtr where) : location(where) { }
};
template <typename Eval_System>
const dispatchkit::Boxed_Value eval_function (Eval_System &ss, langkit::TokenPtr node, const std::vector<std::string> &param_names, const std::vector<dispatchkit::Boxed_Value> &vals) {
const dispatchkit::Boxed_Value eval_function (Eval_System &ss, TokenPtr node, const std::vector<std::string> &param_names, const std::vector<dispatchkit::Boxed_Value> &vals) {
ss.new_scope();
for (unsigned int i = 0; i < param_names.size(); ++i) {
@@ -61,18 +29,17 @@ namespace chaiscript
}
template <typename Eval_System>
dispatchkit::Boxed_Value eval_token(Eval_System &ss, langkit::TokenPtr node) {
dispatchkit::Boxed_Value eval_token(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval;
unsigned int i, j;
switch (node->identifier) {
case (TokenType::Value) :
case (TokenType::File) :
case (Token_Type::File) :
for (i = 0; i < node->children.size(); ++i) {
retval = eval_token(ss, node->children[i]);
}
break;
case (TokenType::Identifier) :
case (Token_Type::Id) :
if (node->text == "true") {
retval = dispatchkit::Boxed_Value(true);
}
@@ -88,19 +55,19 @@ namespace chaiscript
}
}
break;
case (TokenType::Real_Number) :
case (Token_Type::Float) :
retval = dispatchkit::Boxed_Value(double(atof(node->text.c_str())));
break;
case (TokenType::Integer) :
case (Token_Type::Int) :
retval = dispatchkit::Boxed_Value(atoi(node->text.c_str()));
break;
case (TokenType::Quoted_String) :
case (Token_Type::Quoted_String) :
retval = dispatchkit::Boxed_Value(node->text);
break;
case (TokenType::Single_Quoted_String) :
case (Token_Type::Single_Quoted_String) :
retval = dispatchkit::Boxed_Value(node->text);
break;
case (TokenType::Equation) :
case (Token_Type::Equation) :
retval = eval_token(ss, node->children.back());
if (node->children.size() > 1) {
for (i = node->children.size()-3; ((int)i) >= 0; i -= 2) {
@@ -116,16 +83,15 @@ namespace chaiscript
}
}
break;
case (TokenType::Variable_Decl): {
case (Token_Type::Var_Decl): {
ss.add_object(node->children[0]->text, dispatchkit::Boxed_Value());
retval = ss.get_object(node->children[0]->text);
}
break;
case (TokenType::Factor) :
case (TokenType::Expression) :
case (TokenType::Term) :
case (TokenType::Boolean) :
case (TokenType::Comparison) : {
case (Token_Type::Comparison) :
case (Token_Type::Expression) :
case (Token_Type::Additive) :
case (Token_Type::Multiplicative) : {
retval = eval_token(ss, node->children[0]);
if (node->children.size() > 1) {
for (i = 1; i < node->children.size(); i += 2) {
@@ -143,7 +109,7 @@ namespace chaiscript
}
}
break;
case (TokenType::Array_Call) : {
case (Token_Type::Array_Call) : {
retval = eval_token(ss, node->children[0]);
for (i = 1; i < node->children.size(); ++i) {
dispatchkit::Param_List_Builder plb;
@@ -161,8 +127,8 @@ namespace chaiscript
}
}
break;
case (TokenType::Negate) : {
retval = eval_token(ss, node->children[1]);
case (Token_Type::Negate) : {
retval = eval_token(ss, node->children[0]);
dispatchkit::Param_List_Builder plb;
plb << retval;
plb << dispatchkit::Boxed_Value(-1);
@@ -175,10 +141,10 @@ namespace chaiscript
}
}
break;
case (TokenType::Not) : {
case (Token_Type::Not) : {
bool cond;
try {
retval = eval_token(ss, node->children[1]);
retval = eval_token(ss, node->children[0]);
cond = dispatchkit::boxed_cast<bool &>(retval);
}
catch (std::exception) {
@@ -187,7 +153,8 @@ namespace chaiscript
retval = dispatchkit::Boxed_Value(!cond);
}
break;
case (TokenType::Prefix) : {
/*
case (Token_Type::Prefix) : {
retval = eval_token(ss, node->children[1]);
dispatchkit::Param_List_Builder plb;
plb << retval;
@@ -200,16 +167,19 @@ namespace chaiscript
}
}
break;
case (TokenType::Array_Init) : {
*/
case (Token_Type::Inline_Array) : {
try {
retval = dispatch(ss.get_function("Vector"), dispatchkit::Param_List_Builder());
for (i = 0; i < node->children.size(); ++i) {
try {
dispatchkit::Boxed_Value tmp = eval_token(ss, node->children[i]);
dispatch(ss.get_function("push_back"), dispatchkit::Param_List_Builder() << retval << tmp);
}
catch (const dispatchkit::dispatch_error &inner_e) {
throw EvalError("Can not find appropriate 'push_back'", node->children[i]);
if (node->children.size() > 0) {
for (i = 0; i < node->children[0]->children.size(); ++i) {
try {
dispatchkit::Boxed_Value tmp = eval_token(ss, node->children[0]->children[i]);
dispatch(ss.get_function("push_back"), dispatchkit::Param_List_Builder() << retval << tmp);
}
catch (const dispatchkit::dispatch_error &inner_e) {
throw EvalError("Can not find appropriate 'push_back'", node->children[0]->children[i]);
}
}
}
}
@@ -218,6 +188,7 @@ namespace chaiscript
}
}
break;
/*
case (TokenType::Map_Init) : {
try {
retval = dispatch(ss.get_function("Map"), dispatchkit::Param_List_Builder());
@@ -237,7 +208,9 @@ namespace chaiscript
}
}
break;
case (TokenType::Fun_Call) : {
*/
case (Token_Type::Fun_Call) : {
dispatchkit::Param_List_Builder plb;
std::vector<std::pair<std::string, dispatchkit::Dispatch_Engine::Function_Map::mapped_type> > fn;
dispatchkit::Dispatch_Engine::Stack prev_stack = ss.get_stack();
@@ -245,9 +218,10 @@ namespace chaiscript
dispatchkit::Dispatch_Engine::Stack new_stack;
new_stack.push_back(dispatchkit::Dispatch_Engine::Scope());
dispatchkit::Param_List_Builder plb;
for (i = 1; i < node->children.size(); ++i) {
plb << eval_token(ss, node->children[i]);
if ((node->children.size() > 1) && (node->children[1]->identifier == Token_Type::Arg_List)) {
for (i = 0; i < node->children[1]->children.size(); ++i) {
plb << eval_token(ss, node->children[1]->children[i]);
}
}
try {
fn = ss.get_function(node->children[0]->text);
@@ -269,7 +243,8 @@ namespace chaiscript
}
}
break;
case (TokenType::Method_Call) : {
case (Token_Type::Dot_Access) : {
std::vector<std::pair<std::string, dispatchkit::Dispatch_Engine::Function_Map::mapped_type> > fn;
dispatchkit::Dispatch_Engine::Stack prev_stack = ss.get_stack();
@@ -282,12 +257,15 @@ namespace chaiscript
dispatchkit::Param_List_Builder plb;
plb << retval;
for (j = 1; j < node->children[i]->children.size(); ++j) {
plb << eval_token(ss, node->children[i]->children[j]);
if (node->children[i]->children.size() > 1) {
//std::cout << "size: " << node->children[i]->children.size() << std::endl;
for (j = 0; j < node->children[i]->children[1]->children.size(); ++j) {
plb << eval_token(ss, node->children[i]->children[1]->children[j]);
}
}
std::string fun_name;
if (node->children[i]->identifier == TokenType::Fun_Call) {
if (node->children[i]->identifier == Token_Type::Fun_Call) {
fun_name = node->children[i]->children[0]->text;
}
else {
@@ -316,7 +294,8 @@ namespace chaiscript
}
}
break;
case(TokenType::If_Block) : {
case(Token_Type::If) : {
retval = eval_token(ss, node->children[0]);
bool cond;
try {
@@ -354,7 +333,7 @@ namespace chaiscript
}
}
break;
case(TokenType::While_Block) : {
case(Token_Type::While) : {
retval = eval_token(ss, node->children[0]);
bool cond;
try {
@@ -381,6 +360,7 @@ namespace chaiscript
retval = dispatchkit::Boxed_Value();
}
break;
/*
case(TokenType::For_Block) : {
dispatchkit::Boxed_Value condition;
bool cond;
@@ -423,31 +403,37 @@ namespace chaiscript
retval = dispatchkit::Boxed_Value();
}
break;
case (TokenType::Function_Def) : {
unsigned int num_args = node->children.size() - 2;
*/
case (Token_Type::Def) : {
std::vector<std::string> param_names;
for (i = 0; i < num_args; ++i) {
param_names.push_back(node->children[i+1]->text);
if ((node->children.size() > 1) && (node->children[1]->identifier == Token_Type::Arg_List)) {
for (i = 0; i < node->children[1]->children.size(); ++i) {
param_names.push_back(node->children[1]->children[i]->text);
}
}
ss.register_function(boost::shared_ptr<dispatchkit::Proxy_Function>(
new dispatchkit::Dynamic_Proxy_Function(boost::bind(&eval_function<Eval_System>, boost::ref(ss), node->children.back(), param_names, _1), num_args)), node->children[0]->text);
new dispatchkit::Dynamic_Proxy_Function(boost::bind(&eval_function<Eval_System>, boost::ref(ss), node->children.back(), param_names, _1), node->children[1]->children.size())), node->children[0]->text);
}
break;
case (TokenType::Lambda_Def) : {
unsigned int num_args = node->children.size() - 1;
case (Token_Type::Lambda) : {
std::vector<std::string> param_names;
for (i = 0; i < num_args; ++i) {
param_names.push_back(node->children[i]->text);
if ((node->children.size() > 0) && (node->children[0]->identifier == Token_Type::Arg_List)) {
for (i = 0; i < node->children[0]->children.size(); ++i) {
param_names.push_back(node->children[0]->children[i]->text);
}
}
//retval = boost::shared_ptr<dispatchkit::Proxy_Function>(new dispatchkit::Proxy_Function_Impl<boost::function<void (const std::string &)> >(&test));
retval = dispatchkit::Boxed_Value(boost::shared_ptr<dispatchkit::Proxy_Function>(
new dispatchkit::Dynamic_Proxy_Function(
boost::bind(&eval_function<Eval_System>, boost::ref(ss), node->children.back(), param_names, _1), num_args)));
boost::bind(&eval_function<Eval_System>, boost::ref(ss), node->children.back(), param_names, _1), node->children[0]->children.size())));
}
break;
case (TokenType::Scoped_Block) : {
case (Token_Type::Block) : {
ss.new_scope();
for (i = 0; i < node->children.size(); ++i) {
retval = eval_token(ss, node->children[i]);
@@ -455,7 +441,8 @@ namespace chaiscript
ss.pop_scope();
}
break;
case (TokenType::Return) : {
case (Token_Type::Return) : {
if (node->children.size() > 0) {
retval = eval_token(ss, node->children[0]);
}
@@ -465,6 +452,7 @@ namespace chaiscript
throw ReturnValue(retval, node);
}
break;
/*
case (TokenType::Break) : {
throw BreakLoop(node);
}
@@ -483,6 +471,7 @@ namespace chaiscript
case (TokenType::Curly_Close) :
case (TokenType::Comma) :
break;
*/
}
return retval;