Improved eval and error handling
This commit is contained in:
@@ -85,7 +85,22 @@ std::string concat_string(const std::string &s1, const std::string &s2) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const Boxed_Value add_two(BoxedCPP_System &ss, const std::vector<Boxed_Value> &vals) {
|
const Boxed_Value add_two(BoxedCPP_System &ss, const std::vector<Boxed_Value> &vals) {
|
||||||
return dispatch(ss.get_function("+"), vals);
|
return dispatch(ss.get_function("+"), vals);
|
||||||
|
}
|
||||||
|
|
||||||
|
const Boxed_Value eval(Lexer &lexer, Rule &parser, BoxedCPP_System &ss, const std::vector<Boxed_Value> &vals) {
|
||||||
|
std::string val;
|
||||||
|
|
||||||
|
try {
|
||||||
|
val = Cast_Helper<std::string &>()(vals[0]);
|
||||||
|
}
|
||||||
|
catch (std::exception &e) {
|
||||||
|
throw EvalError("Can not evaluate string: " + val, TokenPtr());
|
||||||
|
}
|
||||||
|
catch (EvalError &ee) {
|
||||||
|
throw EvalError("Can not evaluate string: " + val + " reason: " + ee.reason, TokenPtr());
|
||||||
|
}
|
||||||
|
return evaluate_string(lexer, parser, ss, val, "__EVAL__");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string load_file(const char *filename) {
|
std::string load_file(const char *filename) {
|
||||||
@@ -206,7 +221,7 @@ Lexer build_lexer() {
|
|||||||
return lexer;
|
return lexer;
|
||||||
}
|
}
|
||||||
|
|
||||||
BoxedCPP_System build_eval_system() {
|
BoxedCPP_System build_eval_system(Lexer &lexer, Rule &parser) {
|
||||||
BoxedCPP_System ss;
|
BoxedCPP_System ss;
|
||||||
bootstrap(ss);
|
bootstrap(ss);
|
||||||
bootstrap_vector<std::vector<int> >(ss, "VectorInt");
|
bootstrap_vector<std::vector<int> >(ss, "VectorInt");
|
||||||
@@ -229,6 +244,10 @@ BoxedCPP_System build_eval_system() {
|
|||||||
ss.register_function(boost::shared_ptr<Proxy_Function>(
|
ss.register_function(boost::shared_ptr<Proxy_Function>(
|
||||||
new Dynamic_Proxy_Function(boost::bind(&add_two, boost::ref(ss), _1), 2)), "add_two");
|
new Dynamic_Proxy_Function(boost::bind(&add_two, boost::ref(ss), _1), 2)), "add_two");
|
||||||
|
|
||||||
|
ss.register_function(boost::shared_ptr<Proxy_Function>(
|
||||||
|
new Dynamic_Proxy_Function(boost::bind(&eval, boost::ref(lexer), boost::ref(parser),
|
||||||
|
boost::ref(ss), _1), 1)), "eval");
|
||||||
|
|
||||||
|
|
||||||
return ss;
|
return ss;
|
||||||
}
|
}
|
||||||
@@ -376,26 +395,18 @@ Boxed_Value eval_token(BoxedCPP_System &ss, TokenPtr node) {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case (TokenType::Fun_Call) : {
|
case (TokenType::Fun_Call) : {
|
||||||
if (node->children[0]->text == "eval") {
|
Param_List_Builder plb;
|
||||||
Lexer new_lexer = build_lexer();
|
for (i = 1; i < node->children.size(); ++i) {
|
||||||
Rule new_parser = build_parser_rules();
|
plb << eval_token(ss, node->children[i]);
|
||||||
|
|
||||||
Boxed_Value arg = eval_token(ss, node->children[1]);
|
|
||||||
std::string arg_string = Cast_Helper<std::string &>()(arg);
|
|
||||||
|
|
||||||
retval = evaluate_string(new_lexer, new_parser, ss, arg_string, "__EVAL__");
|
|
||||||
}
|
}
|
||||||
else {
|
try {
|
||||||
Param_List_Builder plb;
|
retval = dispatch(ss.get_function(node->children[0]->text), plb);
|
||||||
for (i = 1; i < node->children.size(); ++i) {
|
}
|
||||||
plb << eval_token(ss, node->children[i]);
|
catch(EvalError &ee) {
|
||||||
}
|
throw EvalError(ee.reason, node->children[0]);
|
||||||
try {
|
}
|
||||||
retval = dispatch(ss.get_function(node->children[0]->text), plb);
|
catch(std::exception &e){
|
||||||
}
|
throw EvalError("Can not find appropriate '" + node->children[0]->text + "'", node->children[0]);
|
||||||
catch(std::exception &e){
|
|
||||||
throw EvalError("Can not find appropriate '" + node->children[0]->text + "'", node->children[0]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -618,7 +629,7 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
Lexer lexer = build_lexer();
|
Lexer lexer = build_lexer();
|
||||||
Rule parser = build_parser_rules();
|
Rule parser = build_parser_rules();
|
||||||
BoxedCPP_System ss = build_eval_system();
|
BoxedCPP_System ss = build_eval_system(lexer, parser);
|
||||||
|
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
std::cout << "eval> ";
|
std::cout << "eval> ";
|
||||||
|
Reference in New Issue
Block a user