Added inline map creation
This commit is contained in:
parent
2cba593c53
commit
bb174b37a6
@ -39,12 +39,12 @@ namespace chaiscript
|
|||||||
*/
|
*/
|
||||||
class Token_Type { public: enum Type { Error, Int, Float, Id, Char, Str, Eol, Fun_Call, Arg_List, Variable, Equation, Var_Decl,
|
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,
|
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, Prefix }; };
|
Lambda, Block, Def, While, If, For, Inline_Array, Inline_Map, Return, File, Prefix, Break, Map_Pair }; };
|
||||||
|
|
||||||
const char *token_type_to_string(int tokentype) {
|
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",
|
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",
|
"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", "Prefix" };
|
"Lambda", "Block", "Def", "While", "If", "For", "Inline_Array", "Inline_Map", "Return", "File", "Prefix", "Break", "Map_Pair" };
|
||||||
|
|
||||||
return token_types[tokentype];
|
return token_types[tokentype];
|
||||||
}
|
}
|
||||||
|
@ -85,11 +85,9 @@ namespace chaiscript
|
|||||||
dispatchkit::Boxed_Value value;
|
dispatchkit::Boxed_Value value;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
parser.clear_match_stack();
|
|
||||||
if (parser.parse(input, filename)) {
|
if (parser.parse(input, filename)) {
|
||||||
//show_match_stack();
|
//parser.show_match_stack();
|
||||||
value = eval_token<Eval_Engine>(engine, parser.ast());
|
value = eval_token<Eval_Engine>(engine, parser.ast());
|
||||||
//clear_match_stack();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (const ReturnValue &rv) {
|
catch (const ReturnValue &rv) {
|
||||||
@ -97,7 +95,6 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
catch (Parse_Error &pe) {
|
catch (Parse_Error &pe) {
|
||||||
std::cout << pe.reason << " in " << pe.filename << " at " << pe.position.line << ", " << pe.position.column << std::endl;
|
std::cout << pe.reason << " in " << pe.filename << " at " << pe.position.line << ", " << pe.position.column << std::endl;
|
||||||
parser.clear_match_stack();
|
|
||||||
}
|
}
|
||||||
catch (EvalError &ee) {
|
catch (EvalError &ee) {
|
||||||
if (filename != std::string("__EVAL__")) {
|
if (filename != std::string("__EVAL__")) {
|
||||||
@ -110,6 +107,7 @@ namespace chaiscript
|
|||||||
catch (std::exception &e) {
|
catch (std::exception &e) {
|
||||||
std::cout << "Exception: " << e.what() << std::endl;
|
std::cout << "Exception: " << e.what() << std::endl;
|
||||||
}
|
}
|
||||||
|
parser.clear_match_stack();
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
@ -186,27 +186,25 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
/*
|
case (Token_Type::Inline_Map) : {
|
||||||
case (TokenType::Map_Init) : {
|
|
||||||
try {
|
try {
|
||||||
retval = dispatch(ss.get_function("Map"), dispatchkit::Param_List_Builder());
|
retval = dispatch(ss.get_function("Map"), dispatchkit::Param_List_Builder());
|
||||||
for (i = 0; i < node->children.size(); ++i) {
|
for (i = 0; i < node->children[0]->children.size(); ++i) {
|
||||||
try {
|
try {
|
||||||
dispatchkit::Boxed_Value key = eval_token(ss, node->children[i]->children[0]);
|
dispatchkit::Boxed_Value key = eval_token(ss, node->children[0]->children[i]->children[0]);
|
||||||
dispatchkit::Boxed_Value slot = dispatch(ss.get_function("[]"), dispatchkit::Param_List_Builder() << retval << key);
|
dispatchkit::Boxed_Value slot = dispatch(ss.get_function("[]"), dispatchkit::Param_List_Builder() << retval << key);
|
||||||
dispatch(ss.get_function("="), dispatchkit::Param_List_Builder() << slot << eval_token(ss, node->children[i]->children[1]));
|
dispatch(ss.get_function("="), dispatchkit::Param_List_Builder() << slot << eval_token(ss, node->children[0]->children[i]->children[1]));
|
||||||
}
|
}
|
||||||
catch (const dispatchkit::dispatch_error &inner_e) {
|
catch (const dispatchkit::dispatch_error &inner_e) {
|
||||||
throw EvalError("Can not find appropriate '=' for map init", node->children[i]);
|
throw EvalError("Can not find appropriate '=' for map init", node->children[0]->children[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (const dispatchkit::dispatch_error &e) {
|
catch (const dispatchkit::dispatch_error &e) {
|
||||||
throw EvalError("Can not find appropriate 'Vector()'", node);
|
throw EvalError("Can not find appropriate 'Map()'", node);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
*/
|
|
||||||
case (Token_Type::Fun_Call) : {
|
case (Token_Type::Fun_Call) : {
|
||||||
dispatchkit::Param_List_Builder plb;
|
dispatchkit::Param_List_Builder plb;
|
||||||
|
|
||||||
@ -461,11 +459,11 @@ namespace chaiscript
|
|||||||
throw ReturnValue(retval, node);
|
throw ReturnValue(retval, node);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
/*
|
case (Token_Type::Break) : {
|
||||||
case (TokenType::Break) : {
|
|
||||||
throw BreakLoop(node);
|
throw BreakLoop(node);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
/*
|
||||||
case (TokenType::Statement) :
|
case (TokenType::Statement) :
|
||||||
case (TokenType::Carriage_Return) :
|
case (TokenType::Carriage_Return) :
|
||||||
case (TokenType::Semicolon) :
|
case (TokenType::Semicolon) :
|
||||||
|
@ -613,6 +613,27 @@ namespace chaiscript
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Container_Arg_List() {
|
||||||
|
bool retval = false;
|
||||||
|
|
||||||
|
int prev_stack_top = match_stack.size();
|
||||||
|
|
||||||
|
if (Map_Pair()) {
|
||||||
|
retval = true;
|
||||||
|
if (Char(',')) {
|
||||||
|
do {
|
||||||
|
if (!Map_Pair()) {
|
||||||
|
throw Parse_Error("Unexpected value in container", match_stack.back());
|
||||||
|
}
|
||||||
|
} while (retval && Char(','));
|
||||||
|
}
|
||||||
|
build_match(Token_Type::Arg_List, prev_stack_top);
|
||||||
|
}
|
||||||
|
|
||||||
|
return retval;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
bool Lambda() {
|
bool Lambda() {
|
||||||
bool retval = false;
|
bool retval = false;
|
||||||
|
|
||||||
@ -762,6 +783,20 @@ namespace chaiscript
|
|||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Break() {
|
||||||
|
bool retval = false;
|
||||||
|
|
||||||
|
int prev_stack_top = match_stack.size();
|
||||||
|
|
||||||
|
if (Keyword("break")) {
|
||||||
|
retval = true;
|
||||||
|
|
||||||
|
build_match(Token_Type::Break, prev_stack_top);
|
||||||
|
}
|
||||||
|
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
|
||||||
bool Id_Fun_Array() {
|
bool Id_Fun_Array() {
|
||||||
bool retval = false;
|
bool retval = false;
|
||||||
std::string::iterator prev_pos = input_pos;
|
std::string::iterator prev_pos = input_pos;
|
||||||
@ -852,11 +887,21 @@ namespace chaiscript
|
|||||||
|
|
||||||
if (Char('[')) {
|
if (Char('[')) {
|
||||||
retval = true;
|
retval = true;
|
||||||
Arg_List();
|
Container_Arg_List();
|
||||||
if (!Char(']')) {
|
if (!Char(']')) {
|
||||||
throw Parse_Error("Missing closing square bracket", File_Position(line, col), filename);
|
throw Parse_Error("Missing closing square bracket", File_Position(line, col), filename);
|
||||||
}
|
}
|
||||||
build_match(Token_Type::Inline_Array, prev_stack_top);
|
if ((prev_stack_top != match_stack.size()) && (match_stack.back()->children.size() > 0)) {
|
||||||
|
if (match_stack.back()->children[0]->identifier == Token_Type::Map_Pair) {
|
||||||
|
build_match(Token_Type::Inline_Map, prev_stack_top);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
build_match(Token_Type::Inline_Array, prev_stack_top);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
build_match(Token_Type::Inline_Array, prev_stack_top);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return retval;
|
return retval;
|
||||||
@ -1058,7 +1103,7 @@ namespace chaiscript
|
|||||||
if (Symbol("&&", true) || Symbol("||", true)) {
|
if (Symbol("&&", true) || Symbol("||", true)) {
|
||||||
do {
|
do {
|
||||||
if (!Comparison()) {
|
if (!Comparison()) {
|
||||||
throw Parse_Error("Incomplete expression", File_Position(line, col), filename);
|
throw Parse_Error("Incomplete expression", File_Position(line, col), filename);
|
||||||
}
|
}
|
||||||
} while (retval && (Symbol("&&", true) || Symbol("||", true)));
|
} while (retval && (Symbol("&&", true) || Symbol("||", true)));
|
||||||
|
|
||||||
@ -1069,6 +1114,27 @@ namespace chaiscript
|
|||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Map_Pair() {
|
||||||
|
bool retval = false;
|
||||||
|
|
||||||
|
int prev_stack_top = match_stack.size();
|
||||||
|
|
||||||
|
if (Expression()) {
|
||||||
|
retval = true;
|
||||||
|
if (Symbol(":")) {
|
||||||
|
do {
|
||||||
|
if (!Expression()) {
|
||||||
|
throw Parse_Error("Incomplete map pair", File_Position(line, col), filename);
|
||||||
|
}
|
||||||
|
} while (retval && Symbol(":"));
|
||||||
|
|
||||||
|
build_match(Token_Type::Map_Pair, prev_stack_top);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
|
||||||
bool Equation() {
|
bool Equation() {
|
||||||
bool retval = false;
|
bool retval = false;
|
||||||
|
|
||||||
@ -1090,7 +1156,7 @@ namespace chaiscript
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool Statement() {
|
bool Statement() {
|
||||||
if (Return() || Equation()) {
|
if (Return() || Break() || Equation()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -26,7 +26,7 @@ int main(int argc, char *argv[]) {
|
|||||||
dispatchkit::dispatch(chai.get_eval_engine().get_function("print"),
|
dispatchkit::dispatch(chai.get_eval_engine().get_function("print"),
|
||||||
dispatchkit::Param_List_Builder() << printeval);
|
dispatchkit::Param_List_Builder() << printeval);
|
||||||
} catch (const std::runtime_error &e) {
|
} catch (const std::runtime_error &e) {
|
||||||
std::cout << "result: object #" << &val << " Error: " << e.what() << std::endl;
|
//std::cout << "result: object #" << &val << " Error: " << e.what() << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
std::cout << "eval> ";
|
std::cout << "eval> ";
|
||||||
|
Loading…
x
Reference in New Issue
Block a user