[template-glib] expr: implement abs() builtin
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [template-glib] expr: implement abs() builtin
- Date: Wed, 4 May 2022 21:37:28 +0000 (UTC)
commit 58275f7204d56680f06fd09f88af87e23d4dd7c3
Author: Christian Hergert <chergert redhat com>
Date: Wed May 4 14:37:16 2022 -0700
expr: implement abs() builtin
And test it in test1.script
src/tmpl-expr-eval.c | 39 ++++++++++++++++++++++++++++++++++++++-
src/tmpl-expr-scanner.l | 1 +
tests/test1.script | 14 +++++++++++++-
3 files changed, 52 insertions(+), 2 deletions(-)
---
diff --git a/src/tmpl-expr-eval.c b/src/tmpl-expr-eval.c
index c7d3cc5..9927416 100644
--- a/src/tmpl-expr-eval.c
+++ b/src/tmpl-expr-eval.c
@@ -1673,7 +1673,44 @@ builtin_abs (const GValue *value,
GValue *return_value,
GError **error)
{
- throw_type_mismatch (error, value, NULL, "not implemented");
+ char *errmsg;
+
+ if (G_VALUE_HOLDS_DOUBLE (value))
+ {
+ g_value_init (return_value, G_TYPE_DOUBLE);
+ g_value_set_double (return_value, ABS (g_value_get_double (value)));
+ return TRUE;
+ }
+ else if (G_VALUE_HOLDS_FLOAT (value))
+ {
+ g_value_init (return_value, G_TYPE_FLOAT);
+ g_value_set_float (return_value, ABS (g_value_get_float (value)));
+ return TRUE;
+ }
+ else if (G_VALUE_HOLDS_INT (value))
+ {
+ g_value_init (return_value, G_TYPE_INT);
+ g_value_set_int (return_value, ABS (g_value_get_int (value)));
+ return TRUE;
+ }
+ else if (G_VALUE_HOLDS_INT64 (value))
+ {
+ g_value_init (return_value, G_TYPE_INT64);
+ g_value_set_int64 (return_value, ABS (g_value_get_int64 (value)));
+ return TRUE;
+ }
+ else if (G_VALUE_HOLDS_UINT (value) ||
+ G_VALUE_HOLDS_UINT64 (value) ||
+ G_VALUE_HOLDS_UCHAR (value))
+ {
+ *return_value = *value;
+ return TRUE;
+ }
+
+ errmsg = g_strdup_printf ("Cannot abs() type %s", G_VALUE_TYPE_NAME (value));
+ throw_type_mismatch (error, value, NULL, errmsg);
+ g_free (errmsg);
+
return FALSE;
}
diff --git a/src/tmpl-expr-scanner.l b/src/tmpl-expr-scanner.l
index c4580f5..0a787d4 100644
--- a/src/tmpl-expr-scanner.l
+++ b/src/tmpl-expr-scanner.l
@@ -82,6 +82,7 @@ TmplExprParser *parser = yyextra;
/* builtin functions */
"assert" { yylval->fn = TMPL_EXPR_BUILTIN_ASSERT; return BUILTIN; }
+"abs" { yylval->fn = TMPL_EXPR_BUILTIN_ABS; return BUILTIN; }
"ceil" { yylval->fn = TMPL_EXPR_BUILTIN_CEIL; return BUILTIN; }
"floor" { yylval->fn = TMPL_EXPR_BUILTIN_FLOOR; return BUILTIN; }
"hex" { yylval->fn = TMPL_EXPR_BUILTIN_HEX; return BUILTIN; }
diff --git a/tests/test1.script b/tests/test1.script
index 54fda20..46e90da 100644
--- a/tests/test1.script
+++ b/tests/test1.script
@@ -5,6 +5,18 @@ assert(true)
assert(true != false)
assert(!false)
+assert(123 == 123)
+assert(123 != 124)
+assert(123 != -123)
+assert(123 == abs(-123))
+
+abc = "abc"
+assert(abc == "abc")
+assert(abc != "abc\n")
+assert(abc != "def")
+
func multiply(x,y) = x * y;
+assert(multiply(10, 10) == 100)
+assert(multiply(-10, 10) == -100)
-multiply(617, 2)
+1234;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]