Added arrays to Netscript. Fixed bug with Hacknet Nodes

This commit is contained in:
Daniel Xie 2017-06-10 17:44:33 -05:00
parent 6582026557
commit 60207b8e04
5 changed files with 63 additions and 9 deletions

@ -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;

@ -28,7 +28,6 @@ Environment.prototype = {
if (name in this.vars) {
return this.vars[name];
}
console.log("here");
throw new Error("Undefined variable " + name);
},

@ -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) {

@ -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();

@ -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 + ":<br>" + e);