Added inline map init using ecmascript syntax
This commit is contained in:
parent
d2d768e12c
commit
e3db4d3595
@ -18,15 +18,15 @@ 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 }; };
|
||||
Value, Fun_Call, Method_Call, Comparison, If_Block, While_Block, Boolean, Real_Number, Array_Call, Variable_Decl, Array_Init, Map_Init,
|
||||
For_Block, Prefix, Break, Map_Pair }; };
|
||||
|
||||
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",
|
||||
"Value", "Fun_Call", "Method_Call", "Comparison", "If_Block", "While_Block", "Boolean", "Real Number", "Array_Call", "Variable_Decl", "Array_Init",
|
||||
"For_Block", "Prefix", "Break" };
|
||||
"Value", "Fun_Call", "Method_Call", "Comparison", "If_Block", "While_Block", "Boolean", "Real Number", "Array_Call", "Variable_Decl", "Array_Init", "Map_Init",
|
||||
"For_Block", "Prefix", "Break", "Map_Pair" };
|
||||
|
||||
return token_types[tokentype];
|
||||
}
|
||||
|
@ -87,7 +87,7 @@ namespace chaiscript
|
||||
lexer << Pattern("[A-Za-z_][A-Za-z_0-9]*", TokenType::Identifier);
|
||||
lexer << Pattern("[0-9]+\\.[0-9]+", TokenType::Real_Number);
|
||||
lexer << Pattern("[0-9]+", TokenType::Integer);
|
||||
lexer << Pattern("[!@#$%^&*|\\-+=<>.]+|/[!@#$%^&|\\-+=<>]*", TokenType::Operator);
|
||||
lexer << Pattern("[!@#$%^&*|\\-+=<>.:]+|/[!@#$%^&|\\-+=<>]*", TokenType::Operator);
|
||||
lexer << Pattern("\\(", TokenType::Parens_Open);
|
||||
lexer << Pattern("\\)", TokenType::Parens_Close);
|
||||
lexer << Pattern("\\[", TokenType::Square_Open);
|
||||
@ -126,6 +126,8 @@ namespace chaiscript
|
||||
Rule arraycall(TokenType::Array_Call);
|
||||
Rule vardecl(TokenType::Variable_Decl);
|
||||
Rule arrayinit(TokenType::Array_Init);
|
||||
Rule mapinit(TokenType::Map_Init);
|
||||
Rule mappair(TokenType::Map_Pair);
|
||||
|
||||
Rule return_statement(TokenType::Return);
|
||||
Rule break_statement(TokenType::Break);
|
||||
@ -166,7 +168,7 @@ namespace chaiscript
|
||||
expression = term >> *((Str("+") >> term) | (Str("-") >> term));
|
||||
term = factor >> *((Str("*") >> factor) | (Str("/") >> factor));
|
||||
factor = methodcall | arraycall | value | negate | boolean_not | prefix | (Ign(Str("+")) >> value);
|
||||
value = vardecl | arrayinit | block | paren_block | lambda_def | return_statement | break_statement |
|
||||
value = vardecl | mapinit | arrayinit | block | paren_block | lambda_def | return_statement | break_statement |
|
||||
funcall | Id(TokenType::Identifier) | Id(TokenType::Real_Number) | Id(TokenType::Integer) | Id(TokenType::Quoted_String) |
|
||||
Id(TokenType::Single_Quoted_String) ;
|
||||
|
||||
@ -178,6 +180,9 @@ namespace chaiscript
|
||||
arraycall = value >> +((Ign(Id(TokenType::Square_Open)) >> boolean >> Ign(Id(TokenType::Square_Close))));
|
||||
|
||||
arrayinit = Ign(Id(TokenType::Square_Open)) >> ~(boolean >> *(Ign(Str(",")) >> boolean)) >> Ign(Id(TokenType::Square_Close));
|
||||
mapinit = Ign(Id(TokenType::Square_Open)) >> ~(mappair >> *(Ign(Str(",")) >> mappair)) >> Ign(Id(TokenType::Square_Close));
|
||||
mappair = Id(TokenType::Quoted_String) >> Ign(Str(":")) >> boolean;
|
||||
|
||||
vardecl = Ign(Str("var")) >> Id(TokenType::Identifier);
|
||||
return_statement = Ign(Str("return")) >> ~boolean;
|
||||
break_statement = Wrap(Ign(Str("break")));
|
||||
|
@ -206,6 +206,25 @@ namespace chaiscript
|
||||
}
|
||||
}
|
||||
break;
|
||||
case (TokenType::Map_Init) : {
|
||||
try {
|
||||
retval = dispatch(ss.get_function("Map"), dispatchkit::Param_List_Builder());
|
||||
for (i = 0; i < node->children.size(); ++i) {
|
||||
try {
|
||||
dispatchkit::Boxed_Value key = eval_token(ss, node->children[i]->children[0]);
|
||||
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]));
|
||||
}
|
||||
catch (std::exception inner_e) {
|
||||
throw EvalError("Can not find appropriate '=' for map init", node->children[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (std::exception e) {
|
||||
throw EvalError("Can not find appropriate 'Vector()'", node);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case (TokenType::Fun_Call) : {
|
||||
|
||||
std::vector<std::pair<std::string, dispatchkit::Dispatch_Engine::Function_Map::mapped_type> > fn;
|
||||
|
Loading…
x
Reference in New Issue
Block a user