diff --git a/src/HacknetNode.js b/src/HacknetNode.js index 728229cce..e87cf0ae5 100644 --- a/src/HacknetNode.js +++ b/src/HacknetNode.js @@ -74,12 +74,12 @@ HacknetNode.prototype.calculateLevelUpgradeCost = function(levels=1) { HacknetNode.prototype.purchaseLevelUpgrade = function(levels=1) { var cost = this.calculateLevelUpgradeCost(levels); if (isNaN(cost)) {return false;} - if (cost > Player.money) {return false;} - Player.loseMoney(cost); if (this.level + levels > CONSTANTS.HacknetNodeMaxLevel) { var diff = Math.max(0, CONSTANTS.HacknetNodeMaxLevel - this.level); return this.purchaseLevelUpgrade(diff); } + if (cost > Player.money) {return false;} + Player.loseMoney(cost); this.level += levels; this.updateMoneyGainRate(); return true; diff --git a/src/NetscriptEnvironment.js b/src/NetscriptEnvironment.js index 7ce792582..632a9381e 100644 --- a/src/NetscriptEnvironment.js +++ b/src/NetscriptEnvironment.js @@ -28,7 +28,6 @@ Environment.prototype = { if (name in this.vars) { return this.vars[name]; } - console.log("here"); throw new Error("Undefined variable " + name); }, diff --git a/src/NetscriptEvaluator.js b/src/NetscriptEvaluator.js index 6b0304be3..3a5429585 100644 --- a/src/NetscriptEvaluator.js +++ b/src/NetscriptEvaluator.js @@ -28,11 +28,46 @@ function evaluate(exp, workerScript) { reject(e); }); return; + } else if (exp.value == "array") { + //A raw array. This will be called under something like this: + // x = Array[1, 2, 3]; + if (exp.array && exp.array instanceof Array) { + resolve(exp.array); + } else { + reject(makeRuntimeRejectMsg(workerScript, "Invalid array instantiation")); + } + return; } try { - resolve(env.get(exp.value)); + var res = env.get(exp.value); + console.log(res); + if (exp.index) { + //If theres an index field, then this variable is supposed to be an array + //and the user needs to be indexing it + if (res.constructor === Array || res instanceof Array) { + //Do array stuff here + var iPromise = evaluate(exp.index.value, workerScript); + iPromise.then(function(i) { + console.log("Index resolved with value: " + i); + if (i >= res.length || i < 0) { + return reject(makeRuntimeRejectMsg(workerScript, "Out of bounds: Invalid index in [] operator")); + } else { + //Evaluate here + return evaluate(res[i], workerScript); + } + }).then(function(res) { + resolve(res); + }).catch(function(e) { + reject(e); + }); + } else { + reject(makeRuntimeRejectMsg(workerScript, "Trying to access a non-array variable using the [] operator")); + } + } else { + resolve(res); + } } catch (e) { - throw new Error("|" + workerScript.serverIp + "|" + workerScript.name + "|" + e.toString()); + reject("|" + workerScript.serverIp + "|" + workerScript.name + "|" + e.toString()); } break; //Can currently only assign to "var"s @@ -236,6 +271,7 @@ function evaluate(exp, workerScript) { } var runScriptPromise = runScriptFromScript(scriptServer, scriptname, workerScript); + return runScriptPromise; }).then(function(res) { resolve(res); }).catch(function(e) { diff --git a/src/NetscriptParser.js b/src/NetscriptParser.js index 394ad4c59..354dfc54e 100644 --- a/src/NetscriptParser.js +++ b/src/NetscriptParser.js @@ -211,17 +211,27 @@ function Parser(input) { value: "hacknetnodes", index: index, op: op, - } + }; } unexpected(); } + function parse_array() { + //Declaring a new array with Array[1,2,3] + var array = delimited("[", "]", ",", parse_expression); + return {type: "var", + value: "array", + array: array + }; + } + function parse_arrayindex() { var index = delimited("[", "]", ";", parse_expression); var val = 0; if (index.length == 1 && (index[0].type == "num" || index[0].type == "var")) { val = index[0]; } else { + val = 0; console.log("WARNING: Extra indices passed in") } @@ -253,12 +263,21 @@ function Parser(input) { if (is_kw("if")) return parse_if(); if (is_kw("for")) return parse_for(); if (is_kw("while")) return parse_while(); - //if (is_kw("hacknetnodes")) return parse_hacknetnodes(); - //Note, let for loops be function calls (call node types) if (is_kw("true") || is_kw("false")) return parse_bool(); var tok = input.next(); if (tok.type == "var" && tok.value == "hacknetnodes") return parse_hacknetnodes(); + if (tok.type == "var" && tok.value == "Array") return parse_array(); + if (tok.type == "var" && is_punc("[")) { + //Returns a variable node except with an extra "index" field so + //we can identify it as an index + var index = parse_arrayindex(); + if (index.type != "index") { + unexpected(); + } + tok.index = index; + return tok; + } if (tok.type == "var" || tok.type == "num" || tok.type == "str") return tok; unexpected(); diff --git a/src/NetscriptWorker.js b/src/NetscriptWorker.js index d3fee5a5d..540c0cb3e 100644 --- a/src/NetscriptWorker.js +++ b/src/NetscriptWorker.js @@ -31,7 +31,7 @@ function runScriptsLoop() { if (workerScripts[i].running == false && workerScripts[i].env.stopFlag == false) { try { var ast = Parser(Tokenizer(InputStream(workerScripts[i].code))); - //console.log(ast); + console.log(ast); } catch (e) { console.log("Error parsing script: " + workerScripts[i].name); dialogBoxCreate("Syntax ERROR in " + workerScripts[i].name + ":
" + e);