mirror of
https://github.com/BorysLevytskyi/BitwiseCmd.git
synced 2025-12-16 09:52:10 +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;
|
return Math.floor(Math.log(num) / Math.log(2)) + 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
calc.maxNumberOfBits = function () {
|
calc.maxNumberOfBits = function (arr) {
|
||||||
|
|
||||||
var counts = [], num;
|
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));
|
counts.push(this.numberOfBits(num));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,23 +1,51 @@
|
|||||||
(function() {
|
(function() {
|
||||||
var twoOperandsRegex = /^(\d+)(<<|>>|\||\&|\^)(\d+)$/;
|
var twoOperandsRegex = /^(\d+)(<<|>>|\||\&|\^)(\d+)$/;
|
||||||
|
var numbersList = /^((\d*)+\s?)+$/;
|
||||||
|
|
||||||
app.service('expression', {
|
app.service('expression', {
|
||||||
parse: function(string) {
|
parse: function(string) {
|
||||||
var matches = twoOperandsRegex.exec(string);
|
var trimmed = string.replace(/^\s+|\s+$/, '');
|
||||||
if(matches == null) {
|
var matches = twoOperandsRegex.exec(trimmed);
|
||||||
return null;
|
|
||||||
|
if(matches != null) {
|
||||||
|
return createCalculableExpression(matches);
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
matches = numbersList.exec(string);
|
||||||
string:matches[0],
|
if(matches != null) {
|
||||||
operand1: parseInt(matches[1], 10),
|
return createListOfNumbersExpression(matches)
|
||||||
sign: matches[2],
|
|
||||||
operand2: parseInt(matches[3], 10),
|
|
||||||
result: function() {
|
|
||||||
return eval(string);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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);
|
})(window.app);
|
||||||
32
app/views.js
32
app/views.js
@@ -10,9 +10,21 @@
|
|||||||
|
|
||||||
ExpressionView.prototype.render = function () {
|
ExpressionView.prototype.render = function () {
|
||||||
var expr = this.expression,
|
var expr = this.expression,
|
||||||
hb = app.service('html').builder(),
|
hb = app.service('html').builder();
|
||||||
result = expr.result(),
|
|
||||||
maxLen = calc.maxNumberOfBits(expr.operand1, expr.operand2, result);
|
console.log('Rendering expression: ', expr)
|
||||||
|
|
||||||
|
if(typeof expr.calculate == "function") {
|
||||||
|
return renderCalculableExpression(expr, hb);
|
||||||
|
} else {
|
||||||
|
return renderListOfNumbers(expr, hb);
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
function renderCalculableExpression(expr, hb) {
|
||||||
|
var result = expr.calculate(),
|
||||||
|
maxLen = calc.maxNumberOfBits([expr.operand1, expr.operand2, result]);
|
||||||
|
|
||||||
hb.element('table', { class: "expression", cellspacing: "0"}, function () {
|
hb.element('table', { class: "expression", cellspacing: "0"}, function () {
|
||||||
buildRow(hb, expr.operand1, formatter.toBinaryString(expr.operand1, maxLen));
|
buildRow(hb, expr.operand1, formatter.toBinaryString(expr.operand1, maxLen));
|
||||||
@@ -21,7 +33,19 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
return hb.toHtmlElement();
|
return hb.toHtmlElement();
|
||||||
};
|
}
|
||||||
|
|
||||||
|
function renderListOfNumbers(expr, hb) {
|
||||||
|
var maxLen = calc.maxNumberOfBits(expr.operands);
|
||||||
|
|
||||||
|
hb.element('table', { class: "expression", cellspacing: "0"}, function () {
|
||||||
|
expr.operands.forEach(function(o){
|
||||||
|
buildRow(hb, o, formatter.toBinaryString(o, maxLen));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return hb.toHtmlElement();
|
||||||
|
}
|
||||||
|
|
||||||
function buildRow(hb, label, binaryStr, attrs) {
|
function buildRow(hb, label, binaryStr, attrs) {
|
||||||
hb.element('tr', attrs, function() {
|
hb.element('tr', attrs, function() {
|
||||||
|
|||||||
@@ -96,8 +96,11 @@
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
element.addEventListener('DOMNodeRemoved', function () {
|
element.addEventListener('DOMNodeRemoved', function (evt) {
|
||||||
|
if(element === evt.target) {
|
||||||
ctrl.detachView();
|
ctrl.detachView();
|
||||||
|
}
|
||||||
|
|
||||||
console.log(ctrlName + ' Controller: view detached');
|
console.log(ctrlName + ' Controller: view detached');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user