From d372ce598086233a0bc48d3f7729bdb6ef35785a Mon Sep 17 00:00:00 2001 From: Daniel Xie Date: Mon, 21 Nov 2016 00:11:14 -0600 Subject: [PATCH] Fixed some bugs, began adding a Script class --- index.html | 13 +++++------ src/Netscript/Evaluator.js | 6 +++--- src/Netscript/Tokenizer.js | 10 ++++++--- src/Script.js | 42 ++++++++++++++++++++++++++++++++++++ src/Server.js | 2 +- src/Terminal.js | 13 ++++++++--- src/engine.js | 44 ++++++++++++++++---------------------- 7 files changed, 89 insertions(+), 41 deletions(-) create mode 100644 src/Script.js diff --git a/index.html b/index.html index bf7678393..1d6087b5c 100644 --- a/index.html +++ b/index.html @@ -10,7 +10,12 @@ the Google CDN (Content Delivery Network). --> - + + + + + + @@ -18,11 +23,7 @@ - - - - - + diff --git a/src/Netscript/Evaluator.js b/src/Netscript/Evaluator.js index 1cda28c9b..827c4be2c 100644 --- a/src/Netscript/Evaluator.js +++ b/src/Netscript/Evaluator.js @@ -51,7 +51,7 @@ function evaluate(exp, env) { cond = evaluate(exp.cond, env); } - //TODO Return somethin? + //TODO I don't think I need to return anything..but I might be wrong break; case "while": cond = evaluate(exp.cond, env); @@ -61,7 +61,7 @@ function evaluate(exp, env) { cond = evaluate(exp.cond, env); } - //TODO DO i need to return anything? + //TODO I don't think I need to return anything..but I might be wrong break; case "prog": var val = false; @@ -85,7 +85,7 @@ function evaluate(exp, env) { } else if (exp.func.value == "sleep") { console.log("Execute sleep()"); } else if (exp.func.value == "print") { - console.log(evaluate(exp.args[0], env)); + post(evaluate(exp.args[0], env).toString()); } break; diff --git a/src/Netscript/Tokenizer.js b/src/Netscript/Tokenizer.js index f7e83252c..d7307e775 100644 --- a/src/Netscript/Tokenizer.js +++ b/src/Netscript/Tokenizer.js @@ -104,8 +104,12 @@ function Tokenizer(input) { return str; } - function read_string() { - return { type: "str", value: read_escaped('"') }; + function read_string(ch) { + if (ch == '"') { + return { type: "str", value: read_escaped('"') }; + } else if (ch == '\'') { + return { type: "str", value: read_escaped('\'') }; + } } //Only supports single-line comments right now @@ -130,7 +134,7 @@ function Tokenizer(input) { return read_next(); } - if (ch == '"') return read_string(); + if (ch == '"' || ch == '\'') return read_string(ch); if (is_digit(ch)) return read_number(); if (is_id_start(ch)) return read_ident(); if (is_punc(ch)) return { diff --git a/src/Script.js b/src/Script.js new file mode 100644 index 000000000..cef6a33f8 --- /dev/null +++ b/src/Script.js @@ -0,0 +1,42 @@ +/* Script.js + * Script object + */ +function Script() { + //Function queue that holds the next functions to be + //executed in this script. A function from this queue + //is executed every second (this may change) + this.functionQueue = []; + + this.code = ""; + this.ramUsage = 0; + +} + +//Execute the next function in the Script's function queue +Script.prototype.executeNext() { + if (this.functionQueue.length <= 0) {return;} + + //Shift the next element off ths function queue and then execute it + (this.functionQueue.shift())(); +} + +/* Wrapper object that wraps a function with its arguments. + * These objects are pushed onto a Script object's function queue. + * The functions can be called with the standard () operator + * + * Example: + * //Define the function + * var fooFunc = function(a1, a2, a3) { + * return a1 + a2 + a3; + * } + * //Wrap the function in the wrapper object + * var fooObj = functionObject(fooFunc, this, [2, 3, 4]); + * //Call the function + * fooObj(); + * + */ +function functionObject = function(fn, context, params) { + return function() { + fn.apply(context, params); + } +} \ No newline at end of file diff --git a/src/Server.js b/src/Server.js index 32a88aea5..c7d2ec07d 100644 --- a/src/Server.js +++ b/src/Server.js @@ -13,7 +13,7 @@ function Server() { this.purchasedByPlayer = false; //RAM, CPU speed and Scripts - this.maxRam = 1; //GB + this.maxRam = 1; //GB this.ramUsed = 0; this.cpuSpeed = 1; //MHz diff --git a/src/Terminal.js b/src/Terminal.js index 8cf6b2a8c..56b932995 100644 --- a/src/Terminal.js +++ b/src/Terminal.js @@ -119,6 +119,12 @@ var Terminal = { post("SQL port: Closed") } Terminal.analyzeFlag = false; + + //Rename the progress bar so that the next hacks dont trigger it. Re-enable terminal + $("#hack-progress-bar").attr('id', "old-hack-progress-bar"); + $("#hack-progress").attr('id', "old-hack-progress"); + document.getElementById("terminal-input-td").innerHTML = '$ '; + $('input[class=terminal-input]').prop('disabled', false); }, executeCommand: function(command) { @@ -292,10 +298,11 @@ var Terminal = { case "test": //TODO - //TESTED: print, for loops - //UNTESTED: + //TESTED: print, for loops, while loops, prog, + // basic ops, var, assign all seem fine + //UNTESTED: if, elif, else - var code = "i = 0; while (i < 100000000000) {print(i); i = i+1;}"; + var code = "i = 0; while (i <= 20) {print(i); i = i+2; hack(); sleep();}"; var ast = Parser(Tokenizer(InputStream(code))); console.log("Printing AST below") console.log(ast); diff --git a/src/engine.js b/src/engine.js index bb34e361c..e0d2ee421 100644 --- a/src/engine.js +++ b/src/engine.js @@ -38,10 +38,8 @@ var Engine = { }, //Time variables (milliseconds unix epoch time) - _timeThen: new Date().getTime(), - _timeNow: new Date().getTime(), + _lastUpdate: new Date().getTime(), - _ticks: 0, //Total ticks _idleSpeed: 200, //Speed (in ms) at which the main loop is updated //Display a status update text @@ -121,22 +119,24 @@ var Engine = { /* Main Event Loop */ idleTimer: function() { //Get time difference - Engine._timeNow = new Date().getTime(); - var timeDifference = Engine._timeNow - Engine._timeThen - Engine._ticks; + var _thisUpdate = new Date().getTime(); + var diff = _thisUpdate - Engine._lastUpdate; + + //Divide this by cycle time to determine how many cycles have elapsed since last update + diff = Math.round(diff / Engine._idleSpeed); + + if (diff > 0) { + //Update the game engine by the calculated number of cycles + Engine.updateGame(diff); + Engine._lastUpdate = _thisUpdate; + } - while (timeDifference >= Engine._idleSpeed) { - //Engine.Display.hacking_skill.innerHTML = Player.hacking_skill; - - //Update timeDifference based on the idle speed - timeDifference -= Engine._idleSpeed; - - //Update the total tick counter - Engine._ticks += Engine._idleSpeed; - } - - var idleTime = Engine._idleSpeed - timeDifference; - - //Manual hack + window.requestAnimationFrame(Engine.idleTimer); + }, + + //TODO Account for numCycles in Code, hasn't been done yet + updateGame: function(numCycles = 1) { + //Manual hack if (Player.startAction == true) { Engine._totalActionTime = Player.actionTime; Engine._actionTimeLeft = Player.actionTime; @@ -150,13 +150,7 @@ var Engine = { } Engine.updateHackProgress(); - - // Once that entire "while loop" has run, we call the IdleTimer - // function again, but this time with a timeout (delay) of - // _idleSpeed minus timeDifference - setTimeout(Engine.idleTimer, idleTime); - - }, + }, /* Calculates the hack progress for a manual (non-scripted) hack and updates the progress bar/time accordingly */ _totalActionTime: 0,