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,6 +10,11 @@
the Google CDN (Content Delivery Network). --> the Google CDN (Content Delivery Network). -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <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/Constants.js"></script>
<script src="src/Server.js"></script> <script src="src/Server.js"></script>
@ -18,11 +23,7 @@
<script src="src/Company.js"></script> <script src="src/Company.js"></script>
<script src="src/Terminal.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> <script src="src/engine.js"></script>
</head> </head>

@ -51,7 +51,7 @@ function evaluate(exp, env) {
cond = evaluate(exp.cond, 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; break;
case "while": case "while":
cond = evaluate(exp.cond, env); cond = evaluate(exp.cond, env);
@ -61,7 +61,7 @@ function evaluate(exp, env) {
cond = evaluate(exp.cond, 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; break;
case "prog": case "prog":
var val = false; var val = false;
@ -85,7 +85,7 @@ function evaluate(exp, env) {
} else if (exp.func.value == "sleep") { } else if (exp.func.value == "sleep") {
console.log("Execute sleep()"); console.log("Execute sleep()");
} else if (exp.func.value == "print") { } else if (exp.func.value == "print") {
console.log(evaluate(exp.args[0], env)); post(evaluate(exp.args[0], env).toString());
} }
break; break;

@ -104,8 +104,12 @@ function Tokenizer(input) {
return str; return str;
} }
function read_string() { function read_string(ch) {
return { type: "str", value: read_escaped('"') }; if (ch == '"') {
return { type: "str", value: read_escaped('"') };
} else if (ch == '\'') {
return { type: "str", value: read_escaped('\'') };
}
} }
//Only supports single-line comments right now //Only supports single-line comments right now
@ -130,7 +134,7 @@ function Tokenizer(input) {
return read_next(); return read_next();
} }
if (ch == '"') return read_string(); if (ch == '"' || ch == '\'') return read_string(ch);
if (is_digit(ch)) return read_number(); if (is_digit(ch)) return read_number();
if (is_id_start(ch)) return read_ident(); if (is_id_start(ch)) return read_ident();
if (is_punc(ch)) return { 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; this.purchasedByPlayer = false;
//RAM, CPU speed and Scripts //RAM, CPU speed and Scripts
this.maxRam = 1; //GB this.maxRam = 1; //GB
this.ramUsed = 0; this.ramUsed = 0;
this.cpuSpeed = 1; //MHz this.cpuSpeed = 1; //MHz

@ -119,6 +119,12 @@ var Terminal = {
post("SQL port: Closed") post("SQL port: Closed")
} }
Terminal.analyzeFlag = false; 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) { executeCommand: function(command) {
@ -292,10 +298,11 @@ var Terminal = {
case "test": case "test":
//TODO //TODO
//TESTED: print, for loops //TESTED: print, for loops, while loops, prog,
//UNTESTED: // 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))); var ast = Parser(Tokenizer(InputStream(code)));
console.log("Printing AST below") console.log("Printing AST below")
console.log(ast); console.log(ast);

@ -38,10 +38,8 @@ var Engine = {
}, },
//Time variables (milliseconds unix epoch time) //Time variables (milliseconds unix epoch time)
_timeThen: new Date().getTime(), _lastUpdate: new Date().getTime(),
_timeNow: new Date().getTime(),
_ticks: 0, //Total ticks
_idleSpeed: 200, //Speed (in ms) at which the main loop is updated _idleSpeed: 200, //Speed (in ms) at which the main loop is updated
//Display a status update text //Display a status update text
@ -121,22 +119,24 @@ var Engine = {
/* Main Event Loop */ /* Main Event Loop */
idleTimer: function() { idleTimer: function() {
//Get time difference //Get time difference
Engine._timeNow = new Date().getTime(); var _thisUpdate = new Date().getTime();
var timeDifference = Engine._timeNow - Engine._timeThen - Engine._ticks; var diff = _thisUpdate - Engine._lastUpdate;
while (timeDifference >= Engine._idleSpeed) { //Divide this by cycle time to determine how many cycles have elapsed since last update
//Engine.Display.hacking_skill.innerHTML = Player.hacking_skill; diff = Math.round(diff / Engine._idleSpeed);
//Update timeDifference based on the idle speed if (diff > 0) {
timeDifference -= Engine._idleSpeed; //Update the game engine by the calculated number of cycles
Engine.updateGame(diff);
Engine._lastUpdate = _thisUpdate;
}
//Update the total tick counter window.requestAnimationFrame(Engine.idleTimer);
Engine._ticks += Engine._idleSpeed; },
}
var idleTime = Engine._idleSpeed - timeDifference; //TODO Account for numCycles in Code, hasn't been done yet
updateGame: function(numCycles = 1) {
//Manual hack //Manual hack
if (Player.startAction == true) { if (Player.startAction == true) {
Engine._totalActionTime = Player.actionTime; Engine._totalActionTime = Player.actionTime;
Engine._actionTimeLeft = Player.actionTime; Engine._actionTimeLeft = Player.actionTime;
@ -150,13 +150,7 @@ var Engine = {
} }
Engine.updateHackProgress(); 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 */ /* Calculates the hack progress for a manual (non-scripted) hack and updates the progress bar/time accordingly */
_totalActionTime: 0, _totalActionTime: 0,