Add bit shifting operators

This commit is contained in:
Jonathan Turner
2009-10-13 03:35:01 +00:00
parent b1e892487f
commit 12e909d9aa
6 changed files with 45 additions and 4 deletions

View File

@@ -50,6 +50,18 @@ namespace chaiscript
return p1 % p2; return p1 % p2;
} }
template<typename Ret, typename P1, typename P2>
Ret shift_left(P1 p1, P2 p2)
{
return p1 << p2;
}
template<typename Ret, typename P1, typename P2>
Ret shift_right(P1 p1, P2 p2)
{
return p1 >> p2;
}
template<typename P1, typename P2> template<typename P1, typename P2>
P1 &assign(P1 &p1, const P2 &p2) P1 &assign(P1 &p1, const P2 &p2)
{ {
@@ -717,6 +729,8 @@ namespace chaiscript
opers_arithmetic_pod(m); opers_arithmetic_pod(m);
m->add(fun(&detail::modulus<int, int, int>), "%"); m->add(fun(&detail::modulus<int, int, int>), "%");
m->add(fun(&detail::shift_left<int, int, int>), "<<");
m->add(fun(&detail::shift_right<int, int, int>), ">>");
m->add(fun(&print), "print_string"); m->add(fun(&print), "print_string");
m->add(fun(&println), "println_string"); m->add(fun(&println), "println_string");

View File

@@ -24,7 +24,7 @@ namespace chaiscript
class Token_Type { public: enum Type { Error, Int, Float, Id, Char, Str, Eol, Fun_Call, Inplace_Fun_Call, Arg_List, Variable, Equation, Var_Decl, class Token_Type { public: enum Type { Error, Int, Float, Id, Char, Str, Eol, Fun_Call, Inplace_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, Break, Map_Pair, Value_Range, Lambda, Block, Def, While, If, For, Inline_Array, Inline_Map, Return, File, Prefix, Break, Map_Pair, Value_Range,
Inline_Range, Annotation, Try, Catch, Finally, Method, Attr_Decl }; }; Inline_Range, Annotation, Try, Catch, Finally, Method, Attr_Decl, Shift }; };
namespace namespace
{ {
@@ -35,7 +35,7 @@ namespace chaiscript
const char *token_types[] = { "Internal Parser Error", "Int", "Float", "Id", "Char", "Str", "Eol", "Fun_Call", "Inplace_Fun_Call", "Arg_List", "Variable", "Equation", "Var_Decl", const char *token_types[] = { "Internal Parser Error", "Int", "Float", "Id", "Char", "Str", "Eol", "Fun_Call", "Inplace_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", "Break", "Map_Pair", "Value_Range", "Lambda", "Block", "Def", "While", "If", "For", "Inline_Array", "Inline_Map", "Return", "File", "Prefix", "Break", "Map_Pair", "Value_Range",
"Inline_Range", "Annotation", "Try", "Catch", "Finally", "Method", "Attr_Decl"}; "Inline_Range", "Annotation", "Try", "Catch", "Finally", "Method", "Attr_Decl", "Shift"};
return token_types[tokentype]; return token_types[tokentype];
} }

View File

@@ -1100,6 +1100,7 @@ namespace chaiscript
case (Token_Type::Comparison) : case (Token_Type::Comparison) :
case (Token_Type::Additive) : case (Token_Type::Additive) :
case (Token_Type::Multiplicative) : case (Token_Type::Multiplicative) :
case (Token_Type::Shift) :
return eval_comp_add_mul(ss, node); return eval_comp_add_mul(ss, node);
break; break;

View File

@@ -1499,11 +1499,11 @@ namespace chaiscript
int prev_stack_top = match_stack.size(); int prev_stack_top = match_stack.size();
if (Additive()) { if (Shift()) {
retval = true; retval = true;
if (Symbol(">=", true) || Symbol(">", true) || Symbol("<=", true) || Symbol("<", true) || Symbol("==", true) || Symbol("!=", true)) { if (Symbol(">=", true) || Symbol(">", true) || Symbol("<=", true) || Symbol("<", true) || Symbol("==", true) || Symbol("!=", true)) {
do { do {
if (!Additive()) { if (!Shift()) {
throw Eval_Error("Incomplete comparison expression", File_Position(line, col), filename); throw Eval_Error("Incomplete comparison expression", File_Position(line, col), filename);
} }
} while (retval && (Symbol(">=", true) || Symbol(">", true) || Symbol("<=", true) || Symbol("<", true) || Symbol("==", true) || Symbol("!=", true))); } while (retval && (Symbol(">=", true) || Symbol(">", true) || Symbol("<=", true) || Symbol("<", true) || Symbol("==", true) || Symbol("!=", true)));
@@ -1587,6 +1587,30 @@ namespace chaiscript
return retval; return retval;
} }
/**
* Top-level expression, parses a string of binary boolean operators from input
*/
bool Shift() {
bool retval = false;
int prev_stack_top = match_stack.size();
if (Additive()) {
retval = true;
if (Symbol("<<", true) || Symbol(">>", true)) {
do {
if (!Additive()) {
throw Eval_Error("Incomplete shift expression", File_Position(line, col), filename);
}
} while (retval && (Symbol("<<", true) || Symbol(">>", true)));
build_match(Token_Type::Shift, prev_stack_top);
}
}
return retval;
}
/** /**
* Top-level expression, parses a string of binary boolean operators from input * Top-level expression, parses a string of binary boolean operators from input
*/ */

1
unittests/shift.chai Normal file
View File

@@ -0,0 +1 @@
print(2 << 2)

1
unittests/shift.txt Normal file
View File

@@ -0,0 +1 @@
8