0
0

🏗️ split command definition into validate and execute functions

This commit is contained in:
DrMaxNix 2022-10-01 17:44:20 +02:00
parent 471b089454
commit 528700b582
4 changed files with 52 additions and 10 deletions

View File

@ -1,14 +1,19 @@
Juicescript.command_add({ Juicescript.command_define({
name: "drw", name: "drw",
alias: ["draw", "echo"], alias: ["draw", "echo"],
function: function(runner){
// VALIDATE ARGUMENTS // validate: function(runner){
// count // count
runner.argument_validate_count({min: 1, max: null}); runner.argument_validate_count({min: 1, max: null});
if(runner.has_error) return;
// types // types
for(var q = 1; q <= runner.command.argument.length; q++){ for(var q = 1; q <= runner.command.argument.length; q++){
runner.argument_validate_type(q, Juicescript.argument_type.VALUE); runner.argument_validate_type(q, Juicescript.argument_type.VALUE);
} }
},
execute: function(runner){
} }
}); });

View File

@ -1,13 +1,21 @@
Juicescript.command_add({ Juicescript.command_define({
name: "mov", name: "mov",
alias: ["move", "set"], alias: ["move", "set"],
function: function(runner){
// VALIDATE ARGUMENTS // validate: function(runner){
// count // count
runner.argument_validate_count(2); runner.argument_validate_count(2);
if(runner.has_error) return;
// types // types
runner.argument_validate_type(1, Juicescript.argument_type.VARIABLE); runner.argument_validate_type(1, Juicescript.argument_type.VARIABLE);
runner.argument_validate_type(2, Juicescript.argument_type.VALUE); runner.argument_validate_type(2, Juicescript.argument_type.VALUE);
},
execute: function(runner){
// ASSIGN VALUE //
// get value to assign
let value = runner.argument_value(2);
/**/runner.debug(value);
} }
}); });

View File

@ -130,7 +130,29 @@ class Juicescript {
/* /*
HELPER: Add command with expanded command aliases to command function list HELPER: Add command with expanded command aliases to command function list
*/ */
static command_add(command){ static command_define(command){
// CHECK IF ALL ATTRIBUTES HAVE BEEN SET //
// name
if(!Object.keys(command).includes("name")){
throw "command definition missing its name";
}
// alias
if(!Object.keys(command).includes("alias")){
throw "command definition '" + command.name + "' missing its alias list";
}
// validate function
if(!(command.validate instanceof Function)){
throw "command definition '" + command.name + "' missing its validate function";
}
// execute function
if(!(command.execute instanceof Function)){
throw "command definition '" + command.name + "' missing its execute function";
}
for(var name_or_alias of command.alias.concat([command.name])){ for(var name_or_alias of command.alias.concat([command.name])){
// MAKE SURE WE'RE NOT RE-DEFINING IT // // MAKE SURE WE'RE NOT RE-DEFINING IT //
if(Object.keys(Juicescript.command).includes(name_or_alias)){ if(Object.keys(Juicescript.command).includes(name_or_alias)){

View File

@ -30,7 +30,7 @@ class Juicescript_runner {
// RUN FULL PROGRAM // // RUN FULL PROGRAM //
while(true){ while(true){
// end execution on error // end execution on error
if(this.error_count > 0) break; if(this.has_error) break;
// handle end of command list // handle end of command list
if(this.handle_command_list_end()) break; if(this.handle_command_list_end()) break;
@ -52,8 +52,15 @@ class Juicescript_runner {
run_one(){ run_one(){
// TRY AS BUILT-IN COMMAND // // TRY AS BUILT-IN COMMAND //
if(Object.keys(Juicescript.command).includes(this.command.name)){ if(Object.keys(Juicescript.command).includes(this.command.name)){
// execute function // run validate function
Juicescript.command[this.command.name].function(this); Juicescript.command[this.command.name].validate(this);
// maybe run execute function
if(!this.has_error){
Juicescript.command[this.command.name].execute(this);
}
// done
return; return;
} }