-
Notifications
You must be signed in to change notification settings - Fork 0
/
machine.h
272 lines (223 loc) · 5.57 KB
/
machine.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#ifndef MACHINE_H_
#define MACHINE_H_
#include <limits.h>
#ifndef STACK_SIZE
#define STACK_SIZE (1 << 16)
#endif
#ifndef WORD_SIZE
#define WORD_SIZE (1 << 12)
#endif
#ifndef NAME_SIZE
#define NAME_SIZE (1 << 8)
#endif
enum Opcode {
OPCODE_SCRIPT_INIT,
OPCODE_SCRIPT_END,
OPCODE_TEST_TERM_NORMAL,
OPCODE_TEST_TERM_NEGATE,
OPCODE_TEST_TERM_BINARY,
OPCODE_TEST_TERM_UNARY,
OPCODE_TEST_EXISTS,
OPCODE_TEST_IS_REGULAR,
OPCODE_TEST_IS_BLOCK,
OPCODE_TEST_IS_CHAR,
OPCODE_TEST_IS_GROUPID_SET,
OPCODE_TEST_IS_USERID_SET,
OPCODE_TEST_IS_STICKY_SET,
OPCODE_TEST_IS_LARGER_ZERO,
OPCODE_TEST_IS_TERMINAL,
OPCODE_TEST_IS_STR_ZERO,
OPCODE_TEST_IS_NAMED_PIPE,
OPCODE_TEST_IS_SYMLINK,
OPCODE_TEST_IS_DIRECTORY,
OPCODE_TEST_IS_READABLE,
OPCODE_TEST_IS_WRITABLE,
OPCODE_TEST_IS_EXECUTABLE,
OPCODE_TEST_IS_LARGER_THAN,
OPCODE_TEST_STR_EMPTY,
OPCODE_TEST_STR_NONEMPTY,
OPCODE_TEST_STR_EQUALS,
OPCODE_TEST_STR_UNEQUALS,
OPCODE_TEST_NUM_EQUALS,
OPCODE_TEST_NUM_UNEQUALS,
OPCODE_TEST_NUM_GREATER,
OPCODE_TEST_NUM_GREATER_EQUALS,
OPCODE_TEST_NUM_LESSER,
OPCODE_TEST_NUM_LESSER_EQUALS,
OPCODE_STMT_ASSIGN,
OPCODE_STMT_FUNCTION,
OPCODE_CASE_PATTERN,
OPCODE_CASE_ITEM,
OPCODE_CASE_BEGIN,
OPCODE_CASE_END,
OPCODE_WORD_LIST_END,
OPCODE_FOR_BEGIN,
OPCODE_FOR_END,
OPCODE_WHILE_BEGIN,
OPCODE_WHILE_END,
OPCODE_IF_BEGIN,
OPCODE_IF_END,
OPCODE_ELIF_BEGIN,
OPCODE_ELIF_END,
OPCODE_ELSE_BEGIN,
OPCODE_ELSE_END,
OPCODE_JUMP,
OPCODE_JUMP_IF_TRUE,
OPCODE_JUMP_IF_FALSE,
OPCODE_DELIMITER,
OPCODE_PATTERN,
OPCODE_REPLACEMENT,
OPCODE_ARITH_ADD,
OPCODE_ARITH_SUB,
OPCODE_ARITH_MUL,
OPCODE_ARITH_DIV,
OPCODE_SUB_EXPR,
OPCODE_SUB_SHELL,
OPCODE_TILDE_EXPN,
OPCODE_REPLACE_ALL,
OPCODE_REPLACE_FIRST,
OPCODE_RM_LARGEST_PFX,
OPCODE_RM_SMALLEST_PFX,
OPCODE_RM_LARGEST_SFX,
OPCODE_RM_SMALLEST_SFX,
OPCODE_ALT_PLUS,
OPCODE_ALT_DASH,
OPCODE_ALT_EQUALS,
OPCODE_ALT_QMARK,
OPCODE_DELIM_COLON,
OPCODE_NO_DELIM,
OPCODE_VAR_SUB,
OPCODE_INPUT_REDIR,
OPCODE_OUTPUT_REDIR,
OPCODE_APPEND_REDIR,
OPCODE_DUPOUT_REDIR,
OPCODE_DUPIN_REDIR,
OPCODE_HERE_DOC,
OPCODE_HERE_STR,
OPCODE_SIMPLE_CMD,
OPCODE_BANGED,
OPCODE_PIPELINE,
OPCODE_LIST_ANDIF,
OPCODE_LIST_ANDOR,
OPCODE_SWITCH,
OPCODE_TRY,
OPCODE_CATCH,
OPCODE_FINALLY,
OPCODE_NORMAL_EXEC,
OPCODE_PARALLEL_EXEC,
OPCODE_ASYNC_EXEC,
OPCODE_ARRAY_CREATE,
OPCODE_ARRAY_ACCESS,
OPCODE_ASSOC_ARRAY_CREATE,
OPCODE_JSON_PARSE,
OPCODE_EXTENDED_VAR_EXPAND,
OPCODE_SUBSTRING,
OPCODE_NAMED_FUNCTION,
OPCODE_MODULE_IMPORT,
OPCODE_REFLECTION,
OPCODE_MIXIN,
OPCODE_STACK_TRACE,
OPCODE_FAILWITH,
OPCODE_OBJECT_MODEL,
OPCODE_METHOD_INVOKE,
OPCODE_API_CALL,
OPCODE_DB_CONNECT,
OPCODE_FUNCTIONAL_OP,
OPCODE_DECLARATIVE_OP,
OPCODE_ADDITIONAL_FUNCTIONS,
OPCODE_REGEX_ENHANCEMENTS,
OPCODE_JOB_DEPENDENCY,
OPCODE_PRIORITY_SCHEDULING,
OPCODE_REGEX_MATCH,
OPCODE_REGEX_NONMATCH,
};
enum ValueKind {
VALUE_WORD,
VALUE_OPCODE,
VALUE_ARGS,
VALUE_NAME,
VALUE_FDESC,
VALUE_CHUNK,
VALUE_REGEX_PATTERN,
VALUE_SPECIAL_PARAMETER,
VALUE_POSITIONAL_PARAMETER,
};
enum SpecialParamKind {
ARGC_NUM,
ARGV,
ENVIRON,
HOME,
PWD,
SHELL,
USER,
UNKNOWN
};
typedef int FDesc;
typedef int PosParam;
typedef char *BytecodeChunk;
typedef char *RegexPattern;
struct Word {
char container[WORD_SIZE];
size_t length;
};
struct Name {
char container[NAME_SIZE];
size_t length;
};
struct Arguments {
char arguments[WORD_SIZE][ARG_MAX];
size_t num_arg;
};
struct StackValue {
enum ValueKind kind;
union Value {
struct Word *word;
struct Name *name;
struct SpecialParamKind special_param;
enum Opcode opcode;
PosParam pos_param;
FDesc fdesc;
BytecodeChunk chunk;
RegexPattern pattern;
};
};
struct Stack {
struct StackValue *operand_stack;
enum Opcode *inst_stack;
size_t ostack_size, istack_size;
int sp, ip;
};
struct Word *new_word(char *word, size_t len);
struct Name *new_name(char *name, size_t len);
struct StackValue *create_word_value(struct Word *word);
struct StackValue *create_bytecode_chunk(BytecodeChunk *chunk);
struct StackValue *create_name_value(struct Name *name);
struct StackValue *create_opcode_value(enum Opcode opcode);
struct StackValue *create_pos_param_value(PosParam posparam);
struct StackValue *create_regex_pattern_value(RegexPattern pattern);
struct StackValue *
create_special_param_value(enum SpecialParamKind kind) struct StackValue
*create_fdesc_value(FDesc fdesc);
bool value_valid_word(struct StackValue *value);
bool value_valid_name(struct StackValue *value);
bool value_valid_opcode(struct StackValue *value);
bool value_valid_pos_param(struct StackValue *value);
bool value_valid_special_param(struct StackValue *value);
bool value_valid_fdesc(struct StackValue *value);
bool value_valid_chunk(struct StackValue *value);
bool value_valid_regex_pattern(struct StackValue *value);
enum SpecialParamKind get_sparam_kind(char c);
struct Stack *create_stack();
int is_stack_empty(struct Stack *stack);
int is_stack_full(struct Stack *stack);
int push_operand(struct Stack *stack, struct StackValue *value);
int push_inst(struct Stack *stack, enum Opcode inst);
struct StackValue *pop_operand(struct Stack *stack);
struct StackValue *peek_operand(struct Stack *stack);
enum Opcode pop_inst(struct Stack *stack);
enum Opcode peek_inst(struct Stack *stack);
void set_sp(struct Stack *stack, int sp);
void set_ip(struct Stack *stack, int ip);
int get_sp(struct Stack *stack);
int get_ip(struct Stack *stack);
#endif /* machine.h */