Fixed issue with assigning expressions to array elements

This commit is contained in:
Daniel Xie 2017-06-11 14:05:28 -05:00
parent f983d5521b
commit 59817db626
2 changed files with 29 additions and 9 deletions

@ -319,7 +319,12 @@ CONSTANTS = {
"&nbsp;!=<br><br>" +
"<u><h1> Arrays </h1></u><br>" +
"Arrays are special container objects. Arrays can holy many values under a single name. Each value in the array " +
"can be accessed using an index number. To declare and access" +
"can be accessed using an index number. The following example shows how to declare an array: <br><br>" +
"var thisIsAnArray = Array[1, 2, 3, 'bitburner!', false];<br><br>" +
"Note that the values in an array can be different types. To access this array we just declared, we can use the index " +
"operator on the array's name: <br><br>" +
"print(thisIsAnArray[0]); <br>" +
"thisIsAnArray[1] = 5" +
"<u><h1> Functions </h1></u><br>" +
"You can NOT define you own functions in Netscript (yet), but there are several built in functions that " +
"you may use: <br><br> " +

@ -14,22 +14,37 @@ function netscriptAssign(exp, workerScript) {
try {
var res = env.get(exp.left.value);
if (res.constructor === Array || res instanceof Array) {
var i = 0;
var iPromise = evaluate(exp.left.index.value, workerScript);
iPromise.then(function(i) {
if (i >= res.length || i < 0) {
iPromise.then(function(idx) {
if (idx >= res.length || idx < 0) {
return reject(makeRuntimeRejectMsg(workerScript, "Out of bounds: Invalid index in [] operator"));
} else {
res[i].type = exp.right.type;
res[i].value = exp.right.value;
return resolve(false);
//TODO evaluate exp.right here....and then determine its type and
//set the type and value below accordingly
i = idx;
return evaluate(exp.right, workerScript);
}
}).then(function(res) {
return resolve(res);
}).then(function(right) {
console.log("evaluate right with result: " + right);
if (right === true || right === false) {
res[i].type = "bool";
res[i].value = right;
} else if (!isNaN(right) || typeof right == 'number') {
res[i].type = "num";
res[i].value = right;
} else { //String
res[i].type = "str";
res[i].value = right.toString();
}
console.log(res);
return resolve(true);
}).then(function(finalRes) {
resolve(finalRes);
}).catch(function(e) {
return reject(e);
});
} else {
console.log("here");
return reject(makeRuntimeRejectMsg(workerScript, "Trying to access a non-array variable using the [] operator"));
}
} catch(e) {