change API of expression to be consistent with dice API
This commit is contained in:
parent
320986d0d5
commit
1a75fb05c9
12
README.md
12
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;
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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) {
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user