Commit c9cbb9b8 authored by kosheo's avatar kosheo

Add variable that stores last evaluated expression. Closes #4.

parent 4be3c625
......@@ -79,6 +79,9 @@
// Evaluate expression in the global scope
var evaluatedExpression = node.compile().eval(this._scope);
// Evaluate and store command as a last evaluated command
this._assignLastEvaluatedCommand(node, this._scope);
if (this._isValidValue(evaluatedExpression))
{
if (evaluatedExpression instanceof clc.CallbackResult)
......@@ -241,4 +244,26 @@
{
return this._decimalNumberRegexp.test(n) ? clc.addThousandsSeparator(n, this._thousandsSeparator) : n;
};
/**
* Store value of the given node into built-in '$' variable that represents last command.
* @param {Object} node
* @param {Object} scope
*/
clc.Calculator.prototype._assignLastEvaluatedCommand = function (node, scope)
{
try
{
var expressionNode = node.clone(),
symbolNode = new this._mathJs.expression.node.SymbolNode('$'),
assignmentNode = new this._mathJs.expression.node.AssignmentNode(symbolNode, expressionNode);
assignmentNode.compile();
assignmentNode.eval(scope);
}
catch (e)
{
e;
}
};
}(window.clc = window.clc || {}));
......@@ -7,7 +7,7 @@
clc.SyntaxHighlighter = function ()
{
this._constants = [].concat(clc.MATH_JS_CONSTANTS, [ 'true', 'false' ]);
this._keywords = [].concat(clc.MATH_JS_FUNCTIONS, clc.CLCALC_FUNCTIONS, clc.ALIASES);
this._keywords = [].concat(clc.MATH_JS_FUNCTIONS, clc.CLCALC_FUNCTIONS, clc.ALIASES, '$');
this._re = {
'identifier': new RegExp('([0-9]*)([A-Za-z_$][A-Za-z0-9_$]*)', 'g'),
......
......@@ -79,6 +79,8 @@ block content
a(href = '#precision') Precision
li
a(href = '#aliases') Aliases
li
a(href = '#last-evaluated-expression') Last Evaluated Expression
li
a(href = '#about') About
div.col-md-9.help-content
......@@ -692,6 +694,18 @@ block content
td.code nPr
td.code permutations
// Last Evaluated Expression
h1(id = 'last-evaluated-expression') Last Evaluated Expression
p For convenience, Cl Calc stores value of the last evaluated expression in the built-in variable '$'. It can then be used in the same manner as a regular variable. However, it doesn't work with a function assignment.
+multisample('eyJjIjpbIjIrMyIsIiQrMiIsIiQiXSwidiI6IjEifQ==',
{ 'expression': '2+3', 'value': '5' },
{ 'expression': '$+2', 'value': '7' },
{ 'expression': '$', 'value': '7' })
p Be extra careful with '$' variable, since its value updated after every expression evaluation. It is nothing more then just a shortcut to quickly access the value of the previous expression.
// About
h1(id = 'about') About
......
describe ('Last evaluated expression', function ()
{
beforeEach(function() { jasmine.addCustomEqualityTester(texIgnoringResultEqualityTester); });
it ('Should store last evaluated expressions value for all supported types', function ()
{
var calc = createCalculatorInstance();
expect(calc.evaluate('0').result).toEqual(createResult('0'));
expect(calc.evaluate('$').result).toEqual(createResult('0'));
expect(calc.evaluate('$').result).toEqual(createResult('0'));
expect(calc.evaluate('2+3*5').result).toEqual(createResult('17'));
expect(calc.evaluate('$').result).toEqual(createResult('17'));
expect(calc.evaluate('pi').result).toEqual(createResult('3.141592653589793238462643383279502884197169399375105820974944592'));
expect(calc.evaluate('$').result).toEqual(createResult('3.141592653589793238462643383279502884197169399375105820974944592'));
expect(calc.evaluate('fraction("2/3")').result).toEqual(createResult('2/3'));
expect(calc.evaluate('$').result).toEqual(createResult('2/3'));
expect(calc.evaluate('3 > 2 and 3 < 5').result).toEqual(createResult('true'));
expect(calc.evaluate('$').result).toEqual(createResult('true'));
expect(calc.evaluate('"test"').result).toEqual(createResult('"test"'));
expect(calc.evaluate('$').result).toEqual(createResult('"test"'));
expect(calc.evaluate('concat("Hello, ", "World")').result).toEqual(createResult('"Hello, World"'));
expect(calc.evaluate('$').result).toEqual(createResult('"Hello, World"'));
expect(calc.evaluate('[1, 2, 3] + 10').result).toEqual(createResult('[11, 12, 13]'));
expect(calc.evaluate('$').result).toEqual(createResult('[11, 12, 13]'));
expect(calc.evaluate('100 m to km').result).toEqual(createResult('0.1 km'));
expect(calc.evaluate('$').result).toEqual(createResult('0.1 km'));
});
it ('Should not update $ variable untill expression evaluation complete', function ()
{
var calc = createCalculatorInstance();
expect(calc.evaluate('2+3').result).toEqual(createResult('5'));
expect(calc.evaluate('$').result).toEqual(createResult('5'));
expect(calc.evaluate('$ + 2').result).toEqual(createResult('7'));
expect(calc.evaluate('$').result).toEqual(createResult('7'));
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment