From 53808346a4a5c73f72cf0bb1fee8a7e11ed47d6e Mon Sep 17 00:00:00 2001 From: twanvl Date: Wed, 9 Jul 2008 17:14:49 +0000 Subject: [PATCH] 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 --- doc/function/to_int.txt | 4 ++ doc/function/to_number.txt | 4 ++ doc/function/to_real.txt | 4 ++ src/script/functions/basic.cpp | 66 +++++++++++++------ .../drupal/mse-drupal-modules/highlight.inc | 9 ++- 5 files changed, 65 insertions(+), 22 deletions(-) diff --git a/doc/function/to_int.txt b/doc/function/to_int.txt index 31eed073..0956ee5c 100644 --- a/doc/function/to_int.txt +++ b/doc/function/to_int.txt @@ -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 diff --git a/doc/function/to_number.txt b/doc/function/to_number.txt index 29041b2f..7b48a9b6 100644 --- a/doc/function/to_number.txt +++ b/doc/function/to_number.txt @@ -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]] diff --git a/doc/function/to_real.txt b/doc/function/to_real.txt index c8cdf3f5..7b3a74fb 100644 --- a/doc/function/to_real.txt +++ b/doc/function/to_real.txt @@ -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 diff --git a/src/script/functions/basic.cpp b/src/script/functions/basic.cpp index 2a1df29a..eeb60608 100644 --- a/src/script/functions/basic.cpp +++ b/src/script/functions/basic.cpp @@ -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(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(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(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(e); + } } // ----------------------------------------------------------------------------- : Math diff --git a/tools/website/drupal/mse-drupal-modules/highlight.inc b/tools/website/drupal/mse-drupal-modules/highlight.inc index d8ea4ef3..6676380b 100644 --- a/tools/website/drupal/mse-drupal-modules/highlight.inc +++ b/tools/website/drupal/mse-drupal-modules/highlight.inc @@ -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 .= "" . $matches[0] . ""; - } else if (preg_match("@^([-+*/=!.\@]|<|>)+|^:=@",$code, $matches)) { + } else if (preg_match("@^([-+*/=!.\@^]|<|>)+|^:=@",$code, $matches)) { $ret .= "" . $matches[0] . ""; } else if (preg_match("@^([}]|[\\(\\)\\[\\]{,]+)@",$code, $matches)) { $ret .= "" . $matches[0] . "";