aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/expressions.c
diff options
context:
space:
mode:
authorRobin Haberkorn <robin.haberkorn@googlemail.com>2025-07-26 16:30:17 +0300
committerRobin Haberkorn <robin.haberkorn@googlemail.com>2025-07-26 16:48:56 +0300
commit0ea082b74414696a7800455a437656fca2886f6d (patch)
tree63d4809b97c9cbdeb2a2063025fd349af4cd5043 /src/expressions.c
parenteb6f7a82045ad78553fca98c54a51366c55bd7a4 (diff)
downloadsciteco-0ea082b74414696a7800455a437656fca2886f6d.tar.gz
properly document some functions in expressions.c and simplified code
* Practically all calls to teco_expressions_args() must be preceded by teco_expressions_eval(). * In code paths where we know that teco_expressions_args() > 0, it is safe to call teco_expressions_pop_num(0) instead of teco_expressions_pop_num_calc(). This is both easier and faster. * teco_expressions_pop_num_calc() is for simple applications where you just want to get a command argument with default (implied) values. Since it includes teco_expressions_eval(), we can avoid superfluous calls. * -EC...$ turned out to be broken and is fixed now. A test case has been added.
Diffstat (limited to 'src/expressions.c')
-rw-r--r--src/expressions.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/expressions.c b/src/expressions.c
index ede54d2..2fbb659 100644
--- a/src/expressions.c
+++ b/src/expressions.c
@@ -76,12 +76,22 @@ teco_expressions_push_int(teco_int_t number)
undo__remove_index__teco_numbers(teco_numbers->len-1);
}
+/** Peek into the numbers stack */
teco_int_t
teco_expressions_peek_num(guint index)
{
return g_array_index(teco_numbers, teco_int_t, teco_numbers->len - 1 - index);
}
+/**
+ * Pop a value from the number stack.
+ *
+ * This must only be called if you are sure that the number at the
+ * given index exists and is an argument, i.e. only after calling
+ * teco_expressions_eval() and teco_expressions_args().
+ * If you are unsure or want to imply a value,
+ * use teco_expressions_pop_num_calc() instead.
+ */
teco_int_t
teco_expressions_pop_num(guint index)
{
@@ -99,6 +109,17 @@ teco_expressions_pop_num(guint index)
return n;
}
+/**
+ * Pop an argument from the number stack.
+ *
+ * This resolves operations and allows implying values
+ * if there isn't any argument on the stack.
+ *
+ * @param ret Where to store the number
+ * @param imply The fallback value if there is no argument
+ * @param error A GError
+ * @return FALSE if an error occurred
+ */
gboolean
teco_expressions_pop_num_calc(teco_int_t *ret, teco_int_t imply, GError **error)
{
@@ -263,6 +284,13 @@ teco_expressions_calc(GError **error)
return TRUE;
}
+/**
+ * Resolve all operations on the top of the stack.
+ *
+ * @param pop_brace If TRUE this also pops the "brace" operator.
+ * @param error A GError
+ * @return FALSE if an error occurred
+ */
gboolean
teco_expressions_eval(gboolean pop_brace, GError **error)
{
@@ -287,6 +315,14 @@ teco_expressions_eval(gboolean pop_brace, GError **error)
return TRUE;
}
+/**
+ * Get number of numeric arguments on the top of the stack.
+ *
+ * @fixme You must call teco_expressions_eval() to resolve operations
+ * before this gives sensitive results.
+ * Overall it might be better to automatically call teco_expressions_eval()
+ * here or introduce a separate teco_expressions_args_calc().
+ */
guint
teco_expressions_args(void)
{