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]].
> to_int(true) == 1
> 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--
| [[fun:to_real]] Convert any value to a [[type:double]]
| [[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.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--
| [[fun:to_int]] Convert any value to a [[type:int]]
| [[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.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--
| [[fun:to_int]] Convert any value to a [[type:int]]
| [[fun:to_number]] Convert any value to a number
......@@ -72,21 +72,29 @@ SCRIPT_FUNCTION(to_string) {
SCRIPT_FUNCTION(to_int) {
ScriptValueP input = ctx.getVariable(SCRIPT_VAR_input);
ScriptType t = input->type();
int result;
if (t == SCRIPT_BOOL) {
result = (bool)*input ? 1 : 0;
} else if (t == SCRIPT_COLOR) {
AColor c = (AColor)*input;
result = (c.Red() + c.Blue() + c.Green()) / 3;
} else {
result = (int)*input;
try {
int result;
if (t == SCRIPT_BOOL) {
result = (bool)*input ? 1 : 0;
} else if (t == SCRIPT_COLOR) {
AColor c = (AColor)*input;
result = (c.Red() + c.Blue() + c.Green()) / 3;
} else {
result = (int)*input;
}
SCRIPT_RETURN(result);
} catch (const ScriptError& e) {
return new_intrusive1<ScriptDelayedError>(e);
}
SCRIPT_RETURN(result);
}
SCRIPT_FUNCTION(to_real) {
SCRIPT_PARAM_C(double, input);
SCRIPT_RETURN(input);
try {
SCRIPT_PARAM_C(double, input);
SCRIPT_RETURN(input);
} catch (const ScriptError& e) {
return new_intrusive1<ScriptDelayedError>(e);
}
}
SCRIPT_FUNCTION(to_number) {
......@@ -100,25 +108,41 @@ SCRIPT_FUNCTION(to_number) {
} else if (t == SCRIPT_DOUBLE) {
SCRIPT_RETURN((double)*input);
} 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) {
ScriptValueP input = ctx.getVariable(SCRIPT_VAR_input);
ScriptType t = input->type();
bool result;
if (t == SCRIPT_INT) {
result = (int)*input != 0;
} else {
result = (bool)*input;
try {
ScriptType t = input->type();
bool result;
if (t == SCRIPT_INT) {
result = (int)*input != 0;
} else {
result = (bool)*input;
}
SCRIPT_RETURN(result);
} catch (const ScriptError& e) {
return new_intrusive1<ScriptDelayedError>(e);
}
SCRIPT_RETURN(result);
}
SCRIPT_FUNCTION(to_color) {
SCRIPT_PARAM_C(AColor, input);
SCRIPT_RETURN(input);
try {
SCRIPT_PARAM_C(AColor, input);
SCRIPT_RETURN(input);
} catch (const ScriptError& e) {
return new_intrusive1<ScriptDelayedError>(e);
}
}
// ----------------------------------------------------------------------------- : Math
......
......@@ -8,8 +8,13 @@ $built_in_functions = array(
'to_string' =>'',
'to_int' =>'',
'to_real' =>'',
'to_number' =>'',
'to_boolean' =>'',
'to_color' =>'',
// numbers
'abs' =>'',
'random_int' =>'',
'random_real' =>'',
// text
'to_upper' =>'',
'to_lower' =>'',
......@@ -36,6 +41,8 @@ $built_in_functions = array(
'number_of_items' =>'',
'sort_list' =>'',
'filter_list' =>'',
'random_shuffle' =>'',
'random_select' =>'',
// keywords
'expand_keywords' =>'', 'expand_keywords_rule'=>'expand_keywords',
'keyword_usage' =>'',
......@@ -159,7 +166,7 @@ function highlight_script($code) {
if ($matches[2] == '{') $string .= 's';
} else if (preg_match("@^\\#.*@",$code, $matches)) {
$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>";
} else if (preg_match("@^([}]|[\\(\\)\\[\\]{,]+)@",$code, $matches)) {
$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