-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokeniser.h
63 lines (43 loc) · 1004 Bytes
/
tokeniser.h
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
#ifndef TOKENISER_H_INCLUDED
#include <stdlib.h>
#include <assert.h>
#include "types.h"
#include "tokid.h"
struct token {
enum tokid id;
struct imm_value *val;
struct token *next;
int ref;
};
int tokeniser_init (void);
void tokeniser_exit(void);
struct token *get_next_token(int fd);
char *sym_from_id(enum tokid id);
static inline struct imm_value *val_get(struct imm_value *v) {
v->ref++;
return v;
}
static inline void val_put(struct imm_value *v) {
v->ref--;
assert(v->ref >= 0);
// This is not a bug. At present, immediate values emitted by the
// tokeniser, which have string values, are allocated in one malloc
// so freeing the value also frees the data.
if(v->ref == 0)
free(v);
}
static inline struct token *tok_get(struct token *t) {
t->ref++;
if(t->val)
val_get(t->val);
return t;
}
static inline void tok_put(struct token *t) {
t->ref--;
assert(t->ref >= 0);
val_put(t->val);
if(t->ref == 0)
free(t);
}
#define TOKENISER_H_INCLUDED
#endif