♻️ simplify data type checks
This commit is contained in:
parent
72454d3538
commit
b21383afcd
@ -320,7 +320,7 @@ class Juicescript_runner {
|
|||||||
// value
|
// value
|
||||||
if(type === Juicescript.argument_type.VALUE){
|
if(type === Juicescript.argument_type.VALUE){
|
||||||
if(![Juicescript.argument_type.VARIABLE, Juicescript.argument_type.LITERAL].includes(actual_type)){
|
if(![Juicescript.argument_type.VARIABLE, Juicescript.argument_type.LITERAL].includes(actual_type)){
|
||||||
this.error(this.command.name + ", argument " + number + ": expected " + Juicescript.argument_type.name(type) + ", but got " + Juicescript.argument_type.name(actual_type));
|
error_argument(number, "expected " + Juicescript.argument_type.name(type) + ", but got " + Juicescript.argument_type.name(actual_type));
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -328,7 +328,7 @@ class Juicescript_runner {
|
|||||||
|
|
||||||
// COMPARE AGAINST PARSABLE TYPES //
|
// COMPARE AGAINST PARSABLE TYPES //
|
||||||
if(actual_type !== type){
|
if(actual_type !== type){
|
||||||
this.error(this.command.name + ", argument " + number + ": expected " + Juicescript.argument_type.name(type) + ", but got " + Juicescript.argument_type.name(actual_type));
|
error_argument(number, "expected " + Juicescript.argument_type.name(type) + ", but got " + Juicescript.argument_type.name(actual_type));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -469,31 +469,11 @@ class Juicescript_runner {
|
|||||||
let value = this.variable_get(variable);
|
let value = this.variable_get(variable);
|
||||||
|
|
||||||
|
|
||||||
// CHECK SPECIAL CASES //
|
// RETURN TYPE //
|
||||||
// null
|
// get type
|
||||||
if(value === null){
|
let type = this.data_type(value);
|
||||||
return Juicescript.variable_type.NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// return
|
||||||
// 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;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -544,6 +524,38 @@ class Juicescript_runner {
|
|||||||
return (this.error_count > 0);
|
return (this.error_count > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
HELPER: Get data type of VALUE
|
||||||
|
*/
|
||||||
|
data_type(value){
|
||||||
|
// CHECK SPECIAL CASES //
|
||||||
|
// null
|
||||||
|
if(value === null){
|
||||||
|
return Juicescript.data_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.data_type.NUM,
|
||||||
|
"boolean": Juicescript.data_type.BOOL,
|
||||||
|
"string": Juicescript.data_type.STR,
|
||||||
|
})[js_type] ?? null;
|
||||||
|
|
||||||
|
// lookup error?
|
||||||
|
if(type === null){
|
||||||
|
throw "unable to convert javascript type '" + js_type + "'";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// RETURN //
|
||||||
|
return type
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
HELPER: Express VALUE as a string
|
HELPER: Express VALUE as a string
|
||||||
*/
|
*/
|
||||||
@ -558,6 +570,18 @@ class Juicescript_runner {
|
|||||||
return value.toString();
|
return value.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
HELPER: Automagically produce error messages
|
||||||
|
*/
|
||||||
|
warning_argument(number, text){
|
||||||
|
// construct warning message
|
||||||
|
this.warning(this.command.name + ", argument " + number + ": " + text);
|
||||||
|
}
|
||||||
|
error_argument(number, text){
|
||||||
|
// construct error message
|
||||||
|
this.error(this.command.name + ", argument " + number + ": " + text);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
HELPER: Automagically keep track of problems and add additional info to stderr
|
HELPER: Automagically keep track of problems and add additional info to stderr
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user