2016-11-17 23:25:40 +01:00
|
|
|
/* Environment
|
|
|
|
* NetScript program environment
|
|
|
|
*/
|
|
|
|
function Environment(parent) {
|
|
|
|
this.vars = Object.create(parent ? parent.vars : null);
|
|
|
|
this.parent = parent;
|
2016-12-06 17:59:20 +01:00
|
|
|
this.stopFlag = false;
|
2016-11-17 23:25:40 +01:00
|
|
|
}
|
|
|
|
Environment.prototype = {
|
|
|
|
//Create a "subscope", which is a new new "sub-environment"
|
|
|
|
//The subscope is linked to this through its parent variable
|
|
|
|
extend: function() {
|
|
|
|
return new Environment(this);
|
|
|
|
},
|
|
|
|
|
|
|
|
//Finds the scope where the variable with the given name is defined
|
|
|
|
lookup: function(name) {
|
|
|
|
var scope = this;
|
|
|
|
while (scope) {
|
|
|
|
if (Object.prototype.hasOwnProperty.call(scope.vars, name))
|
|
|
|
return scope;
|
|
|
|
scope = scope.parent;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
//Get the current value of a variable
|
|
|
|
get: function(name) {
|
2017-05-30 00:37:38 +02:00
|
|
|
if (name in this.vars) {
|
2016-11-17 23:25:40 +01:00
|
|
|
return this.vars[name];
|
2017-05-30 00:37:38 +02:00
|
|
|
}
|
|
|
|
console.log("here");
|
2016-11-17 23:25:40 +01:00
|
|
|
throw new Error("Undefined variable " + name);
|
|
|
|
},
|
|
|
|
|
|
|
|
//Sets the value of a variable in any scope
|
|
|
|
set: function(name, value) {
|
|
|
|
var scope = this.lookup(name);
|
|
|
|
// let's not allow defining globals from a nested environment
|
|
|
|
//
|
|
|
|
// If scope is null (aka existing variable with name could not be found)
|
|
|
|
// and this is NOT the global scope, throw error
|
2017-05-30 00:37:38 +02:00
|
|
|
if (!scope && this.parent) {
|
|
|
|
console.log("Here");
|
2016-11-17 23:25:40 +01:00
|
|
|
throw new Error("Undefined variable " + name);
|
2017-05-30 00:37:38 +02:00
|
|
|
}
|
2016-11-17 23:25:40 +01:00
|
|
|
return (scope || this).vars[name] = value;
|
|
|
|
},
|
|
|
|
|
|
|
|
//Creates (or overwrites) a variable in the current scope
|
|
|
|
def: function(name, value) {
|
|
|
|
return this.vars[name] = value;
|
2016-12-06 17:59:20 +01:00
|
|
|
}
|
2016-11-17 23:25:40 +01:00
|
|
|
};
|