From 528700b582cac862cd61ab32f5403368be3b1e1f Mon Sep 17 00:00:00 2001 From: DrMaxNix Date: Sat, 1 Oct 2022 17:44:20 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=8F=97=EF=B8=8F=20split=20command=20defin?= =?UTF-8?q?ition=20into=20`validate`=20and=20`execute`=20functions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/command/drw.js | 11 ++++++++--- src/command/mov.js | 14 +++++++++++--- src/main.js | 24 +++++++++++++++++++++++- src/runner.js | 13 ++++++++++--- 4 files changed, 52 insertions(+), 10 deletions(-) diff --git a/src/command/drw.js b/src/command/drw.js index a67bce9..fd4dd61 100644 --- a/src/command/drw.js +++ b/src/command/drw.js @@ -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){ + } }); diff --git a/src/command/mov.js b/src/command/mov.js index a968ab7..8ad2641 100644 --- a/src/command/mov.js +++ b/src/command/mov.js @@ -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); } }); diff --git a/src/main.js b/src/main.js index 65fdd0b..16ea9c9 100644 --- a/src/main.js +++ b/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)){ diff --git a/src/runner.js b/src/runner.js index aad3ffb..643bcbc 100644 --- a/src/runner.js +++ b/src/runner.js @@ -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; }