From 1a75fb05c927da708f1d1d11f4e66d97eafa8366 Mon Sep 17 00:00:00 2001 From: Florian Stinglmayr Date: Thu, 22 Feb 2018 13:32:59 +0100 Subject: [PATCH] change API of expression to be consistent with dice API --- README.md | 12 ++++++++---- lib/dice.h | 6 ++++-- lib/diceexpr.c | 25 ++++++++++++++++++------- tests/test_expr_parse.c | 30 ++++++++++++++---------------- 4 files changed, 44 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 5d22c7a..d6f2d86 100644 --- a/README.md +++ b/README.md @@ -109,20 +109,24 @@ dice_free(d20); The expression API supports multiple die within one mathematical expression. Expressions are parsed using [tinyexpr](https://github.com/codeplea/tinyexpr) -library. +library. The API works with objects of the type ``dice_expression_t``, which +are allocated using ``dice_expression_new``, and must be freed by using +``dice_expression_free``. Expressions in the format of a C string, can be +parsed by using ``dice_expression_parse``, and a result can be evaluated +by using ``dice_expression_roll``. ```C int error = 0; char const *expr = "1d20+8+4+3-1"; -dice_expression_t e = dice_expression_parse(expr, &err); +dice_expression_t e = dice_expression_new(); int64_t result = 0; -if (e == NULL) { +if (!dice_expression_parse(e, expr, &err)) { fprintf(stderr, "error in expression: \"%s\": at %d\n", expr, err); goto fail; } -if (!dice_expression_evaluate(e, &result)) { +if (!dice_expression_roll(e, &result)) { goto fail; } diff --git a/lib/dice.h b/lib/dice.h index 94360be..ae73ac9 100644 --- a/lib/dice.h +++ b/lib/dice.h @@ -55,9 +55,11 @@ bool dice_get(dice_t d, dice_option_t opt, ...); int64_t dice_roll(dice_t d); bool dice_evaluate(dice_t d, dice_result_t **res, size_t *reslen); -dice_expression_t dice_expression_parse(char const *s, int *error); +dice_expression_t dice_expression_new(void); void dice_expression_free(dice_expression_t e); -bool dice_expression_evaluate(dice_expression_t e, int64_t *result); + +bool dice_expression_parse(dice_expression_t d, char const *s, int *error); +bool dice_expression_roll(dice_expression_t e, int64_t *result); bool dice_expression_print(dice_expression_t e); #endif diff --git a/lib/diceexpr.c b/lib/diceexpr.c index 22f5315..6bc7e1f 100644 --- a/lib/diceexpr.c +++ b/lib/diceexpr.c @@ -729,7 +729,7 @@ void dice_expression_free(dice_expression_t e) free(e); } -dice_expression_t dice_expression_parse(char const *n, int *error) +dice_expression_t dice_expression_new(void) { dice_expression_t e = NULL; @@ -738,16 +738,27 @@ dice_expression_t dice_expression_parse(char const *n, int *error) return NULL; } - e->expr = te_compile(n, 0, 0, error); - if (e->expr == NULL) { - free(e); - return NULL; - } + e->expr = NULL; return e; } -bool dice_expression_evaluate(dice_expression_t e, int64_t *result) +bool dice_expression_parse(dice_expression_t e, char const *n, int *error) +{ + if (e->expr != NULL) { + te_free(e->expr); + e->expr = NULL; + } + + e->expr = te_compile(n, 0, 0, error); + if (e->expr == NULL) { + return false; + } + + return true; +} + +bool dice_expression_roll(dice_expression_t e, int64_t *result) { double val; if (e == NULL || e->expr == NULL) { diff --git a/tests/test_expr_parse.c b/tests/test_expr_parse.c index 4204b8f..15e0194 100644 --- a/tests/test_expr_parse.c +++ b/tests/test_expr_parse.c @@ -27,28 +27,28 @@ static void test_expr_parse_none(void **arg) { - dice_expression_t e = NULL; + dice_expression_t e = dice_expression_new(); int error = 0; - e = dice_expression_parse("", &error); - - assert_null(e); + assert_non_null(e); + assert_false(dice_expression_parse(e, "", &error)); assert_int_equal(error, 1); + + dice_expression_free(e); } static void test_expr_parse_simple(void **arg) { - dice_expression_t e = NULL; + dice_expression_t e = dice_expression_new(); int error = 0, i = 0; int64_t result = 0; - e = dice_expression_parse("4d6", &error); - assert_non_null(e); + assert_true(dice_expression_parse(e, "4d6", &error)); assert_int_equal(error, 0); for (i = 0; i < 100000; i++) { - assert_true(dice_expression_evaluate(e, &result)); + assert_true(dice_expression_roll(e, &result)); assert_true(result >= 4 && result <= 32); } @@ -57,17 +57,16 @@ static void test_expr_parse_simple(void **arg) static void test_expr_parse_modifier(void **arg) { - dice_expression_t e = NULL; + dice_expression_t e = dice_expression_new(); int error = 0, i = 0; int64_t result = 0; - e = dice_expression_parse("1d20+3+5", &error); - assert_non_null(e); + assert_true(dice_expression_parse(e, "1d20+3+5", &error)); assert_int_equal(error, 0); for (i = 0; i < 100000; i++) { - assert_true(dice_expression_evaluate(e, &result)); + assert_true(dice_expression_roll(e, &result)); assert_true(result >= 9 && result <= 28); } @@ -76,17 +75,16 @@ static void test_expr_parse_modifier(void **arg) static void test_expr_parse_complex(void **arg) { - dice_expression_t e = NULL; + dice_expression_t e = dice_expression_new(); int error = 0, i = 0; int64_t result = 0; - e = dice_expression_parse("1d8+(3+5)+1d6+5d4", &error); - assert_non_null(e); + assert_true(dice_expression_parse(e, "1d8+(3+5)+1d6+5d4", &error)); assert_int_equal(error, 0); for (i = 0; i < 100000; i++) { - assert_true(dice_expression_evaluate(e, &result)); + assert_true(dice_expression_roll(e, &result)); assert_true(result >= 15 && result <= 42); }