Initial reworking of chaiscript parser to use the new chaioop parser

This commit is contained in:
Jonathan Turner 2009-06-30 18:17:15 +00:00
parent 2f27dc55fc
commit ee44ae0ca0
5 changed files with 1337 additions and 286 deletions

View File

@ -8,13 +8,19 @@
#include <boost/shared_ptr.hpp>
#include <boost/function.hpp>
#include <stdexcept>
#include <tr1/memory>
#include <iostream>
#include <map>
#include <fstream>
#include "dispatchkit.hpp"
#include "bootstrap.hpp"
#include "bootstrap_stl.hpp"
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,
@ -30,15 +36,101 @@ namespace chaiscript
return token_types[tokentype];
}
*/
class Token_Type { public: enum Type { Error, Int, Float, Id, Char, Str, Eol, 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 }; };
const char *token_type_to_string(int tokentype) {
const char *token_types[] = { "Internal Parser Error", "Int", "Float", "Id", "Char", "Str", "Eol", "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" };
return token_types[tokentype];
}
struct File_Position {
int line;
int column;
// std::string::iterator text_pos;
File_Position(int file_line, int file_column)
: line(file_line), column(file_column) { }
File_Position() : line(0), column(0) { }
};
typedef std::tr1::shared_ptr<struct Token> TokenPtr;
struct Token {
std::string text;
int identifier;
const char *filename;
File_Position start, end;
std::vector<TokenPtr> children;
Token(const std::string &token_text, int id, const char *fname) : text(token_text), identifier(id), filename(fname) { }
Token(const std::string &token_text, int id, const char *fname, int start_line, int start_col, int end_line, int end_col) :
text(token_text), identifier(id), filename(fname) {
start.line = start_line;
start.column = start_col;
end.line = end_line;
end.column = end_col;
}
};
struct Parse_Error {
std::string reason;
File_Position position;
const char *filename;
Parse_Error(const std::string &why, const File_Position &where, const char *fname) :
reason(why), position(where), filename(fname) { }
Parse_Error(const std::string &why, const TokenPtr &where) : reason(why) {
filename = where->filename;
position = where->start;
}
virtual ~Parse_Error() throw() {}
};
struct ParserError {
std::string reason;
TokenPtr location;
ParserError(const std::string &why, const TokenPtr where) : reason(why), location(where){ }
};
struct EvalError : public std::runtime_error {
std::string reason;
TokenPtr location;
EvalError(const std::string &why, const TokenPtr where)
: std::runtime_error("Eval error: \"" + why + "\" in '"
+ where->filename + "' line: " + boost::lexical_cast<std::string>(where->start.line+1)),
reason(why), location(where) { }
virtual ~EvalError() throw() {}
};
struct ReturnValue {
dispatchkit::Boxed_Value retval;
TokenPtr location;
ReturnValue(const dispatchkit::Boxed_Value &return_value, const TokenPtr where) : retval(return_value), location(where) { }
};
struct BreakLoop {
TokenPtr location;
BreakLoop(const TokenPtr where) : location(where) { }
};
}
#include "dispatchkit.hpp"
#include "bootstrap.hpp"
#include "bootstrap_stl.hpp"
#include "langkit_lexer.hpp"
#include "langkit_parser.hpp"
#include "chaiscript_eval.hpp"
#include "chaiscript_engine.hpp"

File diff suppressed because it is too large Load Diff

View File

@ -8,40 +8,8 @@
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 : public std::runtime_error {
std::string reason;
langkit::TokenPtr location;
EvalError(const std::string &why, const langkit::TokenPtr where)
: std::runtime_error("Eval error: \"" + why + "\" in '"
+ where->filename + "' line: " + boost::lexical_cast<std::string>(where->start.line+1)),
reason(why), location(where) { }
virtual ~EvalError() throw() {}
};
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 {
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> &param_names, const std::vector<dispatchkit::Boxed_Value> &vals) {
const dispatchkit::Boxed_Value eval_function (Eval_System &ss, TokenPtr node, const std::vector<std::string> &param_names, const std::vector<dispatchkit::Boxed_Value> &vals) {
ss.new_scope();
for (unsigned int i = 0; i < param_names.size(); ++i) {
@ -61,18 +29,17 @@ namespace chaiscript
}
template <typename Eval_System>
dispatchkit::Boxed_Value eval_token(Eval_System &ss, langkit::TokenPtr node) {
dispatchkit::Boxed_Value eval_token(Eval_System &ss, TokenPtr node) {
dispatchkit::Boxed_Value retval;
unsigned int i, j;
switch (node->identifier) {
case (TokenType::Value) :
case (TokenType::File) :
case (Token_Type::File) :
for (i = 0; i < node->children.size(); ++i) {
retval = eval_token(ss, node->children[i]);
}
break;
case (TokenType::Identifier) :
case (Token_Type::Id) :
if (node->text == "true") {
retval = dispatchkit::Boxed_Value(true);
}
@ -88,19 +55,19 @@ namespace chaiscript
}
}
break;
case (TokenType::Real_Number) :
case (Token_Type::Float) :
retval = dispatchkit::Boxed_Value(double(atof(node->text.c_str())));
break;
case (TokenType::Integer) :
case (Token_Type::Int) :
retval = dispatchkit::Boxed_Value(atoi(node->text.c_str()));
break;
case (TokenType::Quoted_String) :
case (Token_Type::Quoted_String) :
retval = dispatchkit::Boxed_Value(node->text);
break;
case (TokenType::Single_Quoted_String) :
case (Token_Type::Single_Quoted_String) :
retval = dispatchkit::Boxed_Value(node->text);
break;
case (TokenType::Equation) :
case (Token_Type::Equation) :
retval = eval_token(ss, node->children.back());
if (node->children.size() > 1) {
for (i = node->children.size()-3; ((int)i) >= 0; i -= 2) {
@ -116,16 +83,15 @@ namespace chaiscript
}
}
break;
case (TokenType::Variable_Decl): {
case (Token_Type::Var_Decl): {
ss.add_object(node->children[0]->text, dispatchkit::Boxed_Value());
retval = ss.get_object(node->children[0]->text);
}
break;
case (TokenType::Factor) :
case (TokenType::Expression) :
case (TokenType::Term) :
case (TokenType::Boolean) :
case (TokenType::Comparison) : {
case (Token_Type::Comparison) :
case (Token_Type::Expression) :
case (Token_Type::Additive) :
case (Token_Type::Multiplicative) : {
retval = eval_token(ss, node->children[0]);
if (node->children.size() > 1) {
for (i = 1; i < node->children.size(); i += 2) {
@ -143,7 +109,7 @@ namespace chaiscript
}
}
break;
case (TokenType::Array_Call) : {
case (Token_Type::Array_Call) : {
retval = eval_token(ss, node->children[0]);
for (i = 1; i < node->children.size(); ++i) {
dispatchkit::Param_List_Builder plb;
@ -161,8 +127,8 @@ namespace chaiscript
}
}
break;
case (TokenType::Negate) : {
retval = eval_token(ss, node->children[1]);
case (Token_Type::Negate) : {
retval = eval_token(ss, node->children[0]);
dispatchkit::Param_List_Builder plb;
plb << retval;
plb << dispatchkit::Boxed_Value(-1);
@ -175,10 +141,10 @@ namespace chaiscript
}
}
break;
case (TokenType::Not) : {
case (Token_Type::Not) : {
bool cond;
try {
retval = eval_token(ss, node->children[1]);
retval = eval_token(ss, node->children[0]);
cond = dispatchkit::boxed_cast<bool &>(retval);
}
catch (std::exception) {
@ -187,7 +153,8 @@ namespace chaiscript
retval = dispatchkit::Boxed_Value(!cond);
}
break;
case (TokenType::Prefix) : {
/*
case (Token_Type::Prefix) : {
retval = eval_token(ss, node->children[1]);
dispatchkit::Param_List_Builder plb;
plb << retval;
@ -200,16 +167,19 @@ namespace chaiscript
}
}
break;
case (TokenType::Array_Init) : {
*/
case (Token_Type::Inline_Array) : {
try {
retval = dispatch(ss.get_function("Vector"), dispatchkit::Param_List_Builder());
for (i = 0; i < node->children.size(); ++i) {
try {
dispatchkit::Boxed_Value tmp = eval_token(ss, node->children[i]);
dispatch(ss.get_function("push_back"), dispatchkit::Param_List_Builder() << retval << tmp);
}
catch (const dispatchkit::dispatch_error &inner_e) {
throw EvalError("Can not find appropriate 'push_back'", node->children[i]);
if (node->children.size() > 0) {
for (i = 0; i < node->children[0]->children.size(); ++i) {
try {
dispatchkit::Boxed_Value tmp = eval_token(ss, node->children[0]->children[i]);
dispatch(ss.get_function("push_back"), dispatchkit::Param_List_Builder() << retval << tmp);
}
catch (const dispatchkit::dispatch_error &inner_e) {
throw EvalError("Can not find appropriate 'push_back'", node->children[0]->children[i]);
}
}
}
}
@ -218,6 +188,7 @@ namespace chaiscript
}
}
break;
/*
case (TokenType::Map_Init) : {
try {
retval = dispatch(ss.get_function("Map"), dispatchkit::Param_List_Builder());
@ -237,7 +208,9 @@ namespace chaiscript
}
}
break;
case (TokenType::Fun_Call) : {
*/
case (Token_Type::Fun_Call) : {
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();
@ -245,9 +218,10 @@ namespace chaiscript
dispatchkit::Dispatch_Engine::Stack new_stack;
new_stack.push_back(dispatchkit::Dispatch_Engine::Scope());
dispatchkit::Param_List_Builder plb;
for (i = 1; i < node->children.size(); ++i) {
plb << eval_token(ss, node->children[i]);
if ((node->children.size() > 1) && (node->children[1]->identifier == Token_Type::Arg_List)) {
for (i = 0; i < node->children[1]->children.size(); ++i) {
plb << eval_token(ss, node->children[1]->children[i]);
}
}
try {
fn = ss.get_function(node->children[0]->text);
@ -269,7 +243,8 @@ namespace chaiscript
}
}
break;
case (TokenType::Method_Call) : {
case (Token_Type::Dot_Access) : {
std::vector<std::pair<std::string, dispatchkit::Dispatch_Engine::Function_Map::mapped_type> > fn;
dispatchkit::Dispatch_Engine::Stack prev_stack = ss.get_stack();
@ -282,12 +257,15 @@ namespace chaiscript
dispatchkit::Param_List_Builder plb;
plb << retval;
for (j = 1; j < node->children[i]->children.size(); ++j) {
plb << eval_token(ss, node->children[i]->children[j]);
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]);
}
}
std::string fun_name;
if (node->children[i]->identifier == TokenType::Fun_Call) {
if (node->children[i]->identifier == Token_Type::Fun_Call) {
fun_name = node->children[i]->children[0]->text;
}
else {
@ -316,7 +294,8 @@ namespace chaiscript
}
}
break;
case(TokenType::If_Block) : {
case(Token_Type::If) : {
retval = eval_token(ss, node->children[0]);
bool cond;
try {
@ -354,7 +333,7 @@ namespace chaiscript
}
}
break;
case(TokenType::While_Block) : {
case(Token_Type::While) : {
retval = eval_token(ss, node->children[0]);
bool cond;
try {
@ -381,6 +360,7 @@ namespace chaiscript
retval = dispatchkit::Boxed_Value();
}
break;
/*
case(TokenType::For_Block) : {
dispatchkit::Boxed_Value condition;
bool cond;
@ -423,31 +403,37 @@ namespace chaiscript
retval = dispatchkit::Boxed_Value();
}
break;
case (TokenType::Function_Def) : {
unsigned int num_args = node->children.size() - 2;
*/
case (Token_Type::Def) : {
std::vector<std::string> param_names;
for (i = 0; i < num_args; ++i) {
param_names.push_back(node->children[i+1]->text);
if ((node->children.size() > 1) && (node->children[1]->identifier == Token_Type::Arg_List)) {
for (i = 0; i < node->children[1]->children.size(); ++i) {
param_names.push_back(node->children[1]->children[i]->text);
}
}
ss.register_function(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), num_args)), node->children[0]->text);
new dispatchkit::Dynamic_Proxy_Function(boost::bind(&eval_function<Eval_System>, boost::ref(ss), node->children.back(), param_names, _1), node->children[1]->children.size())), node->children[0]->text);
}
break;
case (TokenType::Lambda_Def) : {
unsigned int num_args = node->children.size() - 1;
case (Token_Type::Lambda) : {
std::vector<std::string> param_names;
for (i = 0; i < num_args; ++i) {
param_names.push_back(node->children[i]->text);
if ((node->children.size() > 0) && (node->children[0]->identifier == Token_Type::Arg_List)) {
for (i = 0; i < node->children[0]->children.size(); ++i) {
param_names.push_back(node->children[0]->children[i]->text);
}
}
//retval = boost::shared_ptr<dispatchkit::Proxy_Function>(new dispatchkit::Proxy_Function_Impl<boost::function<void (const std::string &)> >(&test));
retval = 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), num_args)));
boost::bind(&eval_function<Eval_System>, boost::ref(ss), node->children.back(), param_names, _1), node->children[0]->children.size())));
}
break;
case (TokenType::Scoped_Block) : {
case (Token_Type::Block) : {
ss.new_scope();
for (i = 0; i < node->children.size(); ++i) {
retval = eval_token(ss, node->children[i]);
@ -455,7 +441,8 @@ namespace chaiscript
ss.pop_scope();
}
break;
case (TokenType::Return) : {
case (Token_Type::Return) : {
if (node->children.size() > 0) {
retval = eval_token(ss, node->children[0]);
}
@ -465,6 +452,7 @@ namespace chaiscript
throw ReturnValue(retval, node);
}
break;
/*
case (TokenType::Break) : {
throw BreakLoop(node);
}
@ -483,6 +471,7 @@ namespace chaiscript
case (TokenType::Curly_Close) :
case (TokenType::Comma) :
break;
*/
}
return retval;

View File

@ -7,12 +7,13 @@ int main(int argc, char *argv[]) {
std::string input;
chaiscript::ChaiScript_Engine chai;
chai.build_eval_system();
if (argc < 2) {
std::cout << "eval> ";
std::getline(std::cin, input);
while (input != "quit") {
dispatchkit::Boxed_Value val;
dispatchkit::Boxed_Value val;
val = chai.evaluate_string(input);
@ -25,7 +26,7 @@ int main(int argc, char *argv[]) {
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;
std::cout << "result: object #" << &val << " Error: " << e.what() << std::endl;
}
}
std::cout << "eval> ";

View File

@ -4,6 +4,7 @@
#include "function_call.hpp"
#include <boost/function.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/regex.hpp>
std::string load_text_file(const std::string &filename)