diff --git a/src/Constants.js b/src/Constants.js index 27571077a..f9dba12e7 100644 --- a/src/Constants.js +++ b/src/Constants.js @@ -319,7 +319,12 @@ CONSTANTS = { " !=

" + "

Arrays


" + "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:

" + + "var thisIsAnArray = Array[1, 2, 3, 'bitburner!', false];

" + + "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:

" + + "print(thisIsAnArray[0]);
" + + "thisIsAnArray[1] = 5" + "

Functions


" + "You can NOT define you own functions in Netscript (yet), but there are several built in functions that " + "you may use:

" + diff --git a/src/NetscriptFunctions.js b/src/NetscriptFunctions.js index 83d2c8384..a5fcaf179 100644 --- a/src/NetscriptFunctions.js +++ b/src/NetscriptFunctions.js @@ -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) {