mirror of
https://github.com/BorysLevytskyi/BitwiseCmd.git
synced 2025-12-23 21:22:48 +01:00
partial support of plain numbers.
This commit is contained in:
@@ -6,12 +6,12 @@
|
||||
return Math.floor(Math.log(num) / Math.log(2)) + 1;
|
||||
};
|
||||
|
||||
calc.maxNumberOfBits = function () {
|
||||
calc.maxNumberOfBits = function (arr) {
|
||||
|
||||
var counts = [], num;
|
||||
for(var i=0;i<arguments.length; i++)
|
||||
for(var i=0;i<arr.length; i++)
|
||||
{
|
||||
num = arguments[i];
|
||||
num = arr[i];
|
||||
counts.push(this.numberOfBits(num));
|
||||
}
|
||||
|
||||
|
||||
@@ -1,23 +1,51 @@
|
||||
(function() {
|
||||
var twoOperandsRegex = /^(\d+)(<<|>>|\||\&|\^)(\d+)$/;
|
||||
var twoOperandsRegex = /^(\d+)(<<|>>|\||\&|\^)(\d+)$/;
|
||||
var numbersList = /^((\d*)+\s?)+$/;
|
||||
|
||||
app.service('expression', {
|
||||
parse: function(string) {
|
||||
var matches = twoOperandsRegex.exec(string);
|
||||
if(matches == null) {
|
||||
return null;
|
||||
var trimmed = string.replace(/^\s+|\s+$/, '');
|
||||
var matches = twoOperandsRegex.exec(trimmed);
|
||||
|
||||
if(matches != null) {
|
||||
return createCalculableExpression(matches);
|
||||
}
|
||||
|
||||
return {
|
||||
string:matches[0],
|
||||
operand1: parseInt(matches[1], 10),
|
||||
sign: matches[2],
|
||||
operand2: parseInt(matches[3], 10),
|
||||
result: function() {
|
||||
return eval(string);
|
||||
}
|
||||
matches = numbersList.exec(string);
|
||||
if(matches != null) {
|
||||
return createListOfNumbersExpression(matches)
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
function createCalculableExpression(matches) {
|
||||
|
||||
var o1 = parseInt(matches[1], 10);
|
||||
var o2 = parseInt(matches[3], 10);
|
||||
|
||||
return {
|
||||
string: matches.input,
|
||||
operand1: o1,
|
||||
sign: matches[2],
|
||||
operand2: o2,
|
||||
calculate: function() {
|
||||
return eval(this.string);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function createListOfNumbersExpression(matches) {
|
||||
var numbers = [], i=0;
|
||||
|
||||
for(;i<matches.length; i++) {
|
||||
numbers.push(parseInt(matches[i], 10));
|
||||
}
|
||||
|
||||
return {
|
||||
string:matches.input,
|
||||
operands: numbers
|
||||
}
|
||||
}
|
||||
|
||||
})(window.app);
|
||||
Reference in New Issue
Block a user