✨ runner state machine
This commit is contained in:
parent
4e1539e85c
commit
195af72d01
@ -2,6 +2,6 @@ Juicescript.command_add({
|
|||||||
name: "drw",
|
name: "drw",
|
||||||
alias: ["draw", "echo"],
|
alias: ["draw", "echo"],
|
||||||
function: function(runner){
|
function: function(runner){
|
||||||
/**/runner.io.stdout("drw");
|
/**/runner.io.stdout(runner.command.argument[0].value);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -1,7 +0,0 @@
|
|||||||
Juicescript.command_add({
|
|
||||||
name: "mov",
|
|
||||||
alias: ["move", "set"],
|
|
||||||
function: function(runner){
|
|
||||||
/**/runner.debug("mov");
|
|
||||||
}
|
|
||||||
});
|
|
114
src/runner.js
114
src/runner.js
@ -15,8 +15,110 @@ class Juicescript_runner {
|
|||||||
MAIN: Do running
|
MAIN: Do running
|
||||||
*/
|
*/
|
||||||
run(){
|
run(){
|
||||||
/**/this.command = this.tree.root.command[0];
|
// RESET //
|
||||||
/**/this.debug(this.tree);
|
// command counter
|
||||||
|
this.counter = 0;
|
||||||
|
|
||||||
|
// current scope
|
||||||
|
this.scope = null;
|
||||||
|
|
||||||
|
// warning and error counter
|
||||||
|
this.warning_count = 0;
|
||||||
|
this.error_count = 0;
|
||||||
|
|
||||||
|
|
||||||
|
// RUN FULL PROGRAM //
|
||||||
|
while(true){
|
||||||
|
// end execution on error
|
||||||
|
if(this.error_count > 0) break;
|
||||||
|
|
||||||
|
// handle end of command list
|
||||||
|
if(this.handle_command_list_end()) break;
|
||||||
|
|
||||||
|
// load next command
|
||||||
|
this.command_load();
|
||||||
|
|
||||||
|
// set new default command counter
|
||||||
|
this.counter++;
|
||||||
|
|
||||||
|
// run
|
||||||
|
this.run_one();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
HELPER: Run one command at current position
|
||||||
|
*/
|
||||||
|
run_one(){
|
||||||
|
// TRY AS BUILT-IN COMMAND //
|
||||||
|
if(Object.keys(Juicescript.command).includes(this.command.name)){
|
||||||
|
// execute function
|
||||||
|
Juicescript.command[this.command.name].function(this);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// TRY AS USER-DEFINED COMMAND //
|
||||||
|
if(Object.keys(this.tree.scope).includes(this.command.name)){
|
||||||
|
//*/ TODO: enter scope
|
||||||
|
/**/this.error("user-defined: " + this.command.name);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// UNKNOWN COMMAND //
|
||||||
|
// stop with error
|
||||||
|
this.error("unknown command '" + this.command.name + "'");
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
HELPER: Handle possible end of command list
|
||||||
|
*/
|
||||||
|
handle_command_list_end(){
|
||||||
|
// CHECK IF AT END OF COMMAND LIST //
|
||||||
|
if(this.counter < this.scope_tree.command.length){
|
||||||
|
// not at end, continue
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// IN USER-DEFINED SCOPE //
|
||||||
|
if(this.scope !== null){
|
||||||
|
// return from scope
|
||||||
|
//*/ TODO: return from scope
|
||||||
|
/**/this.error("return from scope");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// HALT PROGRAM //
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
HELPER: Load next command from current scope at current command counter
|
||||||
|
*/
|
||||||
|
command_load(){
|
||||||
|
this.command = this.scope_tree.command[this.counter];
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
GETTER: Return tree of current scope
|
||||||
|
*/
|
||||||
|
get scope_tree(){
|
||||||
|
// are we in root scope?
|
||||||
|
if(this.scope === null){
|
||||||
|
// return root scope
|
||||||
|
return this.tree.root;
|
||||||
|
}
|
||||||
|
|
||||||
|
// make sure this scope exists
|
||||||
|
if(!Object.keys(this.tree.scope).includes(this.scope)){
|
||||||
|
throw "unknown scope " + this.scope;
|
||||||
|
}
|
||||||
|
|
||||||
|
// return scope
|
||||||
|
return this.tree.scope[this.scope];
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -25,7 +127,7 @@ class Juicescript_runner {
|
|||||||
debug(text, additional){
|
debug(text, additional){
|
||||||
// add defaults
|
// add defaults
|
||||||
additional ??= {};
|
additional ??= {};
|
||||||
additional.line ??= this.command.line;
|
additional.line ??= (this.command === undefined ? null : this.command.line);
|
||||||
|
|
||||||
// forward
|
// forward
|
||||||
this.io.stderr.debug(text, additional);
|
this.io.stderr.debug(text, additional);
|
||||||
@ -33,7 +135,7 @@ class Juicescript_runner {
|
|||||||
info(text, additional){
|
info(text, additional){
|
||||||
// add defaults
|
// add defaults
|
||||||
additional ??= {};
|
additional ??= {};
|
||||||
additional.line ??= this.command.line;
|
additional.line ??= (this.command === undefined ? null : this.command.line);
|
||||||
|
|
||||||
// forward
|
// forward
|
||||||
this.io.stderr.info(text, additional);
|
this.io.stderr.info(text, additional);
|
||||||
@ -46,7 +148,7 @@ class Juicescript_runner {
|
|||||||
// PRINT MESSAGE //
|
// PRINT MESSAGE //
|
||||||
// add defaults
|
// add defaults
|
||||||
additional ??= {};
|
additional ??= {};
|
||||||
additional.line ??= this.command.line;
|
additional.line ??= (this.command === undefined ? null : this.command.line);
|
||||||
|
|
||||||
// forward
|
// forward
|
||||||
this.io.stderr.warning(text, additional);
|
this.io.stderr.warning(text, additional);
|
||||||
@ -59,7 +161,7 @@ class Juicescript_runner {
|
|||||||
// PRINT MESSAGE //
|
// PRINT MESSAGE //
|
||||||
// add defaults
|
// add defaults
|
||||||
additional ??= {};
|
additional ??= {};
|
||||||
additional.line ??= this.command.line;
|
additional.line ??= (this.command === undefined ? null : this.command.line);
|
||||||
|
|
||||||
// forward
|
// forward
|
||||||
this.io.stderr.error(text, additional);
|
this.io.stderr.error(text, additional);
|
||||||
|
Reference in New Issue
Block a user