-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.h
72 lines (47 loc) · 1.38 KB
/
util.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
63
64
65
66
67
68
69
70
71
72
#ifndef UTIL_H
#define UTIL_H
#include "token.h"
#include "word.h"
#include <stdlib.h> // for size_t
#include <string.h> // for STREQ()
#include <stdbool.h> // for bool
/* macro */
#define UNUSED_ARG(x) ((void)x)
#define DEREF(type, ptr) (*(type*)(ptr))
#define CAST(type, val) ((type)(val))
#define ALLOCATED(ptr) ((ptr) != NULL && errno != ENOMEM)
#define FREE(ptr) \
do { \
if ((ptr) != NULL) { \
free(ptr); \
(ptr) = NULL; \
} \
} while (0)
#define ASSERT(interp, cond) \
do { \
if (! (cond)) { \
fprintf(stderr, "file %s, line %d: ", __FILE__, __LINE__); \
forth_die((interp), "ASSERT", FORTH_ERR_ASSERT_FAILED); \
} \
} while (0)
#define STREQ(s1, s2) \
(*(s1) == *(s2) && strcmp((s1), (s2)) == 0)
#define AC_TOP_WORD(interp) \
CAST(ForthWord*, (interp)->word_stack->top)
/* util functions */
int
d_print(const char *fmg, ...);
// count c in s.
size_t
strcount(const char *s, int c);
// almost code from 'man 3 strtol'.
// if failed, *failed is not NULL.
digit_t
atod(const char *digit_str, int base, char **failed);
// on success, return true.
bool
dtoa(digit_t digit, char *ascii, size_t max_size, int base);
// convert token to word.
void
forth_token2word(ForthInterp *interp, const char *token, ForthWord *word);
#endif /* UTIL_H */