Add chaiscript namespace
This commit is contained in:
parent
785263628b
commit
ae67be1ecd
@ -12,13 +12,16 @@
|
||||
#include <map>
|
||||
#include <fstream>
|
||||
|
||||
class TokenType { public: enum Type { File, Whitespace, Identifier, Integer, Operator, Parens_Open, Parens_Close,
|
||||
namespace chaiscript
|
||||
{
|
||||
|
||||
class TokenType { public: enum Type { File, Whitespace, Identifier, Integer, Operator, Parens_Open, Parens_Close,
|
||||
Square_Open, Square_Close, Curly_Open, Curly_Close, Comma, Quoted_String, Single_Quoted_String, Carriage_Return, Semicolon,
|
||||
Function_Def, Lambda_Def, Scoped_Block, Statement, Equation, Return, Expression, Term, Factor, Negate, Not, Comment,
|
||||
Value, Fun_Call, Method_Call, Comparison, If_Block, While_Block, Boolean, Real_Number, Array_Call, Variable_Decl, Array_Init,
|
||||
For_Block, Prefix, Break }; };
|
||||
|
||||
const char *tokentype_to_string(int tokentype) {
|
||||
const char *tokentype_to_string(int tokentype) {
|
||||
const char *token_types[] = {"File", "Whitespace", "Identifier", "Integer", "Operator", "Parens_Open", "Parens_Close",
|
||||
"Square_Open", "Square_Close", "Curly_Open", "Curly_Close", "Comma", "Quoted_String", "Single_Quoted_String", "Carriage_Return", "Semicolon",
|
||||
"Function_Def", "Lambda_Def", "Scoped_Block", "Statement", "Equation", "Return", "Expression", "Term", "Factor", "Negate", "Not", "Comment",
|
||||
@ -26,6 +29,7 @@ const char *tokentype_to_string(int tokentype) {
|
||||
"For_Block", "Prefix", "Break" };
|
||||
|
||||
return token_types[tokentype];
|
||||
}
|
||||
}
|
||||
|
||||
#include "dispatchkit.hpp"
|
||||
|
@ -6,15 +6,15 @@
|
||||
|
||||
#include <exception>
|
||||
|
||||
//A function that prints any string passed to it
|
||||
|
||||
template <typename Eval_Engine>
|
||||
class ChaiScript_System {
|
||||
namespace chaiscript
|
||||
{
|
||||
template <typename Eval_Engine>
|
||||
class ChaiScript_System {
|
||||
langkit::Lexer lexer;
|
||||
langkit::Rule parser;
|
||||
Eval_Engine engine;
|
||||
|
||||
public:
|
||||
public:
|
||||
ChaiScript_System() : lexer(build_lexer()), parser(build_parser_rules()), engine(build_eval_system(lexer, parser)) {
|
||||
|
||||
}
|
||||
@ -265,9 +265,9 @@ public:
|
||||
dispatchkit::Boxed_Value evaluate_file(const char *filename) {
|
||||
return evaluate_string(load_file(filename), filename);
|
||||
}
|
||||
};
|
||||
|
||||
typedef ChaiScript_System<dispatchkit::Dispatch_Engine> ChaiScript_Engine;
|
||||
};
|
||||
|
||||
typedef ChaiScript_System<dispatchkit::Dispatch_Engine> ChaiScript_Engine;
|
||||
}
|
||||
#endif /* CHAISCRIPT_ENGINE_HPP_ */
|
||||
|
||||
|
@ -6,35 +6,37 @@
|
||||
|
||||
#include <map>
|
||||
|
||||
struct ParserError {
|
||||
namespace chaiscript
|
||||
{
|
||||
struct ParserError {
|
||||
std::string reason;
|
||||
langkit::TokenPtr location;
|
||||
|
||||
ParserError(const std::string &why, const langkit::TokenPtr where) : reason(why), location(where){ }
|
||||
};
|
||||
};
|
||||
|
||||
struct EvalError {
|
||||
struct EvalError {
|
||||
std::string reason;
|
||||
langkit::TokenPtr location;
|
||||
|
||||
EvalError(const std::string &why, const langkit::TokenPtr where) : reason(why), location(where) { }
|
||||
};
|
||||
};
|
||||
|
||||
struct ReturnValue {
|
||||
struct ReturnValue {
|
||||
dispatchkit::Boxed_Value retval;
|
||||
langkit::TokenPtr location;
|
||||
|
||||
ReturnValue(const dispatchkit::Boxed_Value &return_value, const langkit::TokenPtr where) : retval(return_value), location(where) { }
|
||||
};
|
||||
};
|
||||
|
||||
struct BreakLoop {
|
||||
struct BreakLoop {
|
||||
langkit::TokenPtr location;
|
||||
|
||||
BreakLoop(const langkit::TokenPtr where) : location(where) { }
|
||||
};
|
||||
};
|
||||
|
||||
template <typename Eval_System>
|
||||
const dispatchkit::Boxed_Value eval_function (Eval_System &ss, langkit::TokenPtr node, const std::vector<std::string> ¶m_names, const std::vector<dispatchkit::Boxed_Value> &vals) {
|
||||
template <typename Eval_System>
|
||||
const dispatchkit::Boxed_Value eval_function (Eval_System &ss, langkit::TokenPtr node, const std::vector<std::string> ¶m_names, const std::vector<dispatchkit::Boxed_Value> &vals) {
|
||||
ss.new_scope();
|
||||
|
||||
for (unsigned int i = 0; i < param_names.size(); ++i) {
|
||||
@ -44,10 +46,10 @@ const dispatchkit::Boxed_Value eval_function (Eval_System &ss, langkit::TokenPtr
|
||||
dispatchkit::Boxed_Value retval = eval_token(ss, node);
|
||||
ss.pop_scope();
|
||||
return retval;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Eval_System>
|
||||
dispatchkit::Boxed_Value eval_token(Eval_System &ss, langkit::TokenPtr node) {
|
||||
template <typename Eval_System>
|
||||
dispatchkit::Boxed_Value eval_token(Eval_System &ss, langkit::TokenPtr node) {
|
||||
dispatchkit::Boxed_Value retval;
|
||||
unsigned int i, j;
|
||||
|
||||
@ -445,6 +447,6 @@ dispatchkit::Boxed_Value eval_token(Eval_System &ss, langkit::TokenPtr node) {
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* CHAISCRIPT_EVAL_HPP_ */
|
||||
|
@ -3,25 +3,27 @@
|
||||
#include "chaiscript.hpp"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
using namespace dispatchkit;
|
||||
|
||||
std::string input;
|
||||
|
||||
ChaiScript_Engine chai;
|
||||
chaiscript::ChaiScript_Engine chai;
|
||||
|
||||
if (argc < 2) {
|
||||
std::cout << "eval> ";
|
||||
std::getline(std::cin, input);
|
||||
while (input != "quit") {
|
||||
Boxed_Value val;
|
||||
dispatchkit::Boxed_Value val;
|
||||
|
||||
val = chai.evaluate_string(input);
|
||||
|
||||
if (val.get_type_info().m_bare_type_info && *(val.get_type_info().m_bare_type_info) != typeid(void)) {
|
||||
try {
|
||||
Boxed_Value printeval = dispatch(chai.get_eval_engine().get_function("to_string"), Param_List_Builder() << val);
|
||||
dispatchkit::Boxed_Value printeval
|
||||
= dispatchkit::dispatch(chai.get_eval_engine().get_function("to_string"),
|
||||
dispatchkit::Param_List_Builder() << val);
|
||||
std::cout << "result: ";
|
||||
dispatch(chai.get_eval_engine().get_function("print"), Param_List_Builder() << printeval);
|
||||
dispatchkit::dispatch(chai.get_eval_engine().get_function("print"),
|
||||
dispatchkit::Param_List_Builder() << printeval);
|
||||
} catch (const std::runtime_error &e) {
|
||||
std::cout << "result: object #" << &val << std::endl;
|
||||
}
|
||||
@ -33,7 +35,7 @@ int main(int argc, char *argv[]) {
|
||||
else {
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
try {
|
||||
Boxed_Value val = chai.evaluate_file(argv[i]);
|
||||
dispatchkit::Boxed_Value val = chai.evaluate_file(argv[i]);
|
||||
}
|
||||
catch (std::exception &e) {
|
||||
std::cerr << "Could not open: " << argv[i] << std::endl;
|
||||
|
Loading…
x
Reference in New Issue
Block a user