diff --git a/src/js/app/bitwise/calc.js b/src/js/app/bitwise/calc.js index 385ccbc..85f7f47 100644 --- a/src/js/app/bitwise/calc.js +++ b/src/js/app/bitwise/calc.js @@ -24,7 +24,7 @@ app.set('calc', function() { }, calcExpression: function (expr) { - return eval(expr.string); + return eval(expr.expressionString); } } }); \ No newline at end of file diff --git a/src/js/app/bitwise/expression.js b/src/js/app/bitwise/expression.js index ffc70a5..493ca07 100644 --- a/src/js/app/bitwise/expression.js +++ b/src/js/app/bitwise/expression.js @@ -5,7 +5,7 @@ app.set('expression', function() { var listRegex = /^(-?(?:\d+|0x[\d,a-f]+)\s?)+$/; var notRex = /^(~)(-?(?:\d+|0x[\d,a-f]+))$/; - return { + var expression = { canParse: function(string) { return exprRegex.test(string) || listRegex.test(string) || notRex.test(string) }, @@ -39,40 +39,36 @@ app.set('expression', function() { } return new Operand(str); - } - + }, + TwoOperandExpression: TwoOperandExpression, + SingleOperandExpression: SingleOperandExpression, + ListOfNumbersExpression: ListOfNumbersExpression }; function createTwoOperandExpr(matches) { - var m = new app.models.BitwiseOperation(); - m.operand1 = new Operand(matches[1]); - m.operand2 = new Operand(matches[3]); - m.sign = matches[2]; - m.string = matches.input; - //m.result = eval(matches.input); + var operand1 = new Operand(matches[1]), + operand2 = new Operand(matches[3]), + sign = matches[2], + expressionString = matches.input; - return m; + return new TwoOperandExpression(expressionString, operand1, operand2, sign); } function createSingleOperandExpr(matches) { - var m = new app.models.BitwiseOperation(); - m.operand1 = new Operand(matches[2]); - m.sign = matches[1]; - m.string = matches.input; - return m; + var operand = new Operand(matches[2]) + return new SingleOperandExpression(matches.input, operand, matches[1]); } function createListOfNumbersExpression(input) { - var operands = []; + var numbers = []; input.split(' ').forEach(function(n){ if(n.trim().length > 0) { - operands.push(new Operand(n.trim())); + numbers.push(new Operand(n.trim())); } - }); - return new app.models.BitwiseNumbers(operands); + return new ListOfNumbersExpression(input, numbers); } function getBase(kind) { @@ -97,4 +93,35 @@ app.set('expression', function() { this.kind = this.input.indexOf('0x') > -1 ? 'hex' : 'dec'; this.other = this.kind == 'dec' ? this.hex : this.dec; } + + function SingleOperandExpression(expressionString, operand, sign) { + this.expressionString = expressionString; + this.operand1 = operand; + this.sign = sign; + } + + function TwoOperandExpression(expressionString, operand1, operand2, sign) { + this.expressionString = expressionString; + this.operand1 = operand1; + this.operand2 = operand2; + this.sign = sign; + } + + function ListOfNumbersExpression(expressionString, numbers) { + this.expressionString = expressionString; + this.numbers = numbers; + } + + function Expression() { + } + + Expression.prototype.toString = function() { + return this.expressionString ? "Expression: " + this.expressionString : this.toString(); + }; + + //TwoOperandExpression.prototype = Expression.prototype; + //SingleOperandExpression.prototype = Expression.prototype; + //ListOfNumbersExpression.prototype = Expression.prototype; + + return expression; }); \ No newline at end of file diff --git a/src/js/app/cmd/commands.js b/src/js/app/cmd/commands.js index ac83341..6ae4839 100644 --- a/src/js/app/cmd/commands.js +++ b/src/js/app/cmd/commands.js @@ -4,6 +4,7 @@ app.run(function() { var cmd = app.get('cmd'); var cmdConfig = app.get('cmdConfig'); var rootView = app.get('rootView'); + var expression = app.get('expression'); cmd.commands({ 'help': function() { @@ -36,6 +37,7 @@ app.run(function() { }, '-debug': function() { app.debugMode = true; + console.log('debug is on'); }, '-notrack': function () {} }); @@ -44,7 +46,19 @@ app.run(function() { cmd.command({ canHandle: function(input) { return app.get('expression').canParse(input); }, handle: function(input) { - return app.get('expression').parse(input); + var expr = expression.parse(input); + return this.locateModel(expr); + }, + locateModel: function (expr) { + if(expr instanceof expression.SingleOperandExpression || expr instanceof expression.TwoOperandExpression){ + return new app.models.BitwiseOperation(expr); + } + + if(expr instanceof expression.ListOfNumbersExpression) { + return new app.models.BitwiseNumbers(expr); + } + + return new app.models.ErrorResult('Cannot create model for expression: ' + expr.toString()); } }); diff --git a/src/js/app/modelViews.js b/src/js/app/modelViews.js index 7197ea6..acd7155 100644 --- a/src/js/app/modelViews.js +++ b/src/js/app/modelViews.js @@ -29,12 +29,12 @@ app.compose(function () { } return { - renderView: function(expr) { + renderView: function(model) { // TODO: move all this to expression - var result = expression.createOperand(calc.calcExpression(expr), getResultMode([expr.operand1, expr.operand2])); - var maxLen = getBinaryLength([expr.operand1.value, expr.operand2 != null ? expr.operand2.value : 0, result.value]); + var result = expression.createOperand(calc.calcExpression(model.expression), getResultMode([model.operand1, model.operand2])); + var maxLen = getBinaryLength([model.operand1.value, model.operand2 != null ? model.operand2.value : 0, result.value]); - var model = Object.create(expr); + var model = Object.create(model); model.bitsSize = maxLen; model.result = result; diff --git a/src/js/app/models.js b/src/js/app/models.js index 1627aab..0ebc5c9 100644 --- a/src/js/app/models.js +++ b/src/js/app/models.js @@ -1,18 +1,21 @@ (function(app) { "use strict"; - function BitwiseOperation () { + function BitwiseOperation (expression) { + this.expression = expression; + this.operand1 = expression.operand1; + this.operand2 = expression.operand2; + this.sign = expression.sign; + this.string = expression.expressionString; } - BitwiseOperation.prototype.calculate = function () { - return eval(this.string); - }; + function BitwiseNumbers(expression) { + this.expression = expression; + this.operands = expression.numbers; - function BitwiseNumbers(operands) { - this.operands = operands; var numbers = this.numbers = []; - operands.forEach(function (o) { + expression.numbers.forEach(function (o) { numbers.push(o.value); }); } diff --git a/src/js/core/di.js b/src/js/core/di.js index ed0cb9a..1c064f7 100644 --- a/src/js/core/di.js +++ b/src/js/core/di.js @@ -35,7 +35,7 @@ function resolveInternal(name) { if(contains(this.resolutionStack, name)) { - throw new Error("Failed to resolve service: " + name + ". Circular reference: " + this.resolutionStack.join(' < ')); + throw new Error("Failed to resolve dependency: " + name + ". Circular reference: " + this.resolutionStack.join(' < ')); } this.resolutionStack.unshift(name); diff --git a/tests/unit/domain/expressionSpec.js b/tests/unit/domain/expressionSpec.js index 7dfb5d4..9b89ddd 100644 --- a/tests/unit/domain/expressionSpec.js +++ b/tests/unit/domain/expressionSpec.js @@ -41,7 +41,8 @@ describe("expression parse", function() { expect(actual.operand2).not.toBeDefined(); } - expect(actual.string).toBe(expected.string); + expect(actual.expressionString).toBe(expected.string); + console.log(actual.toString()); } }); @@ -59,7 +60,8 @@ describe("expression parse", function() { var expected = listCases[input]; for(i =0; i