Merge branch '2011-03-15-DocumentationUpdates'

Conflicts:
	include/chaiscript/language/chaiscript_engine.hpp
This commit is contained in:
Jason Turner
2011-03-29 20:24:17 -06:00
38 changed files with 5314 additions and 5315 deletions

View File

@@ -1,6 +1,6 @@
// This file is distributed under the BSD License.
// See "license.txt" for details.
// Copyright 2009-2010, Jonathan Turner (jonathan@emptycrate.com)
// Copyright 2009-2011, Jonathan Turner (jonathan@emptycrate.com)
// and Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com
@@ -8,6 +8,7 @@
#define CHAISCRIPT_COMMON_HPP_
#include <chaiscript/dispatchkit/dispatchkit.hpp>
#include <boost/enable_shared_from_this.hpp>
namespace chaiscript
{
@@ -57,110 +58,155 @@ namespace chaiscript
typedef boost::shared_ptr<struct AST_Node> AST_NodePtr;
namespace exception
{
/**
* Errors generated during parsing or evaluation
*/
struct eval_error : public std::runtime_error {
std::string reason;
File_Position start_position;
File_Position end_position;
std::string filename;
std::vector<AST_NodePtr> call_stack;
eval_error(const std::string &t_why, const File_Position &t_where, const std::string &t_fname) throw() :
std::runtime_error("Error: \"" + t_why + "\" " +
(t_fname != "__EVAL__" ? ("in '" + t_fname + "' ") : "during evaluation ") +
+ "at (" + boost::lexical_cast<std::string>(t_where.line) + ", " +
boost::lexical_cast<std::string>(t_where.column) + ")"),
reason(t_why), start_position(t_where), end_position(t_where), filename(t_fname)
{ }
eval_error(const std::string &t_why) throw()
: std::runtime_error("Error: \"" + t_why + "\" "),
reason(t_why)
{}
virtual ~eval_error() throw() {}
};
/**
* Errors generated when loading a file
*/
struct file_not_found_error : public std::runtime_error {
file_not_found_error(const std::string &t_filename) throw()
: std::runtime_error("File Not Found: " + t_filename)
{ }
virtual ~file_not_found_error() throw() {}
};
}
/**
* The struct that doubles as both a parser ast_node and an AST node
*/
struct AST_Node {
std::string text;
int identifier;
boost::shared_ptr<std::string> filename;
File_Position start, end;
std::vector<AST_NodePtr> children;
AST_NodePtr annotation;
struct AST_Node : boost::enable_shared_from_this<AST_Node> {
public:
const std::string text;
const int identifier;
boost::shared_ptr<const std::string> filename;
File_Position start, end;
std::vector<AST_NodePtr> children;
AST_NodePtr annotation;
AST_Node(const std::string &t_ast_node_text, int t_id, const boost::shared_ptr<std::string> &t_fname,
int t_start_line, int t_start_col, int t_end_line, int t_end_col) :
text(t_ast_node_text), identifier(t_id), filename(t_fname),
start(t_start_line, t_start_col), end(t_end_line, t_end_col)
{
}
/**
* Prints the contents of an AST node, including its children, recursively
*/
std::string to_string(std::string t_prepend = "") {
std::ostringstream oss;
AST_Node(const std::string &t_ast_node_text, int t_id, const boost::shared_ptr<std::string> &t_fname) :
text(t_ast_node_text), identifier(t_id), filename(t_fname) {}
virtual ~AST_Node() {}
/**
* Prints the contents of an AST node, including its children, recursively
*/
std::string to_string(std::string t_prepend = "") {
std::ostringstream oss;
oss << t_prepend << "(" << ast_node_type_to_string(this->identifier) << ") "
<< this->text << " : " << this->start.line << ", " << this->start.column << std::endl;
for (unsigned int j = 0; j < this->children.size(); ++j) {
oss << this->children[j]->to_string(t_prepend + " ");
oss << t_prepend << "(" << ast_node_type_to_string(this->identifier) << ") "
<< this->text << " : " << this->start.line << ", " << this->start.column << std::endl;
for (unsigned int j = 0; j < this->children.size(); ++j) {
oss << this->children[j]->to_string(t_prepend + " ");
}
return oss.str();
}
return oss.str();
}
std::string internal_to_string() {
return to_string();
}
std::string internal_to_string() {
return to_string();
}
virtual Boxed_Value eval(Dispatch_Engine &) {
Boxed_Value bv;
throw std::runtime_error("Undispatched ast_node (internal error)");
}
Boxed_Value eval(chaiscript::detail::Dispatch_Engine &t_e)
{
try {
return eval_internal(t_e);
} catch (exception::eval_error &ee) {
ee.call_stack.push_back(shared_from_this());
throw ee;
}
}
void replace_child(const AST_NodePtr &t_child, const AST_NodePtr &t_new_child)
{
std::replace(children.begin(), children.end(), t_child, t_new_child);
}
protected:
AST_Node(const std::string &t_ast_node_text, int t_id, const boost::shared_ptr<std::string> &t_fname,
int t_start_line, int t_start_col, int t_end_line, int t_end_col) :
text(t_ast_node_text), identifier(t_id), filename(t_fname),
start(t_start_line, t_start_col), end(t_end_line, t_end_col)
{
}
AST_Node(const std::string &t_ast_node_text, int t_id, const boost::shared_ptr<std::string> &t_fname) :
text(t_ast_node_text), identifier(t_id), filename(t_fname) {}
virtual ~AST_Node() {}
virtual Boxed_Value eval_internal(chaiscript::detail::Dispatch_Engine &)
{
throw std::runtime_error("Undispatched ast_node (internal error)");
}
};
/**
* Errors generated during parsing or evaluation
*/
struct Eval_Error : public std::runtime_error {
std::string reason;
File_Position start_position;
File_Position end_position;
std::string filename;
std::vector<AST_NodePtr> call_stack;
Eval_Error(const std::string &t_why, const File_Position &t_where, const std::string &t_fname) :
std::runtime_error("Error: \"" + t_why + "\" " +
(t_fname != "__EVAL__" ? ("in '" + t_fname + "' ") : "during evaluation ") +
+ "at (" + boost::lexical_cast<std::string>(t_where.line) + ", " +
boost::lexical_cast<std::string>(t_where.column) + ")"),
reason(t_why), start_position(t_where), end_position(t_where), filename(t_fname)
{ }
namespace detail
{
/**
* Special type for returned values
*/
struct Return_Value {
Boxed_Value retval;
Eval_Error(const std::string &t_why)
: std::runtime_error("Error: \"" + t_why + "\" "),
reason(t_why)
{}
Return_Value(const Boxed_Value &t_return_value) : retval(t_return_value) { }
};
virtual ~Eval_Error() throw() {}
};
/**
* Special type indicating a call to 'break'
*/
struct Break_Loop {
Break_Loop() { }
};
/**
* Errors generated when loading a file
*/
struct File_Not_Found_Error : public std::runtime_error {
File_Not_Found_Error(const std::string &t_filename)
: std::runtime_error("File Not Found: " + t_filename)
{ }
/// Creates a new scope then pops it on destruction
struct Scope_Push_Pop
{
Scope_Push_Pop(chaiscript::detail::Dispatch_Engine &t_de)
: m_de(t_de)
{
m_de.new_scope();
}
virtual ~File_Not_Found_Error() throw() {}
};
~Scope_Push_Pop()
{
m_de.pop_scope();
}
/**
* Special type for returned values
*/
struct Return_Value {
Boxed_Value retval;
private:
chaiscript::detail::Dispatch_Engine &m_de;
};
Return_Value(const Boxed_Value &t_return_value) : retval(t_return_value) { }
};
/**
* Special type indicating a call to 'break'
*/
struct Break_Loop {
Break_Loop() { }
};
}
}
#endif /* _CHAISCRIPT_COMMON_HPP */

View File

@@ -1,6 +1,6 @@
// This file is distributed under the BSD License.
// See "license.txt" for details.
// Copyright 2009-2010, Jonathan Turner (jonathan@emptycrate.com)
// Copyright 2009-2011, Jonathan Turner (jonathan@emptycrate.com)
// and Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com
@@ -28,205 +28,210 @@
namespace chaiscript
{
struct load_module_error : std::runtime_error
namespace exception
{
load_module_error(const std::string &t_reason) throw()
: std::runtime_error(t_reason)
struct load_module_error : std::runtime_error
{
}
load_module_error(const std::string &t_reason) throw()
: std::runtime_error(t_reason)
{
}
virtual ~load_module_error() throw()
{
}
};
virtual ~load_module_error() throw()
{
}
};
}
namespace detail
{
#ifdef _POSIX_VERSION
struct Loadable_Module
{
struct DLModule
struct Loadable_Module
{
DLModule(const std::string &t_filename)
: m_data(dlopen(t_filename.c_str(), RTLD_NOW))
struct DLModule
{
if (!m_data)
DLModule(const std::string &t_filename)
: m_data(dlopen(t_filename.c_str(), RTLD_NOW))
{
if (!m_data)
{
throw load_module_error(dlerror());
throw exception::load_module_error(dlerror());
}
}
}
DLModule(const DLModule &); // Explicitly unimplemented copy constructor
DLModule &operator=(const DLModule &); // Explicitly unimplemented assignment operator
DLModule(const DLModule &); // Explicitly unimplemented copy constructor
DLModule &operator=(const DLModule &); // Explicitly unimplemented assignment operator
~DLModule()
{
dlclose(m_data);
}
~DLModule()
{
dlclose(m_data);
}
void *m_data;
};
void *m_data;
};
template<typename T>
struct DLSym
{
DLSym(DLModule &t_mod, const std::string &t_symbol)
: m_symbol(reinterpret_cast<T>(dlsym(t_mod.m_data, t_symbol.c_str())))
{
if (!m_symbol)
template<typename T>
struct DLSym
{
DLSym(DLModule &t_mod, const std::string &t_symbol)
: m_symbol(reinterpret_cast<T>(dlsym(t_mod.m_data, t_symbol.c_str())))
{
throw load_module_error(dlerror());
if (!m_symbol)
{
throw exception::load_module_error(dlerror());
}
}
}
T m_symbol;
};
T m_symbol;
};
Loadable_Module(const std::string &t_module_name, const std::string &t_filename)
: m_dlmodule(t_filename), m_func(m_dlmodule, "create_chaiscript_module_" + t_module_name),
Loadable_Module(const std::string &t_module_name, const std::string &t_filename)
: m_dlmodule(t_filename), m_func(m_dlmodule, "create_chaiscript_module_" + t_module_name),
m_moduleptr(m_func.m_symbol())
{
}
{
}
DLModule m_dlmodule;
DLSym<Create_Module_Func> m_func;
ModulePtr m_moduleptr;
};
DLModule m_dlmodule;
DLSym<Create_Module_Func> m_func;
ModulePtr m_moduleptr;
};
#else
#ifdef WIN32
struct Loadable_Module
{
template<typename T>
static std::wstring towstring(const T &t_str)
struct Loadable_Module
{
return std::wstring(t_str.begin(), t_str.end());
}
template<typename T>
static std::wstring towstring(const T &t_str)
{
return std::wstring(t_str.begin(), t_str.end());
}
template<typename T>
static std::string tostring(const T &t_str)
{
return std::string(t_str.begin(), t_str.end());
}
template<typename T>
static std::string tostring(const T &t_str)
{
return std::string(t_str.begin(), t_str.end());
}
#ifdef _UNICODE
template<typename T>
static std::wstring toproperstring(const T &t_str)
{
return towstring(t_str);
}
template<typename T>
static std::wstring toproperstring(const T &t_str)
{
return towstring(t_str);
}
#else
template<typename T>
static std::string toproperstring(const T &t_str)
{
return tostring(t_str);
}
template<typename T>
static std::string toproperstring(const T &t_str)
{
return tostring(t_str);
}
#endif
static std::string GetErrorMessage(DWORD t_err)
{
static std::string GetErrorMessage(DWORD t_err)
{
#ifdef _UNICODE
typedef LPWSTR StringType;
std::wstring retval = L"Unknown Error";
typedef LPWSTR StringType;
std::wstring retval = L"Unknown Error";
#else
typedef LPSTR StringType;
std::string retval = "Unknown Error";
typedef LPSTR StringType;
std::string retval = "Unknown Error";
#endif
StringType lpMsgBuf = 0;
StringType lpMsgBuf = 0;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
t_err,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(StringType)&lpMsgBuf,
0, NULL );
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
t_err,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(StringType)&lpMsgBuf,
0, NULL );
if (lpMsgBuf)
{
retval = lpMsgBuf;
if (lpMsgBuf)
{
retval = lpMsgBuf;
}
LocalFree(lpMsgBuf);
return tostring(retval);
}
LocalFree(lpMsgBuf);
return tostring(retval);
}
struct DLModule
{
DLModule(const std::string &t_filename)
: m_data(LoadLibrary(toproperstring(t_filename).c_str()))
struct DLModule
{
if (!m_data)
DLModule(const std::string &t_filename)
: m_data(LoadLibrary(toproperstring(t_filename).c_str()))
{
if (!m_data)
{
throw load_module_error(GetErrorMessage(GetLastError()));
throw exception::load_module_error(GetErrorMessage(GetLastError()));
}
}
}
~DLModule()
{
FreeLibrary(m_data);
}
~DLModule()
{
FreeLibrary(m_data);
}
HMODULE m_data;
};
HMODULE m_data;
};
template<typename T>
struct DLSym
{
DLSym(DLModule &t_mod, const std::string &t_symbol)
: m_symbol(reinterpret_cast<T>(GetProcAddress(t_mod.m_data, t_symbol.c_str())))
{
if (!m_symbol)
template<typename T>
struct DLSym
{
DLSym(DLModule &t_mod, const std::string &t_symbol)
: m_symbol(reinterpret_cast<T>(GetProcAddress(t_mod.m_data, t_symbol.c_str())))
{
throw load_module_error(GetErrorMessage(GetLastError()));
if (!m_symbol)
{
throw exception::load_module_error(GetErrorMessage(GetLastError()));
}
}
}
T m_symbol;
};
T m_symbol;
};
Loadable_Module(const std::string &t_module_name, const std::string &t_filename)
: m_dlmodule(t_filename), m_func(m_dlmodule, "create_chaiscript_module_" + t_module_name),
Loadable_Module(const std::string &t_module_name, const std::string &t_filename)
: m_dlmodule(t_filename), m_func(m_dlmodule, "create_chaiscript_module_" + t_module_name),
m_moduleptr(m_func.m_symbol())
{
}
{
}
DLModule m_dlmodule;
DLSym<Create_Module_Func> m_func;
ModulePtr m_moduleptr;
};
DLModule m_dlmodule;
DLSym<Create_Module_Func> m_func;
ModulePtr m_moduleptr;
};
#else
struct Loadable_Module
{
Loadable_Module(const std::string &, const std::string &)
struct Loadable_Module
{
throw load_module_error("Loadable module support not available for your platform");
}
Loadable_Module(const std::string &, const std::string &)
{
throw exception::load_module_error("Loadable module support not available for your platform");
}
ModulePtr m_moduleptr;
};
ModulePtr m_moduleptr;
};
#endif
#endif
typedef boost::shared_ptr<Loadable_Module> Loadable_Module_Ptr;
typedef boost::shared_ptr<Loadable_Module> Loadable_Module_Ptr;
}
class ChaiScript {
#ifndef CHAISCRIPT_NO_THREADS
mutable boost::shared_mutex m_mutex;
mutable boost::recursive_mutex m_use_mutex;
#endif
mutable chaiscript::detail::threading::shared_mutex m_mutex;
mutable chaiscript::detail::threading::recursive_mutex m_use_mutex;
std::set<std::string> m_used_files;
std::map<std::string, Loadable_Module_Ptr> m_loaded_modules;
std::map<std::string, detail::Loadable_Module_Ptr> m_loaded_modules;
std::set<std::string> m_active_loaded_modules;
std::vector<std::string> m_modulepaths;
std::vector<std::string> m_usepaths;
Dispatch_Engine m_engine;
chaiscript::detail::Dispatch_Engine m_engine;
/**
@@ -235,7 +240,7 @@ namespace chaiscript
Boxed_Value do_eval(const std::string &t_input, const std::string &t_filename = "__EVAL__", bool /* t_internal*/ = false)
{
try {
ChaiScript_Parser parser;
parser::ChaiScript_Parser parser;
if (parser.parse(t_input, t_filename)) {
//parser.show_match_stack();
return parser.ast()->eval(m_engine);
@@ -243,7 +248,7 @@ namespace chaiscript
return Boxed_Value();
}
}
catch (const Return_Value &rv) {
catch (const detail::Return_Value &rv) {
return rv.retval;
}
}
@@ -271,23 +276,19 @@ namespace chaiscript
try {
const std::string appendedpath = m_usepaths[i] + t_filename;
#ifndef CHAISCRIPT_NO_THREADS
boost::lock_guard<boost::recursive_mutex> l(m_use_mutex);
boost::shared_lock<boost::shared_mutex> l2(m_mutex);
#endif
chaiscript::detail::threading::lock_guard<chaiscript::detail::threading::recursive_mutex> l(m_use_mutex);
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l2(m_mutex);
if (m_used_files.count(appendedpath) == 0)
{
#ifndef CHAISCRIPT_NO_THREADS
m_used_files.insert(appendedpath);
l2.unlock();
#endif
eval_file(appendedpath);
}
} catch (const File_Not_Found_Error &) {
} catch (const exception::file_not_found_error &) {
if (i == m_usepaths.size() - 1)
{
throw File_Not_Found_Error(t_filename);
throw exception::file_not_found_error(t_filename);
}
// failed to load, try the next path
@@ -295,6 +296,63 @@ namespace chaiscript
}
}
/**
* Returns the current evaluation m_engine
*/
chaiscript::detail::Dispatch_Engine &get_eval_engine() {
return m_engine;
}
/**
* Builds all the requirements for ChaiScript, including its evaluator and a run of its prelude.
*/
void build_eval_system() {
using namespace bootstrap;
m_engine.add_reserved_word("def");
m_engine.add_reserved_word("fun");
m_engine.add_reserved_word("while");
m_engine.add_reserved_word("for");
m_engine.add_reserved_word("if");
m_engine.add_reserved_word("else");
m_engine.add_reserved_word("&&");
m_engine.add_reserved_word("||");
m_engine.add_reserved_word(",");
m_engine.add_reserved_word(":=");
m_engine.add_reserved_word("var");
m_engine.add_reserved_word("return");
m_engine.add_reserved_word("break");
m_engine.add_reserved_word("true");
m_engine.add_reserved_word("false");
m_engine.add_reserved_word("_");
add(Bootstrap::bootstrap());
m_engine.add(fun(&chaiscript::detail::Dispatch_Engine::dump_system, boost::ref(m_engine)), "dump_system");
m_engine.add(fun(&chaiscript::detail::Dispatch_Engine::dump_object, boost::ref(m_engine)), "dump_object");
m_engine.add(fun(&chaiscript::detail::Dispatch_Engine::is_type, boost::ref(m_engine)), "is_type");
m_engine.add(fun(&chaiscript::detail::Dispatch_Engine::type_name, boost::ref(m_engine)), "type_name");
m_engine.add(fun(&chaiscript::detail::Dispatch_Engine::function_exists, boost::ref(m_engine)), "function_exists");
m_engine.add(fun(&chaiscript::detail::Dispatch_Engine::get_type_name, boost::ref(m_engine)), "name");
typedef void (ChaiScript::*load_mod_1)(const std::string&);
typedef void (ChaiScript::*load_mod_2)(const std::string&, const std::string&);
m_engine.add(fun(static_cast<load_mod_1>(&ChaiScript::load_module), this), "load_module");
m_engine.add(fun(static_cast<load_mod_2>(&ChaiScript::load_module), this), "load_module");
add(standard_library::vector_type<std::vector<Boxed_Value> >("Vector"));
add(standard_library::string_type<std::string>("string"));
add(standard_library::map_type<std::map<std::string, Boxed_Value> >("Map"));
add(standard_library::pair_type<std::pair<Boxed_Value, Boxed_Value > >("Pair"));
m_engine.add(fun(&ChaiScript::use, this), "use");
m_engine.add(fun(&ChaiScript::internal_eval, this), "eval");
m_engine.add(fun(&ChaiScript::internal_eval_ast, this), "eval");
do_eval(chaiscript_prelude, "standard prelude");
}
public:
ChaiScript(const std::vector<std::string> &t_modulepaths = std::vector<std::string>(),
@@ -326,7 +384,7 @@ namespace chaiscript
struct State
{
std::set<std::string> used_files;
Dispatch_Engine::State engine_state;
chaiscript::detail::Dispatch_Engine::State engine_state;
std::set<std::string> active_loaded_modules;
};
@@ -337,10 +395,8 @@ namespace chaiscript
*/
State get_state()
{
#ifndef CHAISCRIPT_NO_THREADS
boost::lock_guard<boost::recursive_mutex> l(m_use_mutex);
boost::shared_lock<boost::shared_mutex> l2(m_mutex);
#endif
chaiscript::detail::threading::lock_guard<chaiscript::detail::threading::recursive_mutex> l(m_use_mutex);
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l2(m_mutex);
State s;
s.used_files = m_used_files;
@@ -354,10 +410,8 @@ namespace chaiscript
*/
void set_state(const State &t_state)
{
#ifndef CHAISCRIPT_NO_THREADS
boost::lock_guard<boost::recursive_mutex> l(m_use_mutex);
boost::shared_lock<boost::shared_mutex> l2(m_mutex);
#endif
chaiscript::detail::threading::lock_guard<chaiscript::detail::threading::recursive_mutex> l(m_use_mutex);
chaiscript::detail::threading::shared_lock<chaiscript::detail::threading::shared_mutex> l2(m_mutex);
m_used_files = t_state.used_files;
m_active_loaded_modules = t_state.active_loaded_modules;
@@ -388,7 +442,7 @@ namespace chaiscript
*/
void load_module(const std::string &t_module_name)
{
std::vector<load_module_error> errors;
std::vector<exception::load_module_error> errors;
std::vector<std::string> prefixes;
prefixes.push_back("lib");
@@ -409,7 +463,7 @@ namespace chaiscript
std::string name = m_modulepaths[i] + prefixes[j] + t_module_name + postfixes[k];
load_module(t_module_name, name);
return;
} catch (const load_module_error &e) {
} catch (const exception::load_module_error &e) {
errors.push_back(e);
// Try next set
}
@@ -419,7 +473,7 @@ namespace chaiscript
std::string errstring;
for (std::vector<load_module_error>::const_iterator itr = errors.begin();
for (std::vector<exception::load_module_error>::const_iterator itr = errors.begin();
itr != errors.end();
++itr)
{
@@ -431,7 +485,7 @@ namespace chaiscript
errstring += itr->what();
}
throw load_module_error("Unable to find module: " + t_module_name + " Errors: " + errstring);
throw exception::load_module_error("Unable to find module: " + t_module_name + " Errors: " + errstring);
}
/**
@@ -439,13 +493,11 @@ namespace chaiscript
*/
void load_module(const std::string &t_module_name, const std::string &t_filename)
{
#ifndef CHAISCRIPT_NO_THREADS
boost::lock_guard<boost::recursive_mutex> l(m_use_mutex);
#endif
chaiscript::detail::threading::lock_guard<chaiscript::detail::threading::recursive_mutex> l(m_use_mutex);
if (m_loaded_modules.count(t_module_name) == 0)
{
Loadable_Module_Ptr lm(new Loadable_Module(t_module_name, t_filename));
detail::Loadable_Module_Ptr lm(new detail::Loadable_Module(t_module_name, t_filename));
m_loaded_modules[t_module_name] = lm;
m_active_loaded_modules.insert(t_module_name);
add(lm->m_moduleptr);
@@ -456,19 +508,6 @@ namespace chaiscript
}
/**
* Helper for calling script code as if it were native C++ code
* example:
* boost::function<int (int, int)> f = build_functor(chai, "func(x, y){x+y}");
* \return a boost::function representing the passed in script
* \param[in] script Script code to build a function from
*/
template<typename FunctionType>
boost::function<FunctionType> functor(const std::string &t_script)
{
return chaiscript::functor<FunctionType>(eval(t_script));
}
/**
* Evaluate a string via eval method
*/
@@ -478,13 +517,6 @@ namespace chaiscript
}
/**
* Returns the current evaluation m_engine
*/
Dispatch_Engine &get_eval_engine() {
return m_engine;
}
/**
* Helper function for loading a file
*/
@@ -492,7 +524,7 @@ namespace chaiscript
std::ifstream infile(t_filename.c_str(), std::ios::in | std::ios::ate | std::ios::binary );
if (!infile.is_open()) {
throw File_Not_Found_Error(t_filename);
throw exception::file_not_found_error(t_filename);
}
std::streampos size = infile.tellg();
@@ -510,56 +542,6 @@ namespace chaiscript
}
}
/**
* Builds all the requirements for ChaiScript, including its evaluator and a run of its prelude.
*/
void build_eval_system() {
using namespace bootstrap;
m_engine.add_reserved_word("def");
m_engine.add_reserved_word("fun");
m_engine.add_reserved_word("while");
m_engine.add_reserved_word("for");
m_engine.add_reserved_word("if");
m_engine.add_reserved_word("else");
m_engine.add_reserved_word("&&");
m_engine.add_reserved_word("||");
m_engine.add_reserved_word(",");
m_engine.add_reserved_word(":=");
m_engine.add_reserved_word("var");
m_engine.add_reserved_word("return");
m_engine.add_reserved_word("break");
m_engine.add_reserved_word("true");
m_engine.add_reserved_word("false");
m_engine.add_reserved_word("_");
add(Bootstrap::bootstrap());
m_engine.add(fun(&Dispatch_Engine::dump_system, boost::ref(m_engine)), "dump_system");
m_engine.add(fun(&Dispatch_Engine::dump_object, boost::ref(m_engine)), "dump_object");
m_engine.add(fun(&Dispatch_Engine::is_type, boost::ref(m_engine)), "is_type");
m_engine.add(fun(&Dispatch_Engine::type_name, boost::ref(m_engine)), "type_name");
m_engine.add(fun(&Dispatch_Engine::function_exists, boost::ref(m_engine)), "function_exists");
m_engine.add(fun(&Dispatch_Engine::get_type_name, boost::ref(m_engine)), "name");
typedef void (ChaiScript::*load_mod_1)(const std::string&);
typedef void (ChaiScript::*load_mod_2)(const std::string&, const std::string&);
m_engine.add(fun(static_cast<load_mod_1>(&ChaiScript::load_module), this), "load_module");
m_engine.add(fun(static_cast<load_mod_2>(&ChaiScript::load_module), this), "load_module");
add(standard_library::vector_type<std::vector<Boxed_Value> >("Vector"));
add(standard_library::string_type<std::string>("string"));
add(standard_library::map_type<std::map<std::string, Boxed_Value> >("Map"));
add(standard_library::pair_type<std::pair<Boxed_Value, Boxed_Value > >("Pair"));
m_engine.add(fun(&ChaiScript::use, this), "use");
m_engine.add(fun(&ChaiScript::internal_eval, this), "eval");
m_engine.add(fun(&ChaiScript::internal_eval_ast, this), "eval");
do_eval(chaiscript_prelude, "standard prelude");
}
template<typename T>
T eval(const std::string &t_input)

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
// This file is distributed under the BSD License.
// See "license.txt" for details.
// Copyright 2009-2010, Jonathan Turner (jonathan@emptycrate.com)
// Copyright 2009-2011, Jonathan Turner (jonathan@emptycrate.com)
// and Jason Turner (jason@emptycrate.com)
// http://www.chaiscript.com