mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-10 04:57:00 -04:00
Allow "or else" construct to be used for error recovery
git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@1016 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
@@ -21,6 +21,10 @@ Convert any value to a [[type:int]].
|
|||||||
> to_int(true) == 1
|
> to_int(true) == 1
|
||||||
> to_int(rgb(255,255,255)) == 255
|
> to_int(rgb(255,255,255)) == 255
|
||||||
|
|
||||||
|
Use @or else@ to recover from errors:
|
||||||
|
> to_number("not a number") or else 0 == 0
|
||||||
|
> to_number("123") or else 0 == 123
|
||||||
|
|
||||||
--See also--
|
--See also--
|
||||||
| [[fun:to_real]] Convert any value to a [[type:double]]
|
| [[fun:to_real]] Convert any value to a [[type:double]]
|
||||||
| [[fun:to_number]] Convert any value to a number
|
| [[fun:to_number]] Convert any value to a number
|
||||||
|
|||||||
@@ -20,6 +20,10 @@ Otherwise it is converted to a [[type:double]].
|
|||||||
> to_number("1") == 1
|
> to_number("1") == 1
|
||||||
> to_number("1.5") == 1.5
|
> to_number("1.5") == 1.5
|
||||||
|
|
||||||
|
Use @or else@ to recover from errors:
|
||||||
|
> to_number("not a number") or else 0 == 0
|
||||||
|
> to_number("123") or else 0 == 123
|
||||||
|
|
||||||
--See also--
|
--See also--
|
||||||
| [[fun:to_int]] Convert any value to a [[type:int]]
|
| [[fun:to_int]] Convert any value to a [[type:int]]
|
||||||
| [[fun:to_real]] Convert any value to a [[type:double]]
|
| [[fun:to_real]] Convert any value to a [[type:double]]
|
||||||
|
|||||||
@@ -16,6 +16,10 @@ Convert any value to a [[type:double]].
|
|||||||
> to_real(1) == 1.0
|
> to_real(1) == 1.0
|
||||||
> to_real("1.5") == 1.5
|
> to_real("1.5") == 1.5
|
||||||
|
|
||||||
|
Use @or else@ to recover from errors:
|
||||||
|
> to_number("not a number") or else 0.0 == 0.0
|
||||||
|
> to_number("123") or else 0.0 == 123.0
|
||||||
|
|
||||||
--See also--
|
--See also--
|
||||||
| [[fun:to_int]] Convert any value to a [[type:int]]
|
| [[fun:to_int]] Convert any value to a [[type:int]]
|
||||||
| [[fun:to_number]] Convert any value to a number
|
| [[fun:to_number]] Convert any value to a number
|
||||||
|
|||||||
@@ -72,6 +72,7 @@ SCRIPT_FUNCTION(to_string) {
|
|||||||
SCRIPT_FUNCTION(to_int) {
|
SCRIPT_FUNCTION(to_int) {
|
||||||
ScriptValueP input = ctx.getVariable(SCRIPT_VAR_input);
|
ScriptValueP input = ctx.getVariable(SCRIPT_VAR_input);
|
||||||
ScriptType t = input->type();
|
ScriptType t = input->type();
|
||||||
|
try {
|
||||||
int result;
|
int result;
|
||||||
if (t == SCRIPT_BOOL) {
|
if (t == SCRIPT_BOOL) {
|
||||||
result = (bool)*input ? 1 : 0;
|
result = (bool)*input ? 1 : 0;
|
||||||
@@ -82,11 +83,18 @@ SCRIPT_FUNCTION(to_int) {
|
|||||||
result = (int)*input;
|
result = (int)*input;
|
||||||
}
|
}
|
||||||
SCRIPT_RETURN(result);
|
SCRIPT_RETURN(result);
|
||||||
|
} catch (const ScriptError& e) {
|
||||||
|
return new_intrusive1<ScriptDelayedError>(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SCRIPT_FUNCTION(to_real) {
|
SCRIPT_FUNCTION(to_real) {
|
||||||
|
try {
|
||||||
SCRIPT_PARAM_C(double, input);
|
SCRIPT_PARAM_C(double, input);
|
||||||
SCRIPT_RETURN(input);
|
SCRIPT_RETURN(input);
|
||||||
|
} catch (const ScriptError& e) {
|
||||||
|
return new_intrusive1<ScriptDelayedError>(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SCRIPT_FUNCTION(to_number) {
|
SCRIPT_FUNCTION(to_number) {
|
||||||
@@ -100,12 +108,21 @@ SCRIPT_FUNCTION(to_number) {
|
|||||||
} else if (t == SCRIPT_DOUBLE) {
|
} else if (t == SCRIPT_DOUBLE) {
|
||||||
SCRIPT_RETURN((double)*input);
|
SCRIPT_RETURN((double)*input);
|
||||||
} else {
|
} else {
|
||||||
SCRIPT_RETURN((int)*input);
|
String s = input->toString();
|
||||||
|
long l; double d;
|
||||||
|
if (s.ToLong(&l)) {
|
||||||
|
SCRIPT_RETURN((int)l);
|
||||||
|
} else if (s.ToDouble(&d)) {
|
||||||
|
SCRIPT_RETURN((double)d);
|
||||||
|
} else {
|
||||||
|
return delayError(_ERROR_2_("can't convert", input->typeName(), _TYPE_("double")));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SCRIPT_FUNCTION(to_boolean) {
|
SCRIPT_FUNCTION(to_boolean) {
|
||||||
ScriptValueP input = ctx.getVariable(SCRIPT_VAR_input);
|
ScriptValueP input = ctx.getVariable(SCRIPT_VAR_input);
|
||||||
|
try {
|
||||||
ScriptType t = input->type();
|
ScriptType t = input->type();
|
||||||
bool result;
|
bool result;
|
||||||
if (t == SCRIPT_INT) {
|
if (t == SCRIPT_INT) {
|
||||||
@@ -114,11 +131,18 @@ SCRIPT_FUNCTION(to_boolean) {
|
|||||||
result = (bool)*input;
|
result = (bool)*input;
|
||||||
}
|
}
|
||||||
SCRIPT_RETURN(result);
|
SCRIPT_RETURN(result);
|
||||||
|
} catch (const ScriptError& e) {
|
||||||
|
return new_intrusive1<ScriptDelayedError>(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SCRIPT_FUNCTION(to_color) {
|
SCRIPT_FUNCTION(to_color) {
|
||||||
|
try {
|
||||||
SCRIPT_PARAM_C(AColor, input);
|
SCRIPT_PARAM_C(AColor, input);
|
||||||
SCRIPT_RETURN(input);
|
SCRIPT_RETURN(input);
|
||||||
|
} catch (const ScriptError& e) {
|
||||||
|
return new_intrusive1<ScriptDelayedError>(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------- : Math
|
// ----------------------------------------------------------------------------- : Math
|
||||||
|
|||||||
@@ -8,8 +8,13 @@ $built_in_functions = array(
|
|||||||
'to_string' =>'',
|
'to_string' =>'',
|
||||||
'to_int' =>'',
|
'to_int' =>'',
|
||||||
'to_real' =>'',
|
'to_real' =>'',
|
||||||
|
'to_number' =>'',
|
||||||
'to_boolean' =>'',
|
'to_boolean' =>'',
|
||||||
'to_color' =>'',
|
'to_color' =>'',
|
||||||
|
// numbers
|
||||||
|
'abs' =>'',
|
||||||
|
'random_int' =>'',
|
||||||
|
'random_real' =>'',
|
||||||
// text
|
// text
|
||||||
'to_upper' =>'',
|
'to_upper' =>'',
|
||||||
'to_lower' =>'',
|
'to_lower' =>'',
|
||||||
@@ -36,6 +41,8 @@ $built_in_functions = array(
|
|||||||
'number_of_items' =>'',
|
'number_of_items' =>'',
|
||||||
'sort_list' =>'',
|
'sort_list' =>'',
|
||||||
'filter_list' =>'',
|
'filter_list' =>'',
|
||||||
|
'random_shuffle' =>'',
|
||||||
|
'random_select' =>'',
|
||||||
// keywords
|
// keywords
|
||||||
'expand_keywords' =>'', 'expand_keywords_rule'=>'expand_keywords',
|
'expand_keywords' =>'', 'expand_keywords_rule'=>'expand_keywords',
|
||||||
'keyword_usage' =>'',
|
'keyword_usage' =>'',
|
||||||
@@ -159,7 +166,7 @@ function highlight_script($code) {
|
|||||||
if ($matches[2] == '{') $string .= 's';
|
if ($matches[2] == '{') $string .= 's';
|
||||||
} else if (preg_match("@^\\#.*@",$code, $matches)) {
|
} else if (preg_match("@^\\#.*@",$code, $matches)) {
|
||||||
$ret .= "<span class='hl-comment'>" . $matches[0] . "</span>";
|
$ret .= "<span class='hl-comment'>" . $matches[0] . "</span>";
|
||||||
} else if (preg_match("@^([-+*/=!.\@]|<|>)+|^:=@",$code, $matches)) {
|
} else if (preg_match("@^([-+*/=!.\@^]|<|>)+|^:=@",$code, $matches)) {
|
||||||
$ret .= "<span class='hl-op'>" . $matches[0] . "</span>";
|
$ret .= "<span class='hl-op'>" . $matches[0] . "</span>";
|
||||||
} else if (preg_match("@^([}]|[\\(\\)\\[\\]{,]+)@",$code, $matches)) {
|
} else if (preg_match("@^([}]|[\\(\\)\\[\\]{,]+)@",$code, $matches)) {
|
||||||
$ret .= "<span class='hl-paren'>" . $matches[0] . "</span>";
|
$ret .= "<span class='hl-paren'>" . $matches[0] . "</span>";
|
||||||
|
|||||||
Reference in New Issue
Block a user