-
Notifications
You must be signed in to change notification settings - Fork 105
/
interpreter.h
387 lines (350 loc) · 11.7 KB
/
interpreter.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/**
* Structures and functions for interpreting a parse tree. The interpreter
* traverses a parse tree in a depth-first manner, interpreting each node it
* reaches along the way. This is the last stage of the processing of a source
* code file.
*
* \file interpreter.h
*
* \author Justin J. Meza
*
* \date 2010-2012
*/
#ifndef __INTERPRETER_H__
#define __INTERPRETER_H__
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include "parser.h"
#include "unicode.h"
/**
* Retrieves a value's integer data.
*/
#define getInteger(value) (value->data.i)
/**
* Retrieves a value's decimal data.
*/
#define getFloat(value) (value->data.f)
/**
* Retrieves a value's string data.
*/
#define getString(value) (value->data.s)
/**
* Retrieves a value's function data.
*/
#define getFunction(value) (value->data.fn)
/**
* Retrieves a value's array data.
*/
#define getArray(value) (value->data.a)
/**
* Represents a value type.
*/
typedef enum {
VT_INTEGER, /**< An integer value. */
VT_FLOAT, /**< A decimal value. */
VT_BOOLEAN, /**< A boolean value. */
VT_STRING, /**< A string value. */
VT_NIL, /**< Represents no value. */
VT_FUNC, /**< A function. */
VT_ARRAY /**< An array. */
} ValueType;
/**
* Stores value data.
*/
typedef union {
long long i; /**< Integer data. */
float f; /**< Decimal data. */
char *s; /**< String data. */
FuncDefStmtNode *fn; /**< Function data. */
struct scopeobject *a; /**< Array data. */
} ValueData;
/**
* Increments a value's semaphore.
*/
#define V(value) (value->semaphore++)
/**
* Decrements a value's semaphore.
*/
#define P(value) (value->semaphore--)
/**
* Stores a value.
*/
typedef struct {
ValueType type; /**< The type of value stored. */
ValueData data; /**< The value data. */
unsigned short semaphore; /**< A semaphore for value usage. */
} ValueObject;
/**
* Represents the return type.
*/
typedef enum {
RT_DEFAULT, /**< Code block completed successfully. */
RT_BREAK, /**< Broke out of a loop or switch statement. */
RT_RETURN /**< Returned from a function. */
} ReturnType;
/**
* Stores return state.
*/
typedef struct {
ReturnType type; /**< The type of return encountered. */
ValueObject *value; /**< The optional return value. */
} ReturnObject;
/**
* Stores a set of variables hierarchically.
*/
typedef struct scopeobject {
struct scopeobject *parent; /**< The parent scope. */
struct scopeobject *caller; /**< The caller scope (if in a function). */
ValueObject *impvar; /**< The \ref impvar "implicit variable". */
unsigned int numvals; /**< The number of values in the scope. */
char **names; /**< The names of the values. */
ValueObject **values; /**< The values in the scope. */
} ScopeObject;
/**
* \name Utilities
*
* Functions for performing helper tasks.
*/
/**@{*/
void printInterpreterError(const char *, IdentifierNode *, ScopeObject *);
char *copyString(char *);
unsigned int isHexString(const char *);
char *resolveIdentifierName(IdentifierNode *, ScopeObject *);
int resolveTerminalSlot(ScopeObject *, ScopeObject *, IdentifierNode *, ScopeObject **, IdentifierNode **);
/**@}*/
/**
* \name Value object modifiers
*
* Functions for creating, copying, and deleting value objects.
*/
/**@{*/
ValueObject *createNilValueObject(void);
ValueObject *createBooleanValueObject(int);
ValueObject *createIntegerValueObject(long long);
ValueObject *createFloatValueObject(float);
ValueObject *createStringValueObject(char *);
ValueObject *createFunctionValueObject(FuncDefStmtNode *);
ValueObject *createArrayValueObject(ScopeObject *);
ValueObject *copyValueObject(ValueObject *);
void deleteValueObject(ValueObject *);
/**@}*/
/**
* \name Scope object modifiers
*
* Functions for manipulating scope objects and their data.
*/
/**@{*/
ScopeObject *createScopeObject(ScopeObject *);
ScopeObject *createScopeObjectCaller(ScopeObject *, ScopeObject *);
void deleteScopeObject(ScopeObject *);
ValueObject *createScopeValue(ScopeObject *, ScopeObject *, IdentifierNode *);
ValueObject *updateScopeValue(ScopeObject *, ScopeObject *, IdentifierNode *, ValueObject *);
ValueObject *getScopeValue(ScopeObject *, ScopeObject *, IdentifierNode *);
ValueObject *getScopeValueLocal(ScopeObject *, ScopeObject *, IdentifierNode *);
ScopeObject *getScopeObject(ScopeObject *, ScopeObject *, IdentifierNode *);
ScopeObject *getScopeObjectLocal(ScopeObject *, ScopeObject *, IdentifierNode *);
void deleteScopeValue(ScopeObject *, ScopeObject *, IdentifierNode *);
/**@}*/
/**
* \name Return object modifiers
*
* Functions for creating and deleting return objects.
*/
/**@{*/
ReturnObject *createReturnObject(ReturnType, ValueObject *);
void deleteReturnObject(ReturnObject *);
/**@}*/
/**
* \name Casts
*
* Functions for performing casts between different types of values.
*/
/**@{*/
ValueObject *castBooleanImplicit(ValueObject *, ScopeObject *);
ValueObject *castIntegerImplicit(ValueObject *, ScopeObject *);
ValueObject *castFloatImplicit(ValueObject *, ScopeObject *);
ValueObject *castStringImplicit(ValueObject *, ScopeObject *);
ValueObject *castBooleanExplicit(ValueObject *, ScopeObject *);
ValueObject *castIntegerExplicit(ValueObject *, ScopeObject *);
ValueObject *castFloatExplicit(ValueObject *, ScopeObject *);
ValueObject *castStringExplicit(ValueObject *, ScopeObject *);
/**@}*/
/**
* \name Node interpreters
*
* Functions for interpreting basic parse tree nodes.
*/
/**@{*/
ValueObject *interpretExprNode(ExprNode *, ScopeObject *);
ReturnObject *interpretStmtNode(StmtNode *, ScopeObject *);
ReturnObject *interpretStmtNodeList(StmtNodeList *, ScopeObject *);
ReturnObject *interpretBlockNode(BlockNode *, ScopeObject *);
int interpretMainNode(MainNode *);
/**@}*/
/**
* \name Expression interpreters
*
* Functions for interpreting expression parse tree nodes.
*/
/**@{*/
ValueObject *interpretImpVarExprNode(ExprNode *, ScopeObject *);
ValueObject *interpretCastExprNode(ExprNode *, ScopeObject *);
ValueObject *interpretFuncCallExprNode(ExprNode *, ScopeObject *);
ValueObject *interpretIdentifierExprNode(ExprNode *, ScopeObject *);
ValueObject *interpretConstantExprNode(ExprNode *, ScopeObject *);
/**@}*/
/**
* \name Operation interpreters
*
* Functions for interpreting operation parse tree nodes.
*/
/**@{*/
ValueObject *interpretNotOpExprNode(OpExprNode *, ScopeObject *);
ValueObject *interpretArithOpExprNode(OpExprNode *, ScopeObject *);
ValueObject *interpretBoolOpExprNode(OpExprNode *, ScopeObject *);
ValueObject *interpretEqualityOpExprNode(OpExprNode *, ScopeObject *);
ValueObject *interpretConcatOpExprNode(OpExprNode *, ScopeObject *);
ValueObject *interpretOpExprNode(ExprNode *, ScopeObject *);
/**@}*/
/**
* \name Statement interpreters
*
* Functions for interpreting statement parse tree nodes.
*/
/**@{*/
ReturnObject *interpretCastStmtNode(StmtNode *, ScopeObject *);
ReturnObject *interpretPrintStmtNode(StmtNode *, ScopeObject *);
ReturnObject *interpretInputStmtNode(StmtNode *, ScopeObject *);
ReturnObject *interpretAssignmentStmtNode(StmtNode *, ScopeObject *);
ReturnObject *interpretDeclarationStmtNode(StmtNode *, ScopeObject *);
ReturnObject *interpretIfThenElseStmtNode(StmtNode *, ScopeObject *);
ReturnObject *interpretSwitchStmtNode(StmtNode *, ScopeObject *);
ReturnObject *interpretBreakStmtNode(StmtNode *, ScopeObject *);
ReturnObject *interpretReturnStmtNode(StmtNode *, ScopeObject *);
ReturnObject *interpretLoopStmtNode(StmtNode *, ScopeObject *);
ReturnObject *interpretDeallocationStmtNode(StmtNode *, ScopeObject *);
ReturnObject *interpretFuncDefStmtNode(StmtNode *, ScopeObject *);
ReturnObject *interpretExprStmtNode(StmtNode *, ScopeObject *);
ReturnObject *interpretAltArrayDefStmtNode(StmtNode *, ScopeObject *);
/**@}*/
/**
* \name Arithmetic operations (integer-integer)
*
* Functions for performing integer-integer operations on values.
*/
/**@{*/
ValueObject *opAddIntegerInteger(ValueObject *, ValueObject *);
ValueObject *opSubIntegerInteger(ValueObject *, ValueObject *);
ValueObject *opMultIntegerInteger(ValueObject *, ValueObject *);
ValueObject *opDivIntegerInteger(ValueObject *, ValueObject *);
ValueObject *opMaxIntegerInteger(ValueObject *, ValueObject *);
ValueObject *opMinIntegerInteger(ValueObject *, ValueObject *);
ValueObject *opModIntegerInteger(ValueObject *, ValueObject *);
/**@}*/
/**
* \name Arithmetic operations (integer-float)
*
* Functions for performing integer-float operations on values.
*/
/**@{*/
ValueObject *opAddIntegerFloat(ValueObject *, ValueObject *);
ValueObject *opSubIntegerFloat(ValueObject *, ValueObject *);
ValueObject *opMultIntegerFloat(ValueObject *, ValueObject *);
ValueObject *opDivIntegerFloat(ValueObject *, ValueObject *);
ValueObject *opMaxIntegerFloat(ValueObject *, ValueObject *);
ValueObject *opMinIntegerFloat(ValueObject *, ValueObject *);
ValueObject *opModIntegerFloat(ValueObject *, ValueObject *);
/**@}*/
/**
* \name Arithmetic operations (float-integer)
*
* Functions for performing float-integer operations on values.
*/
/**@{*/
ValueObject *opAddFloatInteger(ValueObject *, ValueObject *);
ValueObject *opSubFloatInteger(ValueObject *, ValueObject *);
ValueObject *opMultFloatInteger(ValueObject *, ValueObject *);
ValueObject *opDivFloatInteger(ValueObject *, ValueObject *);
ValueObject *opMaxFloatInteger(ValueObject *, ValueObject *);
ValueObject *opMinFloatInteger(ValueObject *, ValueObject *);
ValueObject *opModFloatInteger(ValueObject *, ValueObject *);
/**@}*/
/**
* \name Arithmetic operations (float-float)
*
* Functions for performing float-float operations on values.
*/
/**@{*/
ValueObject *opAddFloatFloat(ValueObject *, ValueObject *);
ValueObject *opSubFloatFloat(ValueObject *, ValueObject *);
ValueObject *opMultFloatFloat(ValueObject *, ValueObject *);
ValueObject *opDivFloatFloat(ValueObject *, ValueObject *);
ValueObject *opMaxFloatFloat(ValueObject *, ValueObject *);
ValueObject *opMinFloatFloat(ValueObject *, ValueObject *);
ValueObject *opModFloatFloat(ValueObject *, ValueObject *);
/**@}*/
/**
* \name Equality operations (boolean-boolean)
*
* Functions for performing boolean-boolean operations on values.
*/
/**@{*/
ValueObject *opEqBooleanBoolean(ValueObject *, ValueObject *);
ValueObject *opNeqBooleanBoolean(ValueObject *, ValueObject *);
/**@}*/
/**
* \name Equality operations (integer-integer)
*
* Functions for performing integer-integer operations on values.
*/
/**@{*/
ValueObject *opEqIntegerInteger(ValueObject *, ValueObject *);
ValueObject *opNeqIntegerInteger(ValueObject *, ValueObject *);
/**@}*/
/**
* \name Equality operations (integer-float)
*
* Functions for performing integer-float operations on values.
*/
/**@{*/
ValueObject *opEqIntegerFloat(ValueObject *, ValueObject *);
ValueObject *opNeqIntegerFloat(ValueObject *, ValueObject *);
/**@}*/
/**
* \name Equality operations (float-integer)
*
* Functions for performing float-integer operations on values.
*/
/**@{*/
ValueObject *opEqFloatInteger(ValueObject *, ValueObject *);
ValueObject *opNeqFloatInteger(ValueObject *, ValueObject *);
/**@}*/
/**
* \name Equality operations (float-float)
*
* Functions for performing float-float operations on values.
*/
/**@{*/
ValueObject *opEqFloatFloat(ValueObject *, ValueObject *);
ValueObject *opNeqFloatFloat(ValueObject *, ValueObject *);
/**@}*/
/**
* \name Equality operations (string-string)
*
* Functions for performing string-string operations on values.
*/
/**@{*/
ValueObject *opEqStringString(ValueObject *, ValueObject *);
ValueObject *opNeqStringString(ValueObject *, ValueObject *);
/**@}*/
/**
* \name Equality operations (nil-nil)
*
* Functions for performing nil-nil operations on values.
*/
/**@{*/
ValueObject *opEqNilNil(ValueObject *, ValueObject *);
ValueObject *opNeqNilNil(ValueObject *, ValueObject *);
/**@}*/
#endif /* __INTERPRETER_H__ */