first version of libdice
This commit is contained in:
178
lib/dice.c
Normal file
178
lib/dice.c
Normal file
@@ -0,0 +1,178 @@
|
||||
#include "dice.h"
|
||||
#include "dice_parse.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifndef HAVE_ARC4RANDOM_UNIFORM
|
||||
#include <bsd/stdlib.h>
|
||||
#endif
|
||||
|
||||
extern void *yy_scan_string(char const *s);
|
||||
extern void yy_delete_buffer(void *b);
|
||||
|
||||
struct dice_
|
||||
{
|
||||
uint32_t amount;
|
||||
uint32_t sides;
|
||||
|
||||
char *errmsg;
|
||||
};
|
||||
|
||||
static dice_t dice_new(void)
|
||||
{
|
||||
dice_t tmp = calloc(1, sizeof(struct dice_));
|
||||
|
||||
if (tmp == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
void dice_free(dice_t t)
|
||||
{
|
||||
if (t == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
free(t);
|
||||
}
|
||||
|
||||
void dice_result_free(dice_result_t *r)
|
||||
{
|
||||
if (r == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
free(r);
|
||||
}
|
||||
|
||||
void dice_result_freev(dice_result_t *r, size_t len)
|
||||
{
|
||||
if (r == NULL || len == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
free(r);
|
||||
}
|
||||
|
||||
dice_t dice_simple(uint32_t amount, uint32_t sides)
|
||||
{
|
||||
dice_t tmp = dice_new();
|
||||
|
||||
if (amount == 0 || sides == 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (tmp == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
tmp->amount = amount;
|
||||
tmp->sides = sides;
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
dice_t dice_parse(char const *s)
|
||||
{
|
||||
dice_t d = NULL;
|
||||
void *buffer = NULL;
|
||||
int ret = 0;
|
||||
|
||||
d = dice_new();
|
||||
if (d == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
buffer = yy_scan_string(s);
|
||||
ret = yyparse(d);
|
||||
yy_delete_buffer(buffer);
|
||||
|
||||
if (ret) {
|
||||
dice_free(d);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return d;
|
||||
}
|
||||
|
||||
bool dice_set(dice_t d, dice_option_t opt, ...)
|
||||
{
|
||||
va_list lst;
|
||||
|
||||
va_start(lst, opt);
|
||||
switch (opt) {
|
||||
case DICEOPTION_AMOUNT: d->amount = va_arg(lst, uint32_t); break;
|
||||
case DICEOPTION_SIDES: d->sides = va_arg(lst, uint32_t); break;
|
||||
|
||||
default: return false;
|
||||
}
|
||||
va_end(lst);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool dice_get(dice_t d, dice_option_t opt, ...)
|
||||
{
|
||||
va_list lst;
|
||||
|
||||
va_start(lst, opt);
|
||||
switch (opt) {
|
||||
case DICEOPTION_AMOUNT:
|
||||
{
|
||||
uint32_t *ptr = va_arg(lst, uint32_t*);
|
||||
*ptr = d->amount;
|
||||
} break;
|
||||
|
||||
case DICEOPTION_SIDES:
|
||||
{
|
||||
uint32_t *ptr = va_arg(lst, uint32_t*);
|
||||
*ptr = d->sides;
|
||||
} break;
|
||||
|
||||
default: return false;
|
||||
}
|
||||
va_end(lst);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int64_t dice_roll(dice_t d)
|
||||
{
|
||||
int64_t result = 0;
|
||||
uint32_t i = 0;
|
||||
|
||||
for (i = 0; i < d->amount; i++) {
|
||||
result += arc4random_uniform(d->sides) + 1;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool dice_evaluate(dice_t d, dice_result_t **res, size_t *reslen)
|
||||
{
|
||||
size_t len = 0;
|
||||
dice_result_t *r = NULL;
|
||||
uint32_t i = 0;
|
||||
|
||||
if (d == NULL || d->amount <= 0 || d->sides <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
len = d->amount;
|
||||
r = calloc(len, sizeof(dice_result_t));
|
||||
if (r == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (i = 0; i < d->amount; i++) {
|
||||
r[i].result = arc4random_uniform(d->sides) + 1;
|
||||
}
|
||||
|
||||
*reslen = len;
|
||||
*res = r;
|
||||
|
||||
return true;
|
||||
}
|
||||
34
lib/dice.h
Normal file
34
lib/dice.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#ifndef LIBDICE_DICE_H
|
||||
#define LIBDICE_DICE_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
|
||||
struct dice_;
|
||||
typedef struct dice_ * dice_t;
|
||||
|
||||
typedef struct {
|
||||
int64_t result;
|
||||
} dice_result_t;
|
||||
|
||||
typedef enum {
|
||||
DICEOPTION_AMOUNT = 0,
|
||||
DICEOPTION_SIDES,
|
||||
} dice_option_t;
|
||||
|
||||
void dice_result_free(dice_result_t *r);
|
||||
void dice_result_freev(dice_result_t *r, size_t len);
|
||||
|
||||
void dice_free(dice_t t);
|
||||
dice_t dice_simple(uint32_t amount, uint32_t sides);
|
||||
dice_t dice_parse(char const *s);
|
||||
|
||||
bool dice_set(dice_t d, dice_option_t opt, ...);
|
||||
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);
|
||||
|
||||
#endif
|
||||
9
lib/dice_lexer.l
Normal file
9
lib/dice_lexer.l
Normal file
@@ -0,0 +1,9 @@
|
||||
%{
|
||||
#include "dice.h"
|
||||
#include "dice_parse.h"
|
||||
%}
|
||||
|
||||
%%
|
||||
[0-9]+ { yylval.integer = atoi(yytext); return TOK_INTEGER; }
|
||||
[d] return TOK_DICESEP;
|
||||
%%
|
||||
39
lib/dice_parse.y
Normal file
39
lib/dice_parse.y
Normal file
@@ -0,0 +1,39 @@
|
||||
%parse-param {dice_t dice}
|
||||
|
||||
%{
|
||||
#include "dice.h"
|
||||
|
||||
extern int yylex(void);
|
||||
|
||||
void yyerror(dice_t dice, char const *err)
|
||||
{
|
||||
}
|
||||
|
||||
int yywrap(void)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
%}
|
||||
|
||||
%union {
|
||||
char *str;
|
||||
int integer;
|
||||
double number;
|
||||
}
|
||||
|
||||
%token TOK_DICESEP
|
||||
%token <integer> TOK_INTEGER
|
||||
|
||||
%%
|
||||
|
||||
dice: TOK_INTEGER TOK_DICESEP TOK_INTEGER
|
||||
{
|
||||
dice_set(dice, DICEOPTION_AMOUNT, $1);
|
||||
dice_set(dice, DICEOPTION_SIDES, $3);
|
||||
}
|
||||
| TOK_DICESEP TOK_INTEGER
|
||||
{
|
||||
dice_set(dice, DICEOPTION_AMOUNT, 1);
|
||||
dice_set(dice, DICEOPTION_SIDES, $2);
|
||||
}
|
||||
;
|
||||
Reference in New Issue
Block a user