✨ variable type helpers
This commit is contained in:
parent
3a1decb368
commit
5ed0341061
13
src/main.js
13
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 = {};
|
||||
|
@ -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
|
||||
*/
|
||||
|
Reference in New Issue
Block a user