Add prefix ++ and --

This commit is contained in:
Jonathan Turner 2009-06-30 19:34:16 +00:00
parent e6a0dc9af6
commit 2cba593c53
3 changed files with 29 additions and 21 deletions

View File

@ -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 }; }; Lambda, Block, Def, While, If, For, Inline_Array, Inline_Map, Return, File, Prefix }; };
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" }; "Lambda", "Block", "Def", "While", "If", "For", "Inline_Array", "Inline_Map", "Return", "File", "Prefix" };
return token_types[tokentype]; return token_types[tokentype];
} }

View File

@ -153,7 +153,6 @@ namespace chaiscript
retval = dispatchkit::Boxed_Value(!cond); retval = dispatchkit::Boxed_Value(!cond);
} }
break; break;
/*
case (Token_Type::Prefix) : { case (Token_Type::Prefix) : {
retval = eval_token(ss, node->children[1]); retval = eval_token(ss, node->children[1]);
dispatchkit::Param_List_Builder plb; dispatchkit::Param_List_Builder plb;
@ -167,7 +166,6 @@ namespace chaiscript
} }
} }
break; break;
*/
case (Token_Type::Inline_Array) : { case (Token_Type::Inline_Array) : {
try { try {
retval = dispatch(ss.get_function("Vector"), dispatchkit::Param_List_Builder()); retval = dispatch(ss.get_function("Vector"), dispatchkit::Param_List_Builder());

View File

@ -542,10 +542,12 @@ namespace chaiscript
line = prev_line; line = prev_line;
return false; return false;
} }
std::string match(start, input_pos); else {
TokenPtr t(new Token(match, Token_Type::Str, filename, prev_line, prev_col, line, col)); std::string match(start, input_pos);
match_stack.push_back(t); TokenPtr t(new Token(match, Token_Type::Str, filename, prev_line, prev_col, line, col));
return true; match_stack.push_back(t);
return true;
}
} }
else { else {
return false; return false;
@ -905,7 +907,7 @@ namespace chaiscript
} }
bool Value() { bool Value() {
if (Var_Decl() || Lambda() || Id_Fun_Array() || Num(true) || Negate() || Not() || Quoted_String(true) || Single_Quoted_String(true) || if (Var_Decl() || Lambda() || Id_Fun_Array() || Num(true) || Prefix() || Quoted_String(true) || Single_Quoted_String(true) ||
Paren_Expression() || Inline_Container() || Id_Literal()) { Paren_Expression() || Inline_Container() || Id_Literal()) {
return true; return true;
} }
@ -914,7 +916,7 @@ namespace chaiscript
} }
} }
bool Negate() { bool Prefix() {
bool retval = false; bool retval = false;
int prev_stack_top = match_stack.size(); int prev_stack_top = match_stack.size();
@ -928,17 +930,7 @@ namespace chaiscript
build_match(Token_Type::Negate, prev_stack_top); build_match(Token_Type::Negate, prev_stack_top);
} }
else if (Symbol("!")) {
return retval;
}
bool Not() {
bool retval = false;
int prev_stack_top = match_stack.size();
if (Symbol("!")) {
retval = true; retval = true;
if (!Expression()) { if (!Expression()) {
@ -947,6 +939,24 @@ namespace chaiscript
build_match(Token_Type::Not, prev_stack_top); build_match(Token_Type::Not, prev_stack_top);
} }
if (Symbol("++", true)) {
retval = true;
if (!Expression()) {
throw Parse_Error("Incomplete '++' expression", File_Position(line, col), filename);
}
build_match(Token_Type::Prefix, prev_stack_top);
}
else if (Symbol("--", true)) {
retval = true;
if (!Expression()) {
throw Parse_Error("Incomplete '--' expression", File_Position(line, col), filename);
}
build_match(Token_Type::Prefix, prev_stack_top);
}
return retval; return retval;