-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword.h
144 lines (98 loc) · 2.78 KB
/
word.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
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
138
139
140
141
142
143
144
#ifndef WORD_H
#define WORD_H
#include "type.h"
#include "digit.h"
#include <stdlib.h>
#include <stdbool.h>
#define WORD_NULL_FUNC ((word_func_t)0)
#define WORD_FUNC_STR "(function)"
// forth's word
enum word_type {
WORD_UNDEF = 0, // unknown token
WORD_STRING, // string
WORD_DIGIT, // digit
WORD_FUNC, // forth's word
};
typedef enum word_type word_type;
// without volatile, comparison result may be wrong.
typedef void (*word_func_t)(ForthInterp *);
// forth operators
struct ForthWord {
// string value.
struct word_str_t {
char *str;
int capacity;
int len;
} strval;
// digit value.
struct word_digit_t {
digit_t digit;
bool is_set;
} digitval;
// parsed string.
struct word_tokstr_t {
char *str;
int capacity;
int len;
} tokstr;
word_func_t func;
word_type type;
};
typedef struct word_value_t word_value_t;
/* word api */
void
word_init(ForthWord *word);
void
word_init_with_digit(ForthWord *word, digit_t digit);
void
word_init_with_tokstr(ForthWord *word, const char *tokstr);
void
word_init_with_str(ForthWord *word, const char *str);
void
word_destruct(ForthWord *word);
void
word_set_tokstr(ForthWord *word, const char *str);
void
word_set_tokstr_copy(ForthWord *word, const char *str);
void
word_set_str(ForthWord *word, const char *str);
void
word_set_str_copy(ForthWord *word, const char *str);
void
word_set_digit(ForthWord *word, digit_t digit);
/* forth word api */
void
forth_init_word(ForthInterp *interp);
// NOTE: copy the address of tokstr.
void
forth_regist_word(ForthInterp *interp, const char *tokstr, word_func_t func);
char*
forth_word_as_tokstr(ForthInterp *interp, ForthWord *word);
// evaluate word->tokstr.
// NOTE: word->type and word->tokstr must be set.
void
forth_eval_word(ForthInterp *interp, ForthWord *word);
void
forth_uneval_word(ForthInterp *interp, ForthWord *word);
word_type
forth_get_word_type(ForthInterp *interp, const char *token);
ForthWord*
forth_get_word_def(ForthInterp *interp, const char *token);
/* utility functions for word functions */
bool
forth_pop_word(ForthInterp *interp, ForthWord *word);
// faster than forth_pop_word(), and check the top word's type.
bool
forth_pop_str(ForthInterp *interp, char *str);
// faster than forth_pop_word(). do not check the top word's type.
// NOTE: strval must be set.
bool
forth_pop_str_fast(ForthInterp *interp, char *str);
// faster than forth_pop_word(), and check the top word's type.
bool
forth_pop_digit(ForthInterp *interp, digit_t *digit);
// faster than forth_pop_word(). do not check the top word's type.
// NOTE: digitval must be set.
bool
forth_pop_digit_fast(ForthInterp *interp, digit_t *digit);
#endif /* WORD_H */