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>" + "&nbsp;!=<br><br>" +
"<u><h1> Arrays </h1></u><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 " + "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>" + "<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 can NOT define you own functions in Netscript (yet), but there are several built in functions that " +
"you may use: <br><br> " + "you may use: <br><br> " +

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