0
0

variable type helpers

This commit is contained in:
DrMaxNix 2022-10-01 20:12:34 +02:00
parent 3a1decb368
commit 5ed0341061
2 changed files with 49 additions and 0 deletions

View File

@ -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 = {};

View File

@ -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
*/