Fix default cmake build to use readline and gdb. Add simple try/catch/throw exceptions

This commit is contained in:
Jonathan Turner 2009-09-18 22:04:07 +00:00
parent a754ce9eb6
commit 204d379176
5 changed files with 115 additions and 6 deletions

View File

@ -15,13 +15,11 @@ else(READLINE_LIBRARY)
SET (READLINE_FLAG )
endif(READLINE_LIBRARY)
#SET (CMAKE_BUILD_TYPE rel)
SET (CMAKE_C_FLAGS_REL " -Wall -O3 ${READLINE_FLAG}")
SET (CMAKE_CXX_FLAGS_REL " -Wall -O3 ${READLINE_FLAG}")
#SET (CMAKE_BUILD_TYPE gdb)
SET (CMAKE_C_FLAGS_GDB " -Wall -ggdb ${READLINE_FLAG}")
SET (CMAKE_CXX_FLAGS_GDB " -Wall -ggdb ${READLINE_FLAG}")
SET (CMAKE_C_FLAGS " -Wall -ggdb ${READLINE_FLAG}")
SET (CMAKE_CXX_FLAGS " -Wall -ggdb ${READLINE_FLAG}")
include_directories(include)

View File

@ -41,7 +41,7 @@ namespace chaiscript
class Token_Type { public: enum Type { Error, Int, Float, Id, Char, Str, Eol, Fun_Call, Inplace_Fun_Call, Arg_List, Variable, Equation, Var_Decl,
Expression, Comparison, Additive, Multiplicative, Negate, Not, Array_Call, Dot_Access, Quoted_String, Single_Quoted_String,
Lambda, Block, Def, While, If, For, Inline_Array, Inline_Map, Return, File, Prefix, Break, Map_Pair, Value_Range,
Inline_Range, Annotation }; };
Inline_Range, Annotation, Try }; };
/**
* Helper lookup to get the name of each node type
@ -50,7 +50,7 @@ namespace chaiscript
const char *token_types[] = { "Internal Parser Error", "Int", "Float", "Id", "Char", "Str", "Eol", "Fun_Call", "Inplace_Fun_Call", "Arg_List", "Variable", "Equation", "Var_Decl",
"Expression", "Comparison", "Additive", "Multiplicative", "Negate", "Not", "Array_Call", "Dot_Access", "Quoted_String", "Single_Quoted_String",
"Lambda", "Block", "Def", "While", "If", "For", "Inline_Array", "Inline_Map", "Return", "File", "Prefix", "Break", "Map_Pair", "Value_Range",
"Inline_Range", "Annotation"};
"Inline_Range", "Annotation", "Try"};
return token_types[tokentype];
}

View File

@ -638,12 +638,21 @@ namespace chaiscript
return Boxed_Value(f->call_match(std::vector<Boxed_Value>(params.begin() + 1, params.end())));
}
static void throw_exception(const Boxed_Value &bv) {
throw bv;
}
static boost::shared_ptr<Dispatch_Engine> bootstrap2(boost::shared_ptr<Dispatch_Engine> e = boost::shared_ptr<Dispatch_Engine> (new Dispatch_Engine()))
{
e->add(user_type<void>(), "void");
return e;
}
static std::string what(const std::exception &e)
{
return e.what();
}
public:
/**
* perform all common bootstrap functions for std::string, void and POD types
@ -655,6 +664,7 @@ namespace chaiscript
m->add(user_type<Boxed_Value>(), "Object");
m->add(user_type<Boxed_POD_Value>(), "PODObject");
m->add(user_type<Proxy_Function>(), "function");
m->add(user_type<std::exception>(), "exception");
basic_constructors<bool>("bool", m);
oper_assign<std::string>(m);
@ -663,6 +673,7 @@ namespace chaiscript
m->add(fun(&to_string<const std::string &>), "internal_to_string");
m->add(fun(&to_string<bool>), "internal_to_string");
m->add(fun(&unknown_assign), "=");
m->add(fun(&throw_exception), "throw");
bootstrap_pod_type<double>("double", m);
bootstrap_pod_type<int>("int", m);

View File

@ -579,6 +579,40 @@ namespace chaiscript
return retval;
}
/**
* Evaluates an if/elseif/else block
*/
template <typename Eval_System>
Boxed_Value eval_try(Eval_System &ss, const TokenPtr &node) {
Boxed_Value retval;
retval = Boxed_Value();
ss.new_scope();
try {
retval = eval_token(ss, node->children[0]);
}
catch (std::exception &) {
// nothing
std::cout << "DEBUG: std::exception caught" << std::endl;
}
catch (Boxed_Value &bv) {
if (node->children.size() > 2) {
if (node->children[1]->text == "catch") {
if (node->children.size() > 3) {
ss.add_object(node->children[2]->text, bv);
retval = eval_token(ss, node->children[3]);
}
else {
retval = eval_token(ss, node->children[2]);
}
}
}
}
ss.pop_scope();
return retval;
}
/**
* Evaluates an if/elseif/else block
*/
@ -924,6 +958,10 @@ namespace chaiscript
return eval_dot_access(ss, node);
break;
case(Token_Type::Try) :
return eval_try(ss, node);
break;
case(Token_Type::If) :
return eval_if(ss, node);
break;

View File

@ -916,6 +916,60 @@ namespace chaiscript
return retval;
}
/**
* Reads a function definition from input
*/
bool Try() {
bool retval = false;
bool is_annotated = false;
TokenPtr annotation;
if (Annotation()) {
while (Eol_());
annotation = match_stack.back();
match_stack.pop_back();
is_annotated = true;
}
int prev_stack_top = match_stack.size();
if (Keyword("try")) {
retval = true;
while (Eol());
if (!Block()) {
throw Eval_Error("Incomplete 'try' block", File_Position(line, col), filename);
}
bool has_matches = true;
while (has_matches) {
while (Eol());
has_matches = false;
if (Keyword("catch", true)) {
if (Char('(')) {
if (!(Id(true) && Char(')'))) {
throw Eval_Error("Incomplete 'catch' expression", File_Position(line, col), filename);
}
}
while (Eol());
if (!Block()) {
throw Eval_Error("Incomplete 'catch' block", File_Position(line, col), filename);
}
has_matches = true;
}
}
build_match(Token_Type::Try, prev_stack_top);
}
return retval;
}
/**
* Reads an if/elseif/else block from input
*/
@ -1550,6 +1604,14 @@ namespace chaiscript
retval = true;
saw_eol = true;
}
else if (Try()) {
if (!saw_eol) {
throw Eval_Error("Two function definitions missing line separator", match_stack.back());
}
has_more = true;
retval = true;
saw_eol = true;
}
else if (If()) {
if (!saw_eol) {
throw Eval_Error("Two function definitions missing line separator", match_stack.back());