Skip to content

Commit

Permalink
Move step code into JS
Browse files Browse the repository at this point in the history
  • Loading branch information
mmastrac committed May 8, 2024
1 parent 57a121b commit e7df06b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,47 +94,13 @@ public ACodeInterface(String file, JavaScriptObject callbacks) {

stateChange0("LOADING");

timer = new Timer() {
@Override
public void run() {
if (!paused) {
try {
trace0("Running code...");
// Run only if the interpreter is running
for (int i = 0; i < 100; i++) {
InterpreterState state = interpreter.step();
if (state == InterpreterState.COMPLETED) {
timer.cancel();
trace0("Done.");
refreshScore();
stateChange0("COMPLETED");
} else if (state == InterpreterState.WAITING_FOR_INPUT || state == InterpreterState.WAITING_FOR_QUERY) {
paused = true;
trace0("Need input: " + state);
refreshScore();
stateChange0("INPUT");
}

if (state != InterpreterState.RUNNING) {
trace0("Exiting loop: " + state);
return;
}
}
} catch (Throwable t) {
trace0("Uh-oh, internal error: " + t.getClass() + " " + t.getMessage());
}
}
}
};

try {
program = ACodeParser.parseProgram(file);
environment = new WebEnvironment();
WorldBuilder kernelStateBuilder = new WorldBuilder(program);
world = kernelStateBuilder.getState();
interpreter = new Interpreter(environment, world, new VirtualMachine());

timer.scheduleRepeating(1);
stateChange0("RUNNING");
} catch (ParseException e) {
stateChange0("ERROR");
Expand All @@ -146,6 +112,7 @@ public void input(String input) {
trace0("Got input: " + input);
environment.setQueuedInput(input);
paused = false;
stateChange0("RUNNING");
}

private Map<String, String> restore() {
Expand Down Expand Up @@ -241,4 +208,20 @@ public void pause() {
public void resume() {
this.paused = false;
}

public void step() {
if (!this.paused) {
InterpreterState state = interpreter.step();
if (state == InterpreterState.COMPLETED) {
trace0("Done.");
refreshScore();
stateChange0("COMPLETED");
} else if (state == InterpreterState.WAITING_FOR_INPUT || state == InterpreterState.WAITING_FOR_QUERY) {
paused = true;
trace0("Need input: " + state);
refreshScore();
stateChange0("INPUT");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ private native void attachToDocument() /*-{
[email protected]::resume()();
}
$wnd.ACode.prototype.step = function() {
[email protected]::step()();
}
if ($wnd.ACodeReady)
$wnd.ACodeReady();
}-*/;
Expand Down
28 changes: 24 additions & 4 deletions com.grack.adventure.web/war/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ window.ACodeReady = async function() {
// Asynchronously load the script
screen[1] = "Loading ADVENTURE.ACODE...";
let code;
let repeat;
try {
let timeout = 100;
let repeat;
let repeater = () => {
screen[1] += '.';
timeout *= 2;
Expand All @@ -54,9 +54,16 @@ window.ACodeReady = async function() {
let wait = new Promise(r => setTimeout(r, 1000));
code = (await fetch('ADVENTURE.ACODE')).text();
code = (await Promise.all([wait, code]))[1];
clearTimeout(repeat);
if (!code.includes("XYZZY")) {
throw new Error("ACODE file was corrupt");
}
} catch (e) {
print("Failed to load ADVENTURE.ACODE!\n");
screen[1] = "Failed to load ADVENTURE.ACODE!";
screen[2] = e.message;
refreshOutput();
return;
} finally {
clearTimeout(repeat);
}

acode = new ACode(code, {
Expand Down Expand Up @@ -206,10 +213,23 @@ function print(string) {

function stateChange(state) {
log("TRACE", "State -> " + state);

if (currentState == 'LOADING') {
refreshOutput();
setInterval(() => {
for (let i = 0; i < 100; i++) {
if (currentState == "RUNNING") {
acode.step()
} else {
break;
}
}
}, 1);
}


if (state == 'RUNNING') {
}

if (state == 'INPUT') {
restartBlink();

Expand Down

0 comments on commit e7df06b

Please sign in to comment.