-
Notifications
You must be signed in to change notification settings - Fork 7
/
parser.h
633 lines (568 loc) · 17.1 KB
/
parser.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
/**
* Structures and functions for parsing tokens to a parse tree. The parser
* reads through a list of tokens (generated by the tokenizer) and adds semantic
* meaning to them by forming them into a parse tree which can then be passed on
* to later stages (such as the interpreter).
*
* \file parser.h
*
* \author Justin J. Meza
*
* \date 2010-2012
*/
#ifndef __PARSER_H__
#define __PARSER_H__
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <float.h>
#include "tokenizer.h"
#undef DEBUG
/**
* Represents a statement type.
*/
typedef enum {
ST_CAST, /**< Cast statement. */
ST_PRINT, /**< Print statement. */
ST_INPUT, /**< Input statement. */
ST_ASSIGNMENT, /**< Assignment statement. */
ST_DECLARATION, /**< Declaration statement. */
ST_IFTHENELSE, /**< If/then/else statement. */
ST_SWITCH, /**< Switch statement. */
ST_BREAK, /**< Break statement. */
ST_RETURN, /**< Return statement. */
ST_LOOP, /**< Loop statement. */
ST_DEALLOCATION, /**< Deallocation statement. */
ST_FUNCDEF, /**< Function definition statement. */
ST_EXPR, /**< Expression statement. */
ST_ALTARRAYDEF, /**< Function definition statement. */
} StmtType;
/**
* Stores statement data.
*/
typedef struct {
StmtType type; /**< The type of statement in \a node. */
void *stmt; /**< The statement. */
} StmtNode;
/**
* Stores a list of statements.
*/
typedef struct {
unsigned int num; /**< The number of statements. */
StmtNode **stmts; /**< The array of statements. */
} StmtNodeList;
/**
* Represents an expression type.
*/
typedef enum {
ET_CAST, /**< Cast expression. */
ET_CONSTANT, /**< Constant expression. */
ET_IDENTIFIER, /**< Identifier expression. */
ET_FUNCCALL, /**< Function call expression. */
ET_OP, /**< Operation expression. */
ET_IMPVAR /**< \ref impvar "Implicit variable". */
} ExprType;
/**
* Stores an expression.
*/
typedef struct {
ExprType type; /**< The type of expression in \a expr. */
void *expr; /**< The expression. */
} ExprNode;
/**
* Stores a list of expressions.
*/
typedef struct {
unsigned int num; /**< The number of expressions. */
ExprNode **exprs; /**< The array of statements. */
} ExprNodeList;
/**
* Represents an identifier type.
*/
typedef enum {
IT_DIRECT, /**< Direct identifier. */
IT_INDIRECT /**< Indirect identifier. */
} IdentifierType;
/**
* Stores an identifier.
*/
typedef struct identifiernode {
IdentifierType type; /**< The type of identifier in \a id. */
void *id; /**< The identifier. */
char *fname; /**< The original file name. */
unsigned int line; /**< The original line number. */
struct identifiernode *slot; /**< The slot to access. */
} IdentifierNode;
/**
* Stores a list of identifiers.
*/
typedef struct {
unsigned int num; /**< The number of identifiers. */
IdentifierNode **ids; /**< The array of identifiers. */
} IdentifierNodeList;
/**
* Stores a code block.
*/
typedef struct {
StmtNodeList *stmts; /**< The list of statements in the block. */
} BlockNode;
/**
* Stores a list of code blocks.
*/
typedef struct {
unsigned int num; /**< The number of code blocks. */
BlockNode **blocks; /**< The array of code blocks. */
} BlockNodeList;
/**
* Represents a constant type.
*/
typedef enum {
CT_INTEGER, /**< Integer constant. */
CT_FLOAT, /**< Decimal constant. */
CT_BOOLEAN, /**< Boolean constant. */
CT_STRING, /**< String constant. */
CT_NIL, /**< Nil constant. */
CT_ARRAY /**< Array constant. */
} ConstantType;
/**
* Stores constant data.
*/
typedef union {
long long i; /**< Integer data. */
float f; /**< Decimal data. */
char *s; /**< String data. */
} ConstantData;
/**
* Stores a constant.
*/
typedef struct {
ConstantType type; /**< The type of constant in \a data. */
ConstantData data; /**< The constant. */
} ConstantNode;
/**
* Stores a function definition statement.
*/
typedef struct {
IdentifierNode *scope; /**< The scope of the function. */
IdentifierNode *name; /**< The name of the function. */
IdentifierNodeList *args; /**< The names of the function arguments. */
BlockNode *body; /**< The body of the function. */
} FuncDefStmtNode;
/**
* Stores an alternate array definition statement.
*/
typedef struct {
IdentifierNode *name; /**< The name of the array. */
BlockNode *body; /**< The body of the array definition. */
IdentifierNode *parent; /**< An optional inherited array. */
} AltArrayDefStmtNode;
/**
* Stores the main code block of a program.
*
* \note This could be represented with just a BlockNode, but it seems
* significant enough to merit its own structure.
*/
typedef struct {
BlockNode *block; /**< The first block of code to execute. */
} MainNode;
/**
* Stores a variable type.
*/
typedef struct {
ConstantType type; /**< The type variable. */
} TypeNode;
/**
* Stores a cast statement. This statement changes the type of a variable.
*/
typedef struct {
IdentifierNode *target; /**< The name of the variable to cast. */
TypeNode *newtype; /**< The type to cast \a target to. */
} CastStmtNode;
/**
* Stores a print statement. This statement prints a list of expressions with
* an optional newline.
*/
typedef struct {
ExprNodeList *args; /**< The expressions to print. */
int nonl; /**< Whether to print an ending newline. */
} PrintStmtNode;
/**
* Stores an input statement. This statement accepts input from the user and
* stores it in a variable.
*/
typedef struct {
IdentifierNode *target; /**< The variable to store the input in. */
} InputStmtNode;
/**
* Stores an assignment statement. This statement stores an evaluated
* expression in a variable.
*/
typedef struct {
IdentifierNode *target; /**< The variable to store \a expr in. */
ExprNode *expr; /**< The expression to store. */
} AssignmentStmtNode;
/**
* Stores a declaration statement. This statement creates a new variable in a
* given scope and optionally initializes it to an expression.
*
* \note Either provide \a expr OR \a type. If both are provided, the result is
* undefined.
*/
typedef struct {
IdentifierNode *scope; /**< The scope to create the variable in. */
IdentifierNode *target; /**< The name of the variable to create. */
ExprNode *expr; /**< An optional initialization expression. */
TypeNode *type; /**< An optional initialization type. */
IdentifierNode *parent; /**< An optional inherited array. */
} DeclarationStmtNode;
/**
* Stores an if/then/else statement. This statement checks the value of the
* \ref impvar "implicit variable" and executes the \c yes block if it can be
* cast to true. Else, the \c guards are evaluated and the corresponding code
* in one of the \c blocks is executed. Finally, if none of these things occur,
* the \c no block is executed.
*/
typedef struct {
BlockNode *yes; /**< The code to execute if \c IT is true. */
BlockNode *no; /**< The code to execute if nothing else does. */
ExprNodeList *guards; /**< The guards for the \c blocks. */
BlockNodeList *blocks; /**< The code to execute if a guard is true. */
} IfThenElseStmtNode;
/**
* Stores a switch statement. This statement compares the value of the \ref
* impvar "implicit variable" to each of the \a guards and executes the
* respective block of code in \a blocks if they match. If no matches are
* found, the optional default block of code, \a def, is executed.
*/
typedef struct {
ExprNodeList *guards; /**< The expressions to evaluate. */
BlockNodeList *blocks; /**< The blocks of code to execute. */
BlockNode *def; /**< An optional default block of code. */
} SwitchStmtNode;
/**
* Stores a return statement. This statement signals that control should be
* returned to the caller with a status value.
*/
typedef struct {
ExprNode *value; /**< The value to return. */
} ReturnStmtNode;
/**
* Stores a loop statement. This statement repeatedly executes its \a body
* while \a guard evaluates to true, executing \a update at the end of each
* cycle.
*/
typedef struct {
IdentifierNode *name; /**< The name of the loop. */
IdentifierNode *var; /**< The variable to be updated. */
ExprNode *guard; /**< The expression to determine continuation. */
ExprNode *update; /**< The expression to update \a var with. */
BlockNode *body; /**< The code to execute at each iteration. */
} LoopStmtNode;
/**
* Stores a deallocation statement. This statement releases the resources used
* by a variable.
*/
typedef struct {
IdentifierNode *target; /**< The variable to deallocate. */
} DeallocationStmtNode;
/**
* Stores a cast expression. This expression evaluates an expression and casts
* its value to a particular type.
*/
typedef struct {
ExprNode *target; /**< The expression to cast. */
TypeNode *newtype; /**< The type to cast \a target to. */
} CastExprNode;
/**
* Stores a function call expression. This expression calls a named function
* and evaluates to the return value of that function.
*/
typedef struct {
IdentifierNode *scope; /**< The scope to call the function in. */
IdentifierNode *name; /**< The name of the function to call. */
ExprNodeList *args; /**< The arguments to supply the function. */
} FuncCallExprNode;
/**
* Represents the type of operation an OpExprNode performs.
*/
typedef enum {
OP_ADD, /**< Addition. */
OP_SUB, /**< Subtraction. */
OP_MULT, /**< Multiplication. */
OP_DIV, /**< Division. */
OP_MOD, /**< Modulo. */
OP_MAX, /**< Maximum. */
OP_MIN, /**< Minimum. */
OP_AND, /**< Logical AND. */
OP_OR, /**< Logical OR. */
OP_XOR, /**< Logical XOR. */
OP_NOT, /**< Logical NOT. */
OP_EQ, /**< Equality. */
OP_NEQ, /**< Inequality. */
OP_CAT /**< String concatenation. */
} OpType;
/**
* Stores an operation expression. This expression applies an operator to its
* arguments.
*/
typedef struct {
OpType type; /**< The type of operation to perform. */
ExprNodeList *args; /**< The arguments to perform the operation on. */
} OpExprNode;
/**
* \name MainNode modifiers
*
* Functions for creating and deleting MainNodes.
*/
/**@{*/
MainNode *createMainNode(BlockNode *);
void deleteMainNode(MainNode *);
/**@}*/
/**
* \name BlockNode modifiers
*
* Functions for creating and deleting single or multiple BlockNodes.
*/
/**@{*/
BlockNode *createBlockNode(StmtNodeList *);
void deleteBlockNode(BlockNode *);
BlockNodeList *createBlockNodeList(void);
int addBlockNode(BlockNodeList *, BlockNode *);
void deleteBlockNodeList(BlockNodeList *);
/**@}*/
/**
* \name IdentifierNode modifiers
*
* Functions for creating and deleting single or multiple IdentifierNodes.
*/
/**@{*/
IdentifierNode *createIdentifierNode(IdentifierType, void *, IdentifierNode *, const char *, unsigned int);
void deleteIdentifierNode(IdentifierNode *);
IdentifierNodeList *createIdentifierNodeList(void);
int addIdentifierNode(IdentifierNodeList *, IdentifierNode *);
void deleteIdentifierNodeList(IdentifierNodeList *);
/**@}*/
/**
* \name TypeNode modifiers
*
* Functions for creating and deleting TypeNodes.
*/
/**@{*/
TypeNode *createTypeNode(ConstantType);
void deleteTypeNode(TypeNode *);
/**@}*/
/**
* \name StmtNode modifiers
*
* Functions for creating and deleting single or multiple of StmtNodes.
*/
/**@{*/
StmtNode *createStmtNode(StmtType, void *);
void deleteStmtNode(StmtNode *);
StmtNodeList *createStmtNodeList(void);
int addStmtNode(StmtNodeList *, StmtNode *);
void deleteStmtNodeList(StmtNodeList *);
/**@}*/
/**
* \name CastStmtNode modifiers
*
* Functions for creating and deleting CastStmtNodes.
*/
/**@{*/
CastStmtNode *createCastStmtNode(IdentifierNode *, TypeNode *);
void deleteCastStmtNode(CastStmtNode *);
/**@}*/
/**
* \name PrintStmtNode modifiers
*
* Functions for creating and deleting PrintStmtNodes.
*/
/**@{*/
PrintStmtNode *createPrintStmtNode(ExprNodeList *, int);
void deletePrintStmtNode(PrintStmtNode *);
/**@}*/
/**
* \name InputStmtNode modifiers
*
* Functions for creating and deleting InputStmtNodes.
*/
/**@{*/
InputStmtNode *createInputStmtNode(IdentifierNode *);
void deleteInputStmtNode(InputStmtNode *);
/**@}*/
/**
* \name AssignmentStmtNode modifiers
*
* Functions for creating and deleting AssignmentStmtNodes.
*/
/**@{*/
AssignmentStmtNode *createAssignmentStmtNode(IdentifierNode *, ExprNode *);
void deleteAssignmentStmtNode(AssignmentStmtNode *);
/**@}*/
/**
* \name DeclarationStmtNode modifiers
*
* Functions for creating and deleting DeclarationStmtNodes.
*/
/**@{*/
DeclarationStmtNode *createDeclarationStmtNode(IdentifierNode *, IdentifierNode *, ExprNode *, TypeNode *, IdentifierNode *);
void deleteDeclarationStmtNode(DeclarationStmtNode *);
/**@}*/
/**
* \name IfThenElseStmtNode modifiers
*
* Functions for creating and deleting IfThenElseStmtNodes.
*/
/**@{*/
IfThenElseStmtNode *createIfThenElseStmtNode(BlockNode *, BlockNode *, ExprNodeList *, BlockNodeList *);
void deleteIfThenElseStmtNode(IfThenElseStmtNode *);
/**@}*/
/**
* \name SwitchStmtNode modifiers
*
* Functions for creating and deleting SwitchStmtNodes.
*/
/**@{*/
SwitchStmtNode *createSwitchStmtNode(ExprNodeList *, BlockNodeList *, BlockNode *);
void deleteSwitchStmtNode(SwitchStmtNode *);
/**@}*/
/**
* \name ReturnStmtNode modifiers
*
* Functions for creating and deleting ReturnStmtNodes.
*/
/**@{*/
ReturnStmtNode *createReturnStmtNode(ExprNode *);
void deleteReturnStmtNode(ReturnStmtNode *);
/**@}*/
/**
* \name LoopStmtNode modifiers
*
* Functions for creating and deleting LoopStmtNodes.
*/
/**@{*/
LoopStmtNode *createLoopStmtNode(IdentifierNode *, IdentifierNode *, ExprNode *, ExprNode *, BlockNode *);
void deleteLoopStmtNode(LoopStmtNode *);
/**@}*/
/**
* \name DeallocationStmtNode modifiers
*
* Functions for creating and deleting DeallocationStmtNodes.
*/
/**@{*/
DeallocationStmtNode *createDeallocationStmtNode(IdentifierNode *);
void deleteDeallocationStmtNode(DeallocationStmtNode *);
/**@}*/
/**
* \name FuncDefStmtNode modifiers
*
* Functions for creating and deleting FuncDefStmtNodes.
*/
/**@{*/
FuncDefStmtNode *createFuncDefStmtNode(IdentifierNode *, IdentifierNode *, IdentifierNodeList *, BlockNode *);
void deleteFuncDefStmtNode(FuncDefStmtNode *);
/**@}*/
/**
* \name AltArrayDefStmtNode modifiers
*
* Functions for creating and deleting AltArrayDefStmtNodes.
*/
/**@{*/
AltArrayDefStmtNode *createAltArrayDefStmtNode(IdentifierNode *, BlockNode *, IdentifierNode *);
void deleteAltArrayDefStmtNode(AltArrayDefStmtNode *);
/**@}*/
/**
* \name ExprNode modifiers
*
* Functions for creating and deleting single or multiple ExprNodes.
*/
/**@{*/
ExprNode *createExprNode(ExprType, void *);
void deleteExprNode(ExprNode *);
ExprNodeList *createExprNodeList(void);
int addExprNode(ExprNodeList *, ExprNode *);
void deleteExprNodeList(ExprNodeList *);
/**@}*/
/**
* \name CastExprNode modifiers
*
* Functions for creating and deleting CastExprNodes.
*/
/**@{*/
CastExprNode *createCastExprNode(ExprNode *, TypeNode *);
void deleteCastExprNode(CastExprNode *);
/**@}*/
/**
* \name FuncCallExprNode modifiers
*
* Functions for creating and deleting FuncCallExprNodes.
*/
/**@{*/
FuncCallExprNode *createFuncCallExprNode(IdentifierNode *, IdentifierNode *, ExprNodeList *);
void deleteFuncCallExprNode(FuncCallExprNode *);
/**@}*/
/**
* \name OpExprNode modifiers
*
* Functions for creating and deleting OpExprNodes.
*/
/**@{*/
OpExprNode *createOpExprNode(OpType, ExprNodeList *);
void deleteOpExprNode(OpExprNode *);
/**@}*/
/**
* \name Utilities
*
* Functions for performing helper tasks.
*/
/**@{*/
int acceptToken(Token ***, TokenType);
int peekToken(Token ***, TokenType);
int nextToken(Token ***, TokenType);
/**@}*/
/**
* \name Parsing functions
*
* Functions for parsing a stream of tokens.
*/
/**@{*/
ConstantNode *parseConstantNode(Token ***);
TypeNode *parseTypeNode(Token ***);
IdentifierNode *parseIdentifierNode(Token ***);
ExprNode *parseExprNode(Token ***);
StmtNode *parseStmtNode(Token ***);
BlockNode *parseBlockNode(Token ***);
MainNode *parseMainNode(Token **);
ExprNode *parseCastExprNode(Token ***);
ExprNode *parseConstantExprNode(Token ***);
ExprNode *parseIdentifierExprNode(Token ***);
ExprNode *parseFuncCallExprNode(Token ***);
ExprNode *parseOpExprNode(Token ***);
StmtNode *parseCastStmtNode(Token ***);
StmtNode *parsePrintStmtNode(Token ***);
StmtNode *parseInputStmtNode(Token ***);
StmtNode *parseAssignmentStmtNode(Token ***);
StmtNode *parseDeclarationStmtNode(Token ***);
StmtNode *parseIfThenElseStmtNode(Token ***);
StmtNode *parseSwitchStmtNode(Token ***);
StmtNode *parseBreakStmtNode(Token ***);
StmtNode *parseReturnStmtNode(Token ***);
StmtNode *parseLoopStmtNode(Token ***);
StmtNode *parseDeallocationStmtNode(Token ***);
StmtNode *parseFuncDefStmtNode(Token ***);
StmtNode *parseAltArrayDefStmtNode(Token ***);
/**@}*/
/**
* \name ConstantNode modifiers
*
* Functions for creating and deleting ConstantNode.
*/
/**@{*/
ConstantNode *createBooleanConstantNode(int);
ConstantNode *createIntegerConstantNode(long long);
ConstantNode *createFloatConstantNode(float);
ConstantNode *createStringConstantNode(char *);
void deleteConstantNode(ConstantNode *);
/**@}*/
#endif /* __PARSER_H__ */