Code cleanup. Refactor chaiscript_eval to use functions instead of inline code

This commit is contained in:
Jonathan Turner 2009-07-08 22:01:40 +00:00
parent a0448fa558
commit 98edfc8dba
6 changed files with 692 additions and 470 deletions

View File

@ -23,10 +23,16 @@ namespace chaiscript
}
/**
* Returns the current evaluation engine
*/
Eval_Engine &get_eval_engine() {
return engine;
}
/**
* Prints the contents of an AST node, including its children, recursively
*/
void debug_print(TokenPtr t, std::string prepend = "") {
std::cout << prepend << "(" << token_type_to_string(t->identifier) << ") " << t->text << " : " << t->start.line << ", " << t->start.column << std::endl;
for (unsigned int j = 0; j < t->children.size(); ++j) {
@ -34,6 +40,9 @@ namespace chaiscript
}
}
/**
* Evaluates the given boxed string, used during eval() inside of a script
*/
const dispatchkit::Boxed_Value eval(const std::vector<dispatchkit::Boxed_Value> &vals) {
std::string val;
try {
@ -48,6 +57,9 @@ namespace chaiscript
return evaluate_string(val);
}
/**
* Helper function for loading a file
*/
std::string load_file(const char *filename) {
std::ifstream infile (filename, std::ios::in | std::ios::ate);
@ -67,6 +79,9 @@ namespace chaiscript
return ret_val;
}
/**
* Builds all the requirements for ChaiScript, including its evaluator and a run of its prelude.
*/
void build_eval_system() {
dispatchkit::Bootstrap::bootstrap(engine);
dispatchkit::bootstrap_vector<std::vector<dispatchkit::Boxed_Value> >(engine, "Vector");
@ -80,6 +95,9 @@ namespace chaiscript
evaluate_string(chaiscript_prelude);
}
/**
* Evaluates the given string in by parsing it and running the results through the evaluator
*/
dispatchkit::Boxed_Value evaluate_string(const std::string &input, const char *filename = "__EVAL__") {
//debug_print(tokens);
dispatchkit::Boxed_Value value;
@ -112,6 +130,9 @@ namespace chaiscript
return value;
}
/**
* Loads the file specified by filename, evaluates it, and returns the result
*/
dispatchkit::Boxed_Value evaluate_file(const char *filename) {
return evaluate_string(load_file(filename), filename);
}

View File

@ -34,45 +34,58 @@ namespace chaiscript
}
template <typename Eval_System>
dispatchkit::Boxed_Value eval_token(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value eval_file(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval;
unsigned int i, j;
switch (node->identifier) {
case (Token_Type::File) :
unsigned int i;
for (i = 0; i < node->children.size(); ++i) {
retval = eval_token(ss, node->children[i]);
}
break;
case (Token_Type::Id) :
return retval;
}
template <typename Eval_System>
dispatchkit::Boxed_Value eval_id(Eval_System &ss, TokenPtr node) {
if (node->text == "true") {
retval = dispatchkit::Boxed_Value(true);
return dispatchkit::Boxed_Value(true);
}
else if (node->text == "false") {
retval = dispatchkit::Boxed_Value(false);
return dispatchkit::Boxed_Value(false);
}
else {
try {
retval = ss.get_object(node->text);
return ss.get_object(node->text);
}
catch (std::exception &e) {
throw EvalError("Can not find object: " + node->text, node);
}
}
break;
case (Token_Type::Float) :
retval = dispatchkit::Boxed_Value(double(atof(node->text.c_str())));
break;
case (Token_Type::Int) :
retval = dispatchkit::Boxed_Value(atoi(node->text.c_str()));
break;
case (Token_Type::Quoted_String) :
retval = dispatchkit::Boxed_Value(node->text);
break;
case (Token_Type::Single_Quoted_String) :
retval = dispatchkit::Boxed_Value(node->text);
break;
case (Token_Type::Equation) :
}
template <typename Eval_System>
dispatchkit::Boxed_Value eval_float(Eval_System &ss, TokenPtr node) {
return dispatchkit::Boxed_Value(double(atof(node->text.c_str())));
}
template <typename Eval_System>
dispatchkit::Boxed_Value eval_int(Eval_System &ss, TokenPtr node) {
return dispatchkit::Boxed_Value(atoi(node->text.c_str()));
}
template <typename Eval_System>
dispatchkit::Boxed_Value eval_quoted_string(Eval_System &ss, TokenPtr node) {
return dispatchkit::Boxed_Value(node->text);
}
template <typename Eval_System>
dispatchkit::Boxed_Value eval_single_quoted_string(Eval_System &ss, TokenPtr node) {
return dispatchkit::Boxed_Value(node->text);
}
template <typename Eval_System>
dispatchkit::Boxed_Value eval_equation(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval;
unsigned int i;
retval = eval_token(ss, node->children.back());
if (node->children.size() > 1) {
for (i = node->children.size()-3; ((int)i) >= 0; i -= 2) {
@ -119,13 +132,20 @@ namespace chaiscript
}
}
}
break;
case (Token_Type::Var_Decl): {
ss.add_object(node->children[0]->text, dispatchkit::Boxed_Value());
retval = ss.get_object(node->children[0]->text);
return retval;
}
break;
case (Token_Type::Expression) : {
template <typename Eval_System>
dispatchkit::Boxed_Value eval_var_decl(Eval_System &ss, TokenPtr node) {
ss.add_object(node->children[0]->text, dispatchkit::Boxed_Value());
return ss.get_object(node->children[0]->text);
}
template <typename Eval_System>
dispatchkit::Boxed_Value eval_expression(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval;
unsigned int i;
retval = eval_token(ss, node->children[0]);
if (node->children.size() > 1) {
for (i = 1; i < node->children.size(); i += 2) {
@ -154,11 +174,14 @@ namespace chaiscript
}
}
}
return retval;
}
break;
case (Token_Type::Comparison) :
case (Token_Type::Additive) :
case (Token_Type::Multiplicative) : {
template <typename Eval_System>
dispatchkit::Boxed_Value eval_comp_add_mul(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval;
unsigned int i;
retval = eval_token(ss, node->children[0]);
if (node->children.size() > 1) {
for (i = 1; i < node->children.size(); i += 2) {
@ -174,9 +197,15 @@ namespace chaiscript
}
}
}
return retval;
}
break;
case (Token_Type::Array_Call) : {
template <typename Eval_System>
dispatchkit::Boxed_Value eval_array_call(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval;
unsigned int i;
retval = eval_token(ss, node->children[0]);
for (i = 1; i < node->children.size(); ++i) {
dispatchkit::Param_List_Builder plb;
@ -192,23 +221,31 @@ namespace chaiscript
throw EvalError("Can not find appropriate array lookup '[]' " + node->children[i]->text, node->children[i]);
}
}
return retval;
}
break;
case (Token_Type::Negate) : {
template <typename Eval_System>
dispatchkit::Boxed_Value eval_negate(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval;
retval = eval_token(ss, node->children[0]);
dispatchkit::Param_List_Builder plb;
plb << retval;
plb << dispatchkit::Boxed_Value(-1);
try {
retval = dispatch(ss.get_function("*"), plb);
return dispatch(ss.get_function("*"), plb);
}
catch(std::exception &e){
throw EvalError("Can not find appropriate negation", node->children[0]);
}
}
break;
case (Token_Type::Not) : {
template <typename Eval_System>
dispatchkit::Boxed_Value eval_not(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval;
bool cond;
try {
retval = eval_token(ss, node->children[0]);
@ -217,23 +254,30 @@ namespace chaiscript
catch (std::exception) {
throw EvalError("Boolean not('!') condition not boolean", node->children[0]);
}
retval = dispatchkit::Boxed_Value(!cond);
return dispatchkit::Boxed_Value(!cond);
}
break;
case (Token_Type::Prefix) : {
template <typename Eval_System>
dispatchkit::Boxed_Value eval_prefix(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval;
retval = eval_token(ss, node->children[1]);
dispatchkit::Param_List_Builder plb;
plb << retval;
try {
retval = dispatch(ss.get_function(node->children[0]->text), plb);
return dispatch(ss.get_function(node->children[0]->text), plb);
}
catch(std::exception &e){
throw EvalError("Can not find appropriate prefix", node->children[0]);
}
}
break;
case (Token_Type::Inline_Array) : {
template <typename Eval_System>
dispatchkit::Boxed_Value eval_inline_array(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval;
unsigned int i;
try {
retval = dispatch(ss.get_function("Vector"), dispatchkit::Param_List_Builder());
if (node->children.size() > 0) {
@ -251,11 +295,14 @@ namespace chaiscript
catch (const dispatchkit::dispatch_error &e) {
throw EvalError("Can not find appropriate 'Vector()'", node);
}
return retval;
}
break;
case (Token_Type::Inline_Range) : {
template <typename Eval_System>
dispatchkit::Boxed_Value eval_inline_range(Eval_System &ss, TokenPtr node) {
try {
retval = dispatch(ss.get_function("generate_range"), dispatchkit::Param_List_Builder()
return dispatch(ss.get_function("generate_range"), dispatchkit::Param_List_Builder()
<< eval_token(ss, node->children[0]->children[0]->children[0])
<< eval_token(ss, node->children[0]->children[0]->children[1]));
}
@ -263,8 +310,12 @@ namespace chaiscript
throw EvalError("Unable to generate range vector", node);
}
}
break;
case (Token_Type::Inline_Map) : {
template <typename Eval_System>
dispatchkit::Boxed_Value eval_inline_map(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval;
unsigned int i;
try {
retval = dispatch(ss.get_function("Map"), dispatchkit::Param_List_Builder());
for (i = 0; i < node->children[0]->children.size(); ++i) {
@ -281,15 +332,18 @@ namespace chaiscript
catch (const dispatchkit::dispatch_error &e) {
throw EvalError("Can not find appropriate 'Map()'", node);
}
return retval;
}
break;
case (Token_Type::Fun_Call) : {
template <typename Eval_System>
dispatchkit::Boxed_Value eval_fun_call(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval;
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();
dispatchkit::Dispatch_Engine::Stack new_stack;
unsigned int i;
new_stack.push_back(dispatchkit::Dispatch_Engine::Scope());
if ((node->children.size() > 1) && (node->children[1]->identifier == Token_Type::Arg_List)) {
@ -306,10 +360,7 @@ namespace chaiscript
throw EvalError(ee.reason, node->children[0]);
}
try {
//fn = ss.get_function(node->children[0]->text);
ss.set_stack(new_stack);
//retval = dispatch(fn, plb);
//retval = dispatch
retval = (*dispatchkit::boxed_cast<boost::shared_ptr<dispatchkit::Proxy_Function> >(fn))(plb);
ss.set_stack(prev_stack);
}
@ -325,16 +376,22 @@ namespace chaiscript
ss.set_stack(prev_stack);
throw;
}
}
break;
case (Token_Type::Dot_Access) : {
return retval;
}
template <typename Eval_System>
dispatchkit::Boxed_Value eval_dot_access(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval;
std::vector<std::pair<std::string, dispatchkit::Dispatch_Engine::Function_Map::mapped_type> > fn;
dispatchkit::Dispatch_Engine::Stack prev_stack = ss.get_stack();
dispatchkit::Dispatch_Engine::Stack new_stack;
unsigned int i, j;
new_stack.push_back(dispatchkit::Dispatch_Engine::Scope());
//todo: Please extract a single way of doing function calls between this and eval_fun_call
retval = eval_token(ss, node->children[0]);
if (node->children.size() > 1) {
for (i = 1; i < node->children.size(); ++i) {
@ -342,7 +399,6 @@ namespace chaiscript
plb << retval;
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]);
}
@ -376,10 +432,15 @@ namespace chaiscript
}
}
}
}
break;
case(Token_Type::If) : {
return retval;
}
template <typename Eval_System>
dispatchkit::Boxed_Value eval_if(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval;
unsigned int i;
retval = eval_token(ss, node->children[0]);
bool cond;
try {
@ -415,9 +476,14 @@ namespace chaiscript
}
}
}
return retval;
}
break;
case(Token_Type::While) : {
template <typename Eval_System>
dispatchkit::Boxed_Value eval_while(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval;
retval = eval_token(ss, node->children[0]);
bool cond;
try {
@ -441,10 +507,13 @@ namespace chaiscript
cond = false;
}
}
retval = dispatchkit::Boxed_Value();
return dispatchkit::Boxed_Value();
}
break;
case(Token_Type::For) : {
template <typename Eval_System>
dispatchkit::Boxed_Value eval_for(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval;
dispatchkit::Boxed_Value condition;
bool cond;
@ -483,10 +552,14 @@ namespace chaiscript
cond = false;
}
}
retval = dispatchkit::Boxed_Value();
return dispatchkit::Boxed_Value();
}
break;
case (Token_Type::Def) : {
template <typename Eval_System>
dispatchkit::Boxed_Value eval_def(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval;
unsigned int i;
std::vector<std::string> param_names;
std::string annotation = node->annotation?node->annotation->text:"";
boost::shared_ptr<dispatchkit::Dynamic_Proxy_Function> guard;
@ -526,9 +599,14 @@ namespace chaiscript
param_names, _1), numparams,
annotation, guard)), function_name);
return retval;
}
break;
case (Token_Type::Lambda) : {
template <typename Eval_System>
dispatchkit::Boxed_Value eval_lambda(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval;
unsigned int i;
std::vector<std::string> param_names;
size_t numparams = 0;
@ -544,13 +622,16 @@ namespace chaiscript
numparams = 0;
}
retval = dispatchkit::Boxed_Value(boost::shared_ptr<dispatchkit::Proxy_Function>(
return 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), numparams)));
}
break;
case (Token_Type::Block) : {
template <typename Eval_System>
dispatchkit::Boxed_Value eval_block(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval;
unsigned int i;
ss.new_scope();
for (i = 0; i < node->children.size(); ++i) {
try {
@ -567,10 +648,13 @@ namespace chaiscript
}
}
ss.pop_scope();
}
break;
case (Token_Type::Return) : {
return retval;
}
template <typename Eval_System>
dispatchkit::Boxed_Value eval_return(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval;
if (node->children.size() > 0) {
retval = eval_token(ss, node->children[0]);
}
@ -579,10 +663,125 @@ namespace chaiscript
}
throw ReturnValue(retval, node);
}
break;
case (Token_Type::Break) : {
template <typename Eval_System>
dispatchkit::Boxed_Value eval_break(Eval_System &ss, TokenPtr node) {
throw BreakLoop(node);
}
template <typename Eval_System>
dispatchkit::Boxed_Value eval_token(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval;
switch (node->identifier) {
case (Token_Type::File) :
retval = eval_file(ss, node);
break;
case (Token_Type::Id) :
retval = eval_id(ss, node);
break;
case (Token_Type::Float) :
retval = eval_float(ss, node);
break;
case (Token_Type::Int) :
retval = eval_int(ss, node);
break;
case (Token_Type::Quoted_String) :
retval = eval_quoted_string(ss, node);
break;
case (Token_Type::Single_Quoted_String) :
retval = eval_single_quoted_string(ss, node);
break;
case (Token_Type::Equation) :
retval = eval_equation(ss, node);
break;
case (Token_Type::Var_Decl) :
retval = eval_var_decl(ss, node);
break;
case (Token_Type::Expression) :
retval = eval_expression(ss, node);
break;
case (Token_Type::Comparison) :
case (Token_Type::Additive) :
case (Token_Type::Multiplicative) :
retval = eval_comp_add_mul(ss, node);
break;
case (Token_Type::Array_Call) :
retval = eval_array_call(ss, node);
break;
case (Token_Type::Negate) :
retval = eval_negate(ss, node);
break;
case (Token_Type::Not) :
retval = eval_not(ss, node);
break;
case (Token_Type::Prefix) :
retval = eval_prefix(ss, node);
break;
case (Token_Type::Inline_Array) :
retval = eval_inline_array(ss, node);
break;
case (Token_Type::Inline_Range) :
retval = eval_inline_range(ss, node);
break;
case (Token_Type::Inline_Map) :
retval = eval_inline_map(ss, node);
break;
case (Token_Type::Fun_Call) :
retval = eval_fun_call(ss, node);
break;
case (Token_Type::Dot_Access) :
retval = eval_dot_access(ss, node);
break;
case(Token_Type::If) :
retval = eval_if(ss, node);
break;
case(Token_Type::While) :
retval = eval_while(ss, node);
break;
case(Token_Type::For) :
retval = eval_for(ss, node);
break;
case (Token_Type::Def) :
retval = eval_def(ss, node);
break;
case (Token_Type::Lambda) :
retval = eval_lambda(ss, node);
break;
case (Token_Type::Block) :
retval = eval_block(ss, node);
break;
case (Token_Type::Return) :
retval = eval_return(ss, node);
break;
case (Token_Type::Break) :
retval = eval_break(ss, node);
break;
}

View File

@ -56,7 +56,7 @@ def update_state(state)
update_cpu_state(state, file, "cpu1");
}
dump_system()
//dump_system()
var global_state = Map()

1
unittests/eval.chai Normal file
View File

@ -0,0 +1 @@
print(eval("3 + 4"))

1
unittests/eval.txt Normal file
View File

@ -0,0 +1 @@
7

View File

@ -1,5 +1,5 @@
def bob() {
def sam() {
return 5
}
print(bob())
print(sam())