diff --git a/src/main.js b/src/main.js index 16ea9c9..3b735d1 100644 --- a/src/main.js +++ b/src/main.js @@ -42,6 +42,19 @@ class Juicescript { ); + // VARIABLE TYPES // + static variable_type = new Juicescript_helper_enum( + // null + "NULL", + + // boolean + "BOOl", + + // string + "NUM", "STR" + ); + + // COMMAND FUNCTIONS // static command = {}; diff --git a/src/runner.js b/src/runner.js index a7009d8..637fa38 100644 --- a/src/runner.js +++ b/src/runner.js @@ -335,6 +335,42 @@ class Juicescript_runner { return value; } + /* + VARIABLE HELPER: Get a variable's data type + */ + variable_type(variable){ + // LOAD VALUE // + let value = this.variable_get(variable); + + + // CHECK SPECIAL CASES // + // null + if(value === null){ + return Juicescript.variable_type.NULL; + } + + + // CONVERT TO ENUM FROM JAVASCRIPT TYPE // + // get javascript's type + let js_type = typeof value; + + // try to convert to enum + let type = ({ + "number": Juicescript.variable_type.NUM, + "boolean": Juicescript.variable_type.BOOl, + "string": Juicescript.variable_type.STR, + })[js_type] ?? null; + + // lookup error? + if(type === null){ + throw "unable to convert javascript type '" + js_type + "'"; + } + + + // RETURN // + return type; + } + /* VARIABLE HELPER: Set a variable's value */