summaryrefslogtreecommitdiff
path: root/src/formula.h
diff options
context:
space:
mode:
authorAnders Betts <anders.betts@gmail.com>2026-09-17 19:55:36 +0200
committerAnders Betts <anders.betts@gmail.com>2026-09-17 19:55:36 +0200
commit380195f7cd5e57acf2c1cf2bc41069e6b0b979ed (patch)
tree32a88fb22a7fbe8f1fd5c105156d1f928c93950d /src/formula.h
downloadbokf-0.1.0.tar.gz
bokf-0.1.0.zip
Initial commit: daemon, clients, docs, Docker deploy pipelinev0.1.0
Diffstat (limited to 'src/formula.h')
-rw-r--r--src/formula.h43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/formula.h b/src/formula.h
new file mode 100644
index 0000000..f8476d0
--- /dev/null
+++ b/src/formula.h
@@ -0,0 +1,43 @@
+#ifndef BOKF_FORMULA_H
+#define BOKF_FORMULA_H
+
+#include <stddef.h>
+#include <stdint.h>
+
+/* Tiny, deterministic expression evaluator for voucher templates.
+ Grammar: expr := term (('+'|'-') term)*
+ term := factor (('*'|'/') factor)*
+ factor := ('+'|'-') factor | primary
+ primary := number | 'x' | '(' expr ')'
+ Values are kronor as double; x is the template variable in kronor. */
+
+struct template_row {
+ const char *account; /* account number */
+ const char *formula;
+ const char *description; /* optional */
+};
+
+struct resolved_row {
+ char account[16];
+ int64_t debit_ore;
+ int64_t credit_ore;
+ char description[128];
+};
+
+/* Returns 0 on success and stores the value in *out_kr. On error returns
+ -1 and writes a message to errbuf. */
+int formula_eval(const char *formula, double x, double *out_kr, char *errbuf,
+ size_t errlen);
+
+/* Evaluates all rows with x, rounds to whole öre, drops zero rows, and
+ assigns the rounding remainder to the largest row so debit equals credit.
+ Returns 0 on success and sets *out_n. Errors (bad formula, too few
+ rows) are reported in errbuf. */
+int formula_resolve_rows(const struct template_row *rows, size_t nrows,
+ double x, struct resolved_row *out, size_t *out_n,
+ char *errbuf, size_t errlen);
+
+/* True when the formula parses (evaluated with x = 1). */
+int formula_valid(const char *formula);
+
+#endif