Skip to content

Commit

Permalink
Binary left and right shift operators, expressions and implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ajlopez committed Dec 14, 2014
1 parent 5b10448 commit ee368e4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
14 changes: 14 additions & 0 deletions lib/expressions.js
Expand Up @@ -573,6 +573,18 @@ function BinaryXorExpression(left, right) {
};
}

function BinaryLeftShiftExpression(left, right) {
this.evaluate = function (context) {
return left.evaluate(context) << right.evaluate(context);
};
}

function BinaryRightShiftExpression(left, right) {
this.evaluate = function (context) {
return left.evaluate(context) >> right.evaluate(context);
};
}

function BinaryNotExpression(expr) {
this.evaluate = function (context) {
return ~expr.evaluate(context);
Expand Down Expand Up @@ -622,6 +634,8 @@ module.exports = {
BinaryOrExpression: BinaryOrExpression,
BinaryXorExpression: BinaryXorExpression,
BinaryNotExpression: BinaryNotExpression,
BinaryLeftShiftExpression: BinaryLeftShiftExpression,
BinaryRightShiftExpression: BinaryRightShiftExpression,
LogicalAndExpression: LogicalAndExpression,
LogicalOrExpression: LogicalOrExpression
};
Expand Down
8 changes: 6 additions & 2 deletions lib/parsers.js
Expand Up @@ -47,6 +47,8 @@ var ClassExpression = expressions.ClassExpression,
BinaryOrExpression = expressions.BinaryOrExpression,
BinaryXorExpression = expressions.BinaryXorExpression,
BinaryNotExpression = expressions.BinaryNotExpression,
BinaryLeftShiftExpression = expressions.BinaryLeftShiftExpression,
BinaryRightShiftExpression = expressions.BinaryRightShiftExpression,
LogicalAndExpression = expressions.LogicalAndExpression,
LogicalOrExpression = expressions.LogicalOrExpression;

Expand Down Expand Up @@ -83,10 +85,10 @@ var rules = [
get('!=').generate('NotEqual'),
get('<=>').generate('LessEqualGreater'),
get('<=').generate('LessEqual'),
get('<<').generate('LessLess'),
get('<<').generate('BinaryLeftShift'),
get('<').generate('Less'),
get('>=').generate('GreaterEqual'),
get('>>').generate('GreaterGreater'),
get('>>').generate('BinaryRightShift'),
get('>').generate('Greater'),
get('=').generate('Assign'),
get('&&').generate('LogicalAnd'),
Expand Down Expand Up @@ -198,6 +200,8 @@ var rules = [
get('Expression2', 'BinaryAnd', 'Expression1').generate('Expression2', function (values) { return new BinaryAndExpression(values[0], values[2]); }),
get('Expression2', 'BinaryOr', 'Expression1').generate('Expression2', function (values) { return new BinaryOrExpression(values[0], values[2]); }),
get('Expression2', 'BinaryXor', 'Expression1').generate('Expression2', function (values) { return new BinaryXorExpression(values[0], values[2]); }),
get('Expression2', 'BinaryLeftShift', 'Expression1').generate('Expression2', function (values) { return new BinaryLeftShiftExpression(values[0], values[2]); }),
get('Expression2', 'BinaryRightShift', 'Expression1').generate('Expression2', function (values) { return new BinaryRightShiftExpression(values[0], values[2]); }),

// Skip initial end of lines in Expression
//get(get('\n').oneOrMore(), 'Expression').generate('Expression', function (values) { return values[1] }),
Expand Down
14 changes: 14 additions & 0 deletions test/evaluate.js
Expand Up @@ -74,6 +74,20 @@ exports['Evaluate binary xor'] = function (test) {
test.strictEqual(result, 15 ^ 2);
};

exports['Evaluate binary left shift'] = function (test) {
var result = evaluate("15 << 2", "Expression", null, test);

test.ok(result);
test.strictEqual(result, 15 << 2);
};

exports['Evaluate binary right shift'] = function (test) {
var result = evaluate("15 >> 2", "Expression", null, test);

test.ok(result);
test.strictEqual(result, 15 >> 2);
};

exports['Evaluate binary not'] = function (test) {
var result = evaluate("~15", "Expression", null, test);

Expand Down

0 comments on commit ee368e4

Please sign in to comment.