From dbca2e03b49637fa685020e22e14d81090f84513 Mon Sep 17 00:00:00 2001 From: Borys Levytskyi Date: Sat, 11 Apr 2015 19:41:27 +0300 Subject: [PATCH] Implemented support of hex mode for expression parser --- src/js/app/bitwise/expression.js | 51 +++++++++++++++++++++++--------- tests/domain/expressionSpec.js | 23 ++++++++++++-- 2 files changed, 57 insertions(+), 17 deletions(-) diff --git a/src/js/app/bitwise/expression.js b/src/js/app/bitwise/expression.js index 7f53f6a..947278a 100644 --- a/src/js/app/bitwise/expression.js +++ b/src/js/app/bitwise/expression.js @@ -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; + } + } }); \ No newline at end of file diff --git a/tests/domain/expressionSpec.js b/tests/domain/expressionSpec.js index 6648ab4..f119430 100644 --- a/tests/domain/expressionSpec.js +++ b/tests/domain/expressionSpec.js @@ -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])); } }); }); \ No newline at end of file