From 380195f7cd5e57acf2c1cf2bc41069e6b0b979ed Mon Sep 17 00:00:00 2001 From: Anders Betts Date: Thu, 17 Sep 2026 19:55:36 +0200 Subject: Initial commit: daemon, clients, docs, Docker deploy pipeline --- src/formula.h | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/formula.h (limited to 'src/formula.h') 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 +#include + +/* 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 -- cgit v1.3