| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
 | #ifndef LICHEN_EXPRESSION_H
#define LICHEN_EXPRESSION_H
#include <stdint.h>
/****************************************************************
 * 
 * a grammar is built on both nonterminals and terminals. both are
 * represented by integers; nonterminals must be less than MAX_NONTERMINAL
 * and terminals must be greater. the intended way to do this is to define
 * an enum with all symbols, and have a MAX_NONTERMINAL symbol dividing the
 * (low) nonterminals from the (high) terminals.
 *
 ****************************************************************
 */
struct li_production_t {
  int *seq;
  size_t seq_len;
  struct li_production_t *next;  
};
struct li_production_t * li_production_alloc(size_t seq_len);
void li_production_free(struct li_production_t *prod);
struct li_production_rule_t {
  size_t num_productions;
  struct li_production_t *head;
};
struct li_production_rule_t * li_prod_rule_alloc();
void li_prod_rule_free(struct li_production_rule_t *rule);
int li_prod_rule_add(struct li_production_rule_t *rule, size_t seq_len, int *seq);
typedef struct li_grammar_t {
  struct li_production_rule_t *rules;
  int num_nonterminal;
} li_grammar_t;
li_grammar_t * li_grammar_alloc(int num_nonterminal);
void li_grammar_free(li_grammar_t *grammar);
int li_grammar_rule_add(li_grammar_t *grammar, int nonterminal, size_t seq_len, int *seq);
// expand a symbol into only nonterminals
// dst is a buffer to place the expansion into, and len should initially contain the size of the buffer
// stack is a buffer used internally to expand the rule, and stack_sz is the size of the stack
// symbol is the symbol to start with
// randomness is a sequence of random values, from any source, and rand_sz is the length of the sequence
// if successful, this function will return 0 and dst will contain the expanded sequence, with len containing
// the length of the actual expansion
// if unsuccessful, this function will return nonzero. dst, len, and stack may be modified in unpredictable ways.
int li_grammar_rule_expand(
  li_grammar_t *grammar, 
  int *dst, size_t *len, 
  int *stack, size_t stack_sz, 
  int symbol, 
  const uint8_t *randomness, size_t rand_sz
);
#endif
 |