Code cleanup. Refactor chaiscript_eval to use functions instead of inline code
This commit is contained in:
parent
a0448fa558
commit
98edfc8dba
@ -23,10 +23,16 @@ namespace chaiscript
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the current evaluation engine
|
||||||
|
*/
|
||||||
Eval_Engine &get_eval_engine() {
|
Eval_Engine &get_eval_engine() {
|
||||||
return engine;
|
return engine;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prints the contents of an AST node, including its children, recursively
|
||||||
|
*/
|
||||||
void debug_print(TokenPtr t, std::string prepend = "") {
|
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;
|
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) {
|
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) {
|
const dispatchkit::Boxed_Value eval(const std::vector<dispatchkit::Boxed_Value> &vals) {
|
||||||
std::string val;
|
std::string val;
|
||||||
try {
|
try {
|
||||||
@ -48,6 +57,9 @@ namespace chaiscript
|
|||||||
return evaluate_string(val);
|
return evaluate_string(val);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function for loading a file
|
||||||
|
*/
|
||||||
std::string load_file(const char *filename) {
|
std::string load_file(const char *filename) {
|
||||||
std::ifstream infile (filename, std::ios::in | std::ios::ate);
|
std::ifstream infile (filename, std::ios::in | std::ios::ate);
|
||||||
|
|
||||||
@ -67,6 +79,9 @@ namespace chaiscript
|
|||||||
return ret_val;
|
return ret_val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds all the requirements for ChaiScript, including its evaluator and a run of its prelude.
|
||||||
|
*/
|
||||||
void build_eval_system() {
|
void build_eval_system() {
|
||||||
dispatchkit::Bootstrap::bootstrap(engine);
|
dispatchkit::Bootstrap::bootstrap(engine);
|
||||||
dispatchkit::bootstrap_vector<std::vector<dispatchkit::Boxed_Value> >(engine, "Vector");
|
dispatchkit::bootstrap_vector<std::vector<dispatchkit::Boxed_Value> >(engine, "Vector");
|
||||||
@ -80,6 +95,9 @@ namespace chaiscript
|
|||||||
evaluate_string(chaiscript_prelude);
|
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__") {
|
dispatchkit::Boxed_Value evaluate_string(const std::string &input, const char *filename = "__EVAL__") {
|
||||||
//debug_print(tokens);
|
//debug_print(tokens);
|
||||||
dispatchkit::Boxed_Value value;
|
dispatchkit::Boxed_Value value;
|
||||||
@ -112,6 +130,9 @@ namespace chaiscript
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads the file specified by filename, evaluates it, and returns the result
|
||||||
|
*/
|
||||||
dispatchkit::Boxed_Value evaluate_file(const char *filename) {
|
dispatchkit::Boxed_Value evaluate_file(const char *filename) {
|
||||||
return evaluate_string(load_file(filename), filename);
|
return evaluate_string(load_file(filename), filename);
|
||||||
}
|
}
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -56,7 +56,7 @@ def update_state(state)
|
|||||||
update_cpu_state(state, file, "cpu1");
|
update_cpu_state(state, file, "cpu1");
|
||||||
}
|
}
|
||||||
|
|
||||||
dump_system()
|
//dump_system()
|
||||||
|
|
||||||
var global_state = Map()
|
var global_state = Map()
|
||||||
|
|
||||||
|
1
unittests/eval.chai
Normal file
1
unittests/eval.chai
Normal file
@ -0,0 +1 @@
|
|||||||
|
print(eval("3 + 4"))
|
1
unittests/eval.txt
Normal file
1
unittests/eval.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
7
|
@ -1,5 +1,5 @@
|
|||||||
def bob() {
|
def sam() {
|
||||||
return 5
|
return 5
|
||||||
}
|
}
|
||||||
|
|
||||||
print(bob())
|
print(sam())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user