🏗️ split command definition into validate
and execute
functions
This commit is contained in:
parent
471b089454
commit
528700b582
@ -1,14 +1,19 @@
|
||||
Juicescript.command_add({
|
||||
Juicescript.command_define({
|
||||
name: "drw",
|
||||
alias: ["draw", "echo"],
|
||||
function: function(runner){
|
||||
// VALIDATE ARGUMENTS //
|
||||
|
||||
validate: function(runner){
|
||||
// count
|
||||
runner.argument_validate_count({min: 1, max: null});
|
||||
if(runner.has_error) return;
|
||||
|
||||
// types
|
||||
for(var q = 1; q <= runner.command.argument.length; q++){
|
||||
runner.argument_validate_type(q, Juicescript.argument_type.VALUE);
|
||||
}
|
||||
},
|
||||
|
||||
execute: function(runner){
|
||||
|
||||
}
|
||||
});
|
||||
|
@ -1,13 +1,21 @@
|
||||
Juicescript.command_add({
|
||||
Juicescript.command_define({
|
||||
name: "mov",
|
||||
alias: ["move", "set"],
|
||||
function: function(runner){
|
||||
// VALIDATE ARGUMENTS //
|
||||
|
||||
validate: function(runner){
|
||||
// count
|
||||
runner.argument_validate_count(2);
|
||||
if(runner.has_error) return;
|
||||
|
||||
// types
|
||||
runner.argument_validate_type(1, Juicescript.argument_type.VARIABLE);
|
||||
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);
|
||||
}
|
||||
});
|
||||
|
24
src/main.js
24
src/main.js
@ -130,7 +130,29 @@ class Juicescript {
|
||||
/*
|
||||
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])){
|
||||
// MAKE SURE WE'RE NOT RE-DEFINING IT //
|
||||
if(Object.keys(Juicescript.command).includes(name_or_alias)){
|
||||
|
@ -30,7 +30,7 @@ class Juicescript_runner {
|
||||
// RUN FULL PROGRAM //
|
||||
while(true){
|
||||
// end execution on error
|
||||
if(this.error_count > 0) break;
|
||||
if(this.has_error) break;
|
||||
|
||||
// handle end of command list
|
||||
if(this.handle_command_list_end()) break;
|
||||
@ -52,8 +52,15 @@ class Juicescript_runner {
|
||||
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);
|
||||
// run validate function
|
||||
Juicescript.command[this.command.name].validate(this);
|
||||
|
||||
// maybe run execute function
|
||||
if(!this.has_error){
|
||||
Juicescript.command[this.command.name].execute(this);
|
||||
}
|
||||
|
||||
// done
|
||||
return;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user