Added var declaration in Netscript 1.0. Loops and conditionals no longer incur RAM cost, but base RAM cost increased from 1.4 to 1.6 GB

This commit is contained in:
danielyxie 2018-06-22 14:39:15 -05:00
parent 24cd1bb498
commit aa17b02da7
5 changed files with 9046 additions and 3200 deletions

12168
dist/engine.bundle.js vendored

File diff suppressed because one or more lines are too long

@ -42,11 +42,11 @@ let CONSTANTS = {
/* Netscript Constants */
//RAM Costs for different commands
ScriptBaseRamCost: 1.4,
ScriptBaseRamCost: 1.6,
ScriptDomRamCost: 100,
ScriptWhileRamCost: 0.2,
ScriptForRamCost: 0.2,
ScriptIfRamCost: 0.15,
ScriptWhileRamCost: 0,
ScriptForRamCost: 0,
ScriptIfRamCost: 0,
ScriptHackRamCost: 0.1,
ScriptGrowRamCost: 0.15,
ScriptWeakenRamCost: 0.15,
@ -490,17 +490,15 @@ let CONSTANTS = {
LatestUpdate:
"v0.38.1<br>" +
"* Bug Fix: Using 'Object.prototype' functions like toLocaleString() or toString() should no longer cause errors in NetscriptJS<br>" +
"* Implemented by Github user hydroflame:<br>" +
"*** Accessing the 'window' and 'document' objects in Netscript JS now requires a large amount of RAM (100 GB)<br>" +
"*** Added game option to suppress travel confirmation<br>" +
"*** Text on buttons can no longer be highlighted<br>" +
"*** Bug Fix: Fixed an issue that caused NaN values when exporting Real Estate in Corporations<br>" +
"*** Bug Fix: Competition and Demand displays in Corporation are now correct (were reversed before)<br>" +
"*** Added ps() Netscript function<br>" +
"*** Bug Fix: grow() should no longer return/log a negative value when it runs on a server that's already at max money<br>" +
"*** Bug Fix: serverExists() Netscript function should now properly return false for non-existent hostname/ips<br>" +
"*** Bug Fix: Sever's security level should now properly increase when its money is grown to max value"
"* Added 'var' declarations in Netscript 1.0 (only works with 'var', not 'let' or 'const')<br>" +
"* Script base RAM cost is now 1.6 GB (increased from 1.4 GB)<br>" +
"* While/for loops and if statements no longer cost RAM in scripts<br>" +
"* Made short-circuit evaluation logic more consistent in Netscript 1.0 (see https://github.com/danielyxie/bitburner/issues/308)<br>" +
"* Changelog button in the Options menu now links to the new Changelog URL (by Github user thePalindrome)<br>" +
"* Skill level calculation is now 'smoother' (by Github user hydroflame)<br>" +
"* Added a button to 'beautify' scripts in the text editor (by Github user hydroflame)<br>" +
"* Added favicon (by Github user kopelli)"
}

@ -224,6 +224,9 @@ function evaluate(exp, workerScript) {
case "AssignmentExpression":
return evalAssignment(exp, workerScript);
break;
case "VariableDeclaration":
return evalVariableDeclaration(exp, workerScript);
break;
case "UpdateExpression":
if (exp.argument.type==="Identifier"){
if (exp.argument.name in env.vars){
@ -333,11 +336,11 @@ function evaluate(exp, workerScript) {
function evalBinary(exp, workerScript){
return evaluate(exp.left, workerScript).then(function(expLeft) {
//Short circuiting
if (expLeft == true && exp.operator === "||") {
return Promise.resolve(true);
if (expLeft && exp.operator === "||") {
return Promise.resolve(expLeft);
}
if (expLeft == false && exp.operator === "&&") {
return Promise.resolve(false);
if (!expLeft && exp.operator === "&&") {
return Promise.resolve(expLeft);
}
return evaluate(exp.right, workerScript).then(function(expRight) {
switch (exp.operator){
@ -511,6 +514,41 @@ function evalAssignment(exp, workerScript) {
});
}
function evalVariableDeclaration(exp, workerScript) {
var env = workerScript.env;
if (env.stopFlag) {return Promise.reject(workerScript);}
if (!(exp.declarations instanceof Array)) {
return Promise.reject(makeRuntimeRejectMsg(workerScript, "Variable declarations parsed incorrectly. This may be a syntax error"));
}
if (exp.kind !== "var") {
return Promise.reject(makeRuntimeRejectMsg(workerScript, "Only 'var' declarations are currently allowed (let, const, etc. are not allowed)"));
}
return Promise.all(exp.declarations.map((decl)=>{
evalVariableDeclarator(decl, workerScript);
})).then(function(res) {
return Promise.resolve(res);
});
}
//A Variable Declaration contains an array of Variable Declarators
function evalVariableDeclarator(exp, workerScript) {
var env = workerScript.env;
if (exp.type !== "VariableDeclarator") {
return Promise.reject(makeRuntimeRejectMsg(workerScript, "Invalid AST Node passed into evalVariableDeclarator: " + exp.type));
}
if (exp.init == null) {
env.set(exp.id.name, null);
return Promise.resolve(null);
} else {
return evaluate(exp.init, workerScript).then(function(initValue) {
env.set(exp.id.name, initValue);
});
}
}
function evaluateIf(exp, workerScript, i) {
var env = workerScript.env;
return evaluate(exp.test, workerScript).then(function(condRes) {

@ -194,7 +194,7 @@ function runScriptsLoop() {
} else {
try {
var ast = parse(workerScripts[i].code, {sourceType:"module"});
//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);

@ -60,7 +60,7 @@ function scriptEditorInit() {
}
var beautifyButton = createElement("a", {
class:"a-link-button", display:"inline-block",
innerText:"beautify",
innerText:"Beautify",
clickListener:()=>{
beautifyScript();
return false;