Fixed some bugs, began adding a Script class

This commit is contained in:
Daniel Xie 2016-11-21 00:11:14 -06:00
parent 8d87b74eaf
commit d372ce5980
7 changed files with 89 additions and 41 deletions

@ -10,7 +10,12 @@
the Google CDN (Content Delivery Network). -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="src/netscript/InputStream.js"></script>
<script src="src/netscript/Tokenizer.js"></script>
<script src="src/netscript/Parser.js"></script>
<script src="src/netscript/Environment.js"></script>
<script src="src/netscript/Evaluator.js"></script>
<script src="src/Constants.js"></script>
<script src="src/Server.js"></script>
<script src="src/Player.js"></script>
@ -18,11 +23,7 @@
<script src="src/Company.js"></script>
<script src="src/Terminal.js"></script>
<script src="src/netscript/InputStream.js"></script>
<script src="src/netscript/Tokenizer.js"></script>
<script src="src/netscript/Parser.js"></script>
<script src="src/netscript/Environment.js"></script>
<script src="src/netscript/Evaluator.js"></script>
<script src="src/engine.js"></script>
</head>

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

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

42
src/Script.js Normal file

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

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

@ -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 type="text" class="terminal-input"/>';
$('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);

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