Fixed an issue in the Parser not resolving variables later on when undefined in first instance.

This commit is contained in:
josdejong 2013-02-25 21:09:05 +01:00
parent 6faacfb120
commit c1fa8d8231
4 changed files with 23 additions and 17 deletions

17
math.js
View File

@ -4250,13 +4250,6 @@ FunctionAssignment.prototype.toString = function() {
return this.def.toString();
};
/**
* @License Apache 2.0 License
*
* @Author Jos de Jong
* @Date 2012-07-10
*/
/**
* Scope
* A scope stores functions.
@ -4278,6 +4271,8 @@ function Scope(parentScope) {
math.parser.node.Scope = Scope;
// TODO: rethink the whole scoping solution again. Try to simplify
/**
* Create a nested scope
* The variables in a nested scope are not accessible from the parent scope
@ -4339,9 +4334,15 @@ Scope.prototype.createSymbol = function (name) {
*/
Scope.prototype.newSymbol = function (name, value) {
// create a new symbol
var scope = this;
var symbol = function () {
if (!symbol.value) {
throw new Error('Undefined symbol ' + name);
// try to resolve again
symbol.value = scope.findDef(name);
if (!symbol.value) {
throw new Error('Undefined symbol ' + name);
}
}
if (typeof symbol.value == 'function') {
return symbol.value.apply(null, arguments);

2
math.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -1,10 +1,3 @@
/**
* @License Apache 2.0 License
*
* @Author Jos de Jong
* @Date 2012-07-10
*/
/**
* Scope
* A scope stores functions.
@ -26,6 +19,8 @@ function Scope(parentScope) {
math.parser.node.Scope = Scope;
// TODO: rethink the whole scoping solution again. Try to simplify
/**
* Create a nested scope
* The variables in a nested scope are not accessible from the parent scope
@ -87,9 +82,15 @@ Scope.prototype.createSymbol = function (name) {
*/
Scope.prototype.newSymbol = function (name, value) {
// create a new symbol
var scope = this;
var symbol = function () {
if (!symbol.value) {
throw new Error('Undefined symbol ' + name);
// try to resolve again
symbol.value = scope.findDef(name);
if (!symbol.value) {
throw new Error('Undefined symbol ' + name);
}
}
if (typeof symbol.value == 'function') {
return symbol.value.apply(null, arguments);

View File

@ -15,3 +15,7 @@ require('./function/probability.js');
require('./function/statistics.js');
require('./function/trigonometry.js');
require('./function/units.js');
// TODO: test Parser
// TODO: test Scope
// TODO: test Node, Assignment, Block, Constant, FunctionAssignment, Symbol