aboutsummaryrefslogtreecommitdiff
path: root/include/stem.h
blob: e8a858764ff6a42f292d186b4f4ea43dd60022b3 (plain)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#ifndef PARSER_H_
#define PARSER_H_
#include <better_string.h>
#include <stdbool.h>
#include <stdlib.h>

typedef struct VALUE_STRUCT value_t;
typedef struct ARRAY_STRUCT array_t;

struct ARRAY_STRUCT {
  value_t **items;
  size_t size;
  size_t capacity;
};

struct VALUE_STRUCT {
  enum { VWORD, VINT, VFLOAT, VSTR, VQUOTE, VERR, VCUSTOM } type;
  union {
    long double int_float;
    array_t *quote;
    string_t *str_word;
  };
  void *custom;
  bool escaped;
};

typedef struct PARSER_STRUCT {
  char *source;
  int i;
  char c;
} parser_t;

typedef struct NODE_STRUCT {
  string_t *key;
  void *value;
  struct NODE_STRUCT *next;
} node_t;

typedef struct {
  node_t *head;
  size_t size;
} sll_t;

typedef struct {
  sll_t **buckets;
  size_t size;
} ht_t;

typedef struct {
  void (*printfunc)(void *);
  void *(*copyfunc)(void *);
  void (*freefunc)(void *);
} custom_t;

void func_free(void *f);

array_t *init_array(size_t size);

void array_append(array_t *a, value_t *v);

value_t *array_pop(array_t *a);

array_t *array_copy(array_t *a);

void array_extend(array_t *a1, array_t *a2);

void array_free(array_t *a);

value_t *init_value(int type);

value_t *value_copy(value_t *v);

void value_free(void *v);

custom_t *init_custom(void (*)(void *), void (*)(void *), void *(*)(void *));

void custom_free(void *);

void add_func(ht_t *h, void (*func)(value_t *), char *key);

void add_obj(ht_t *h, ht_t *h2, void (*printfunc)(void *),
             void (*freefunc)(void *), void *(*copyfunc)(void *),
             void (*createfunc)(void *), char *key);

parser_t *init_parser(char *source);

void parser_reset(parser_t *p, char *source);

void parser_move(parser_t *p);

void parser_skip_whitespace(parser_t *p);

value_t *parse_string(parser_t *p);

value_t *parse_quote(parser_t *p);

value_t *parse_word(parser_t *p);

value_t *parse_num(parser_t *p);

void parser_error(parser_t *p);

value_t *parser_get_next(parser_t *p);

node_t *init_node(string_t *key, void *v);

void node_free(node_t *n, void (*freefunc)(void *));

sll_t *init_sll();

void sll_add(sll_t *l, string_t *key, void *v, void (*freefunc)(void *));

void *sll_get(sll_t *l, string_t *key);

void sll_free(sll_t *l, void (*freefunc)(void *));

ht_t *init_ht(size_t size);

void ht_add(ht_t *h, string_t *key, void *v, void (*freefunc)(void *));

void *ht_get(ht_t *h, string_t *key);

bool ht_exists(ht_t *h, string_t *key);

void ht_free(ht_t *h, void (*freefunc)(void *));

unsigned long hash(ht_t *h, char *key);

bool eval_builtins(value_t *v);

bool eval_ht(value_t *v);

bool eval_quote(value_t *v);

void eval(value_t *v);

#endif // PARSER_H_