mirror of
https://github.com/BorysLevytskyi/BitwiseCmd.git
synced 2026-02-01 00:24:31 +01:00
Implemented support of hex mode for expression parser
This commit is contained in:
@@ -1,32 +1,47 @@
|
||||
app.set('expression', function() {
|
||||
"use strict";
|
||||
var decNumber = "\d+";
|
||||
var hexNumber = "(?:\d|a|b|c|d|e|f)";
|
||||
|
||||
var twoOperandsRegex = /^(\d+)\s*(<<|>>|\||\&|\^)\s*(\d+)$/;
|
||||
var numbersList = /^((\d*)+\s?)+$/;
|
||||
var modes = {
|
||||
'dec': {
|
||||
expr: /^(\d+)\s*(<<|>>|\||\&|\^)\s*(\d+)$/,
|
||||
list: /^((\d*)+\s?)+$/
|
||||
},
|
||||
'hex': {
|
||||
expr: /^([\d,a-f]+)\s*(<<|>>|\||\&|\^)\s*([\d,a-f]+)$/,
|
||||
list: /^(([\d,a-f]*)+\s?)+$/
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
canParse: function(string) {
|
||||
return twoOperandsRegex.test(string) || numbersList.test(string);
|
||||
canParse: function(string, mode) {
|
||||
var regex = modes[mode || 'dec'];
|
||||
return regex.expr.test(string) || regex.list.test(string);
|
||||
},
|
||||
parse: function(string) {
|
||||
parse: function(string, mode) {
|
||||
mode = (mode || 'dec');
|
||||
|
||||
var trimmed = string.replace(/^\s+|\s+$/, '');
|
||||
var matches = twoOperandsRegex.exec(trimmed);
|
||||
var regex = modes[mode];
|
||||
var base = getBase(mode);
|
||||
var matches = regex.expr.exec(trimmed);
|
||||
|
||||
if(matches != null) {
|
||||
return createCalculableExpression(matches);
|
||||
return createCalculableExpression(matches, base);
|
||||
}
|
||||
|
||||
matches = numbersList.exec(string);
|
||||
matches = regex.list.exec(string);
|
||||
if(matches != null) {
|
||||
return createListOfNumbersExpression(string)
|
||||
return createListOfNumbersExpression(string, base)
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
function createCalculableExpression(matches) {
|
||||
function createCalculableExpression(matches, base) {
|
||||
|
||||
var o1 = parseInt(matches[1], 10);
|
||||
var o2 = parseInt(matches[3], 10);
|
||||
var o1 = parseInt(matches[1], base);
|
||||
var o2 = parseInt(matches[3], base);
|
||||
|
||||
var m = new app.models.BitwiseOperation();
|
||||
m.operand1 = o1;
|
||||
@@ -38,15 +53,23 @@ app.set('expression', function() {
|
||||
return m;
|
||||
}
|
||||
|
||||
function createListOfNumbersExpression(input) {
|
||||
function createListOfNumbersExpression(input, base) {
|
||||
var numbers = [];
|
||||
input.split(' ').forEach(function(n){
|
||||
if(n.trim().length > 0) {
|
||||
numbers.push(parseInt(n));
|
||||
numbers.push(parseInt(n, base));
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
return new app.models.BitwiseNumbers(numbers);
|
||||
}
|
||||
|
||||
function getBase(mode) {
|
||||
switch (mode){
|
||||
case 'bin': return 2;
|
||||
case 'hex': return 16;
|
||||
case 'dec': return 10;
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -2,7 +2,7 @@ describe("expression parse", function() {
|
||||
var app = window.app;
|
||||
var expression = app.get('expression');
|
||||
|
||||
var expressionsCases = {
|
||||
var decCases = {
|
||||
"1 2 3": { numbers: [1,2,3] },
|
||||
"1": { numbers: [1] },
|
||||
"2>>1": { operand1: 2, operand2:1, "sign":">>", string:"2>>1" },
|
||||
@@ -10,12 +10,29 @@ describe("expression parse", function() {
|
||||
"23^1": { operand1: 23, operand2:1, "sign":"^", string: "23^1" }
|
||||
};
|
||||
|
||||
var hexCases = {
|
||||
"1 10 f" : { numbers: [1, 16, 15] },
|
||||
"f>>a": { operand1: 15, operand2:10, "sign":">>", string:"f>>a" },
|
||||
"10&11": { operand1: 16, operand2:17, "sign":"&", string:"10&11" },
|
||||
"10^11": { operand1: 16, operand2:17, "sign":"^", string:"10^11" }
|
||||
};
|
||||
|
||||
it("should parse decimal expressions", function() {
|
||||
var input, expr;
|
||||
for(input in expressionsCases) {
|
||||
for(input in decCases) {
|
||||
expect(expression.canParse(input)).toBe(true);
|
||||
expr = expression.parse(input);
|
||||
expect(JSON.stringify(expr)).toEqual(JSON.stringify(expressionsCases[input]));
|
||||
expect(JSON.stringify(expr)).toEqual(JSON.stringify(decCases[input]));
|
||||
}
|
||||
});
|
||||
|
||||
it("should parse hexadecimal expressions", function() {
|
||||
var input, expr;
|
||||
for(input in hexCases) {
|
||||
console.log('input: ' + input)
|
||||
expect(expression.canParse(input, 'hex')).toBe(true);
|
||||
expr = expression.parse(input, 'hex');
|
||||
expect(JSON.stringify(expr)).toEqual(JSON.stringify(hexCases[input]));
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user