Commit 9354ddf9 authored by twanvl's avatar twanvl

Allow "or else" construct to be used for error recovery

parent 5aae5075
...@@ -21,6 +21,10 @@ Convert any value to a [[type:int]]. ...@@ -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]]. ...@@ -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]]. ...@@ -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,21 +72,29 @@ SCRIPT_FUNCTION(to_string) { ...@@ -72,21 +72,29 @@ 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();
int result; try {
if (t == SCRIPT_BOOL) { int result;
result = (bool)*input ? 1 : 0; if (t == SCRIPT_BOOL) {
} else if (t == SCRIPT_COLOR) { result = (bool)*input ? 1 : 0;
AColor c = (AColor)*input; } else if (t == SCRIPT_COLOR) {
result = (c.Red() + c.Blue() + c.Green()) / 3; AColor c = (AColor)*input;
} else { result = (c.Red() + c.Blue() + c.Green()) / 3;
result = (int)*input; } else {
result = (int)*input;
}
SCRIPT_RETURN(result);
} catch (const ScriptError& e) {
return new_intrusive1<ScriptDelayedError>(e);
} }
SCRIPT_RETURN(result);
} }
SCRIPT_FUNCTION(to_real) { SCRIPT_FUNCTION(to_real) {
SCRIPT_PARAM_C(double, input); try {
SCRIPT_RETURN(input); SCRIPT_PARAM_C(double, input);
SCRIPT_RETURN(input);
} catch (const ScriptError& e) {
return new_intrusive1<ScriptDelayedError>(e);
}
} }
SCRIPT_FUNCTION(to_number) { SCRIPT_FUNCTION(to_number) {
...@@ -100,25 +108,41 @@ SCRIPT_FUNCTION(to_number) { ...@@ -100,25 +108,41 @@ 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);
ScriptType t = input->type(); try {
bool result; ScriptType t = input->type();
if (t == SCRIPT_INT) { bool result;
result = (int)*input != 0; if (t == SCRIPT_INT) {
} else { result = (int)*input != 0;
result = (bool)*input; } else {
result = (bool)*input;
}
SCRIPT_RETURN(result);
} catch (const ScriptError& e) {
return new_intrusive1<ScriptDelayedError>(e);
} }
SCRIPT_RETURN(result);
} }
SCRIPT_FUNCTION(to_color) { SCRIPT_FUNCTION(to_color) {
SCRIPT_PARAM_C(AColor, input); try {
SCRIPT_RETURN(input); SCRIPT_PARAM_C(AColor, input);
SCRIPT_RETURN(input);
} catch (const ScriptError& e) {
return new_intrusive1<ScriptDelayedError>(e);
}
} }
// ----------------------------------------------------------------------------- : Math // ----------------------------------------------------------------------------- : Math
......
...@@ -8,8 +8,13 @@ $built_in_functions = array( ...@@ -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( ...@@ -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) { ...@@ -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("@^([-+*/=!.\@]|&lt;|&gt;)+|^:=@",$code, $matches)) { } else if (preg_match("@^([-+*/=!.\@^]|&lt;|&gt;)+|^:=@",$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>";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment