Decaf Compiler
ast.h
Go to the documentation of this file.
1 
10 #ifndef __AST_H
11 #define __AST_H
12 
13 #include "common.h"
14 
18 typedef void (*AttributeValueDOTPrinter)(void*, FILE*);
19 
23 typedef void (*Destructor)(void*);
24 
32 void dummy_free(void*);
33 
40 void dummy_print(void*, FILE*);
41 
49 void int_attr_print(void*, FILE*);
50 
51 
55 typedef enum NodeType {
56  PROGRAM, VARDECL, FUNCDECL, BLOCK,
57  ASSIGNMENT, CONDITIONAL, WHILELOOP, RETURNSTMT, BREAKSTMT, CONTINUESTMT,
58  BINARYOP, UNARYOP, LOCATION, FUNCCALL, LITERAL
59 } NodeType;
60 
67 const char* NodeType_to_string(NodeType type);
68 
72 typedef struct ProgramNode {
73  struct NodeList* variables;
74  struct NodeList* functions;
75 } ProgramNode;
76 
86 struct ASTNode* ProgramNode_new (struct NodeList* vars, struct NodeList* funcs);
87 
91 typedef struct VarDeclNode {
92  char name[MAX_ID_LEN];
94  bool is_array;
96 } VarDeclNode;
97 
108 struct ASTNode* VarDeclNode_new (const char* name, DecafType type,
109  bool is_array, int array_length, int source_line);
110 
114 typedef struct Parameter {
115  char name[MAX_ID_LEN];
117  struct Parameter* next;
118 } Parameter;
119 
120 /*
121  * Declare ParameterList to be a linked list of Parameter* elements.
122  */
124 
125 
133 void ParameterList_add_new (ParameterList* list, const char* name, DecafType type);
134 
138 typedef struct FuncDeclNode {
139  char name[MAX_ID_LEN];
142  struct ASTNode* body;
143 } FuncDeclNode;
144 
155 struct ASTNode* FuncDeclNode_new (const char* name, DecafType return_type, ParameterList* parameters,
156  struct ASTNode* body, int source_line);
157 
161 typedef struct BlockNode {
162  struct NodeList* variables;
164 } BlockNode;
165 
174 struct ASTNode* BlockNode_new (struct NodeList* vars, struct NodeList* stmts, int source_line);
175 
179 typedef struct AssignmentNode {
180  struct ASTNode* location;
181  struct ASTNode* value;
183 
192 struct ASTNode* AssignmentNode_new (struct ASTNode* location, struct ASTNode* value, int source_line);
193 
199 typedef struct ConditionalNode {
200  struct ASTNode* condition;
201  struct ASTNode* if_block;
202  struct ASTNode* else_block;
205 
216 struct ASTNode* ConditionalNode_new (struct ASTNode* condition, struct ASTNode* if_block,
217  struct ASTNode* else_block, int source_line);
218 
222 typedef struct WhileLoopNode {
223  struct ASTNode* condition;
224  struct ASTNode* body;
225 } WhileLoopNode;
226 
235 struct ASTNode* WhileLoopNode_new (struct ASTNode* condition, struct ASTNode* body, int source_line);
236 
242 typedef struct ReturnNode {
243  struct ASTNode* value;
244 } ReturnNode;
245 
253 struct ASTNode* ReturnNode_new (struct ASTNode* value, int source_line);
254 
261 struct ASTNode* BreakNode_new (int source_line);
262 
269 struct ASTNode* ContinueNode_new (int source_line);
270 
274 typedef enum BinaryOpType {
275  OROP, ANDOP, EQOP, NEQOP, LTOP, LEOP, GEOP, GTOP,
276  ADDOP, SUBOP, MULOP, DIVOP, MODOP
277 } BinaryOpType;
278 
285 const char* BinaryOpToString(BinaryOpType op);
286 
290 typedef struct BinaryOpNode {
291  BinaryOpType operator;
292  struct ASTNode* left;
293  struct ASTNode* right;
294 } BinaryOpNode;
295 
305 struct ASTNode* BinaryOpNode_new (BinaryOpType operator,
306  struct ASTNode* left, struct ASTNode* right,
307  int source_line);
308 
312 typedef enum UnaryOpType {
313  NEGOP, NOTOP
314 } UnaryOpType;
315 
322 const char* UnaryOpToString(UnaryOpType op);
323 
327 typedef struct UnaryOpNode {
328  UnaryOpType operator;
329  struct ASTNode* child;
330 } UnaryOpNode;
331 
340 struct ASTNode* UnaryOpNode_new (UnaryOpType operator, struct ASTNode* child, int source_line);
341 
347 typedef struct LocationNode {
348  char name[MAX_ID_LEN];
349  struct ASTNode* index;
350 } LocationNode;
351 
360 struct ASTNode* LocationNode_new (const char* name, struct ASTNode* index, int source_line);
361 
365 typedef struct FuncCallNode {
366  char name[MAX_ID_LEN];
367  struct NodeList* arguments;
368 } FuncCallNode;
369 
378 struct ASTNode* FuncCallNode_new (const char* name, struct NodeList* args, int source_line);
379 
383 typedef struct LiteralNode {
385  union {
386  int integer;
387  bool boolean;
388  char string[MAX_LINE_LEN];
389  };
390 } LiteralNode;
391 
399 struct ASTNode* LiteralNode_new_int (int value, int source_line);
400 
408 struct ASTNode* LiteralNode_new_bool (bool value, int source_line);
409 
417 struct ASTNode* LiteralNode_new_string (const char* value, int source_line);
418 
422 typedef struct Attribute
423 {
424  const char* key;
425  void* value;
431  struct Attribute* next;
432 } Attribute;
433 
470 typedef struct ASTNode
471 {
475  struct ASTNode* next;
477  /* anonymous union of type-specific node data (C polymorphism) */
478  union {
479  struct ProgramNode program;
480  struct VarDeclNode vardecl;
481  struct FuncDeclNode funcdecl;
482  struct BlockNode block;
483  struct AssignmentNode assignment;
484  struct ConditionalNode conditional;
485  struct WhileLoopNode whileloop;
486  struct ReturnNode funcreturn;
487  struct BinaryOpNode binaryop;
488  struct UnaryOpNode unaryop;
489  struct LocationNode location;
490  struct FuncCallNode funccall;
491  struct LiteralNode literal;
492  };
493 } ASTNode;
494 
495 /*
496  * Declare NodeList to be a linked list of ASTNode* elements.
497  */
498 DECL_LIST_TYPE(Node, struct ASTNode*)
499 
500 
516 ASTNode* ASTNode_new (NodeType type, int line);
517 
533 void ASTNode_set_attribute (ASTNode* node, const char* key, void* value, Destructor dtor);
534 
550 void ASTNode_set_printable_attribute (ASTNode* node, const char* key, void* value,
551  AttributeValueDOTPrinter dot_printer, Destructor dtor);
552 
564 void ASTNode_set_int_attribute (ASTNode* node, const char* key, int value);
565 
573 bool ASTNode_has_attribute (ASTNode* node, const char* key);
574 
585 void* ASTNode_get_attribute (ASTNode* node, const char* key);
586 
596 int ASTNode_get_int_attribute (ASTNode* node, const char* key);
597 
609 void ASTNode_free (ASTNode* node);
610 
611 #endif
AssignmentNode::location
struct ASTNode * location
Left-hand side of assignment.
Definition: ast.h:180
LiteralNode
AST literal expression structure.
Definition: ast.h:383
NodeType
NodeType
AST node type tag.
Definition: ast.h:55
Parameter
AST parameter (used in function declarations)
Definition: ast.h:114
FuncDeclNode
AST function structure.
Definition: ast.h:138
ConditionalNode::if_block
struct ASTNode * if_block
Block to be executed if the condition is true.
Definition: ast.h:201
BinaryOpNode_new
struct ASTNode * BinaryOpNode_new(BinaryOpType operator, struct ASTNode *left, struct ASTNode *right, int source_line)
Allocate a new binary operation expression AST node.
Definition: ast.c:320
ProgramNode
struct ProgramNode ProgramNode
AST root structure.
AssignmentNode_new
struct ASTNode * AssignmentNode_new(struct ASTNode *location, struct ASTNode *value, int source_line)
Allocate a new assignment statement AST node.
Definition: ast.c:258
dummy_free
void dummy_free(void *)
Fake "free" function that does nothing.
Definition: ast.c:14
FuncDeclNode::return_type
DecafType return_type
Function return type.
Definition: ast.h:140
FuncCallNode
AST function call expression structure.
Definition: ast.h:365
ASTNode_get_int_attribute
int ASTNode_get_int_attribute(ASTNode *node, const char *key)
Retrieve a particular integer attribute from a node.
Definition: ast.c:133
FuncCallNode_new
struct ASTNode * FuncCallNode_new(const char *name, struct NodeList *args, int source_line)
Allocate a new function call expression AST node.
Definition: ast.c:354
BlockNode
struct BlockNode BlockNode
AST block structure.
LocationNode
struct LocationNode LocationNode
AST location expression structure.
UnaryOpNode
AST unary operation expression structure.
Definition: ast.h:327
ASTNode::next
struct ASTNode * next
Next node (if stored in a list)
Definition: ast.h:475
ASTNode
Main AST node structure.
Definition: ast.h:470
WhileLoopNode
struct WhileLoopNode WhileLoopNode
AST while loop structure.
FuncDeclNode_new
struct ASTNode * FuncDeclNode_new(const char *name, DecafType return_type, ParameterList *parameters, struct ASTNode *body, int source_line)
Allocate a new function declaration AST node.
Definition: ast.c:240
ASTNode::attributes
Attribute * attributes
Attribute list (not a formal list because of the provided accessor methods)
Definition: ast.h:474
NodeList
Linked list of struct ASTNode* elements.
Definition: ast.h:498
LiteralNode
struct LiteralNode LiteralNode
AST literal expression structure.
LiteralNode_new_string
struct ASTNode * LiteralNode_new_string(const char *value, int source_line)
Allocate a new string literal expression AST node.
Definition: ast.c:378
Attribute::dtor
Destructor dtor
Pointer to destructor function that should be called to deallocate the attribute value (should be NUL...
Definition: ast.h:428
ContinueNode_new
struct ASTNode * ContinueNode_new(int source_line)
Allocate a new continue statement AST node.
Definition: ast.c:295
LocationNode::name
char name[256]
Location/variable name.
Definition: ast.h:348
FuncDeclNode::body
struct ASTNode * body
Function body block.
Definition: ast.h:142
FuncCallNode::arguments
struct NodeList * arguments
List of actual parameters/arguments.
Definition: ast.h:367
BlockNode_new
struct ASTNode * BlockNode_new(struct NodeList *vars, struct NodeList *stmts, int source_line)
Allocate a new block declaration AST node.
Definition: ast.c:250
ASTNode_get_attribute
void * ASTNode_get_attribute(ASTNode *node, const char *key)
Retrieve a particular attribute from a node.
Definition: ast.c:138
FuncDeclNode::name
char name[256]
Function name.
Definition: ast.h:139
VarDeclNode::name
char name[256]
Variable name.
Definition: ast.h:92
WhileLoopNode_new
struct ASTNode * WhileLoopNode_new(struct ASTNode *condition, struct ASTNode *body, int source_line)
Allocate a new while loop statement AST node.
Definition: ast.c:275
VarDeclNode
struct VarDeclNode VarDeclNode
AST variable structure.
ReturnNode
AST return statement structure.
Definition: ast.h:242
Parameter::name
char name[256]
Parameter formal name.
Definition: ast.h:115
WhileLoopNode::condition
struct ASTNode * condition
Guard condition (expression)
Definition: ast.h:223
Attribute::key
const char * key
Attribute key.
Definition: ast.h:424
ConditionalNode::condition
struct ASTNode * condition
Guard condition (expression)
Definition: ast.h:200
ConditionalNode
AST conditional structure.
Definition: ast.h:199
ProgramNode_new
struct ASTNode * ProgramNode_new(struct NodeList *vars, struct NodeList *funcs)
Allocate a new program AST node.
Definition: ast.c:222
LocationNode::index
struct ASTNode * index
Index expression (can be NULL for non-array locations)
Definition: ast.h:349
Attribute::value
void * value
Attribute value (integral value or pointer to heap)
Definition: ast.h:425
DecafType
DecafType
Valid Decaf types.
Definition: common.h:66
UnaryOpNode
struct UnaryOpNode UnaryOpNode
AST unary operation expression structure.
WhileLoopNode::body
struct ASTNode * body
Block to be executed while the condition is true.
Definition: ast.h:224
ReturnNode_new
struct ASTNode * ReturnNode_new(struct ASTNode *value, int source_line)
Allocate a new return statement AST node.
Definition: ast.c:283
LocationNode
AST location expression structure.
Definition: ast.h:347
BinaryOpNode::right
struct ASTNode * right
Right operand expression.
Definition: ast.h:293
ProgramNode::variables
struct NodeList * variables
List of global variable declarations.
Definition: ast.h:73
LiteralNode::integer
int integer
Integer value (if type is INT)
Definition: ast.h:386
ReturnNode
struct ReturnNode ReturnNode
AST return statement structure.
ASTNode_set_attribute
void ASTNode_set_attribute(ASTNode *node, const char *key, void *value, Destructor dtor)
Add or change an attribute for an AST node.
Definition: ast.c:70
NodeType_to_string
const char * NodeType_to_string(NodeType type)
Return a string representation of a node type.
Definition: ast.c:19
LiteralNode::type
DecafType type
Literal type (discriminator/tag for the anonymous union)
Definition: ast.h:384
BinaryOpNode
AST binary operation expression structure.
Definition: ast.h:290
LiteralNode_new_int
struct ASTNode * LiteralNode_new_int(int value, int source_line)
Allocate a new integer literal expression AST node.
Definition: ast.c:362
ASTNode::type
NodeType type
Node type (discriminator/tag for the anonymous union)
Definition: ast.h:472
AttributeValueDOTPrinter
void(* AttributeValueDOTPrinter)(void *, FILE *)
Function pointer used to store references to custom DOT output routines.
Definition: ast.h:18
ASTNode::source_line
int source_line
Source code line number.
Definition: ast.h:473
DECL_LIST_TYPE
#define DECL_LIST_TYPE(NAME, ELEMTYPE)
Declare a singly-linked list structure of the given type.
Definition: common.h:132
BlockNode::statements
struct NodeList * statements
List of statements in the block.
Definition: ast.h:163
FuncDeclNode::parameters
ParameterList * parameters
List of formal parameters.
Definition: ast.h:141
AssignmentNode
AST assignment structure.
Definition: ast.h:179
LiteralNode::boolean
bool boolean
Boolean value (if type is BOOL)
Definition: ast.h:387
ParameterList
Linked list of struct Parameter* elements.
Definition: ast.h:123
ReturnNode::value
struct ASTNode * value
Return value (can be NULL for void returns)
Definition: ast.h:243
Parameter::next
struct Parameter * next
Pointer to next parameter (if in a list)
Definition: ast.h:117
ProgramNode::functions
struct NodeList * functions
List of function declarations.
Definition: ast.h:74
BlockNode::variables
struct NodeList * variables
List of local variable declarations in the block.
Definition: ast.h:162
AssignmentNode
struct AssignmentNode AssignmentNode
AST assignment structure.
UnaryOpToString
const char * UnaryOpToString(UnaryOpType op)
Convert a UnaryOpType to a string.
Definition: ast.c:329
dummy_print
void dummy_print(void *, FILE *)
Fake "print" function that does nothing.
Definition: ast.c:3
ConditionalNode
struct ConditionalNode ConditionalNode
AST conditional structure.
ConditionalNode::else_block
struct ASTNode * else_block
Block to be executed if the condition is false (can be NULL if there is no else-block)
Definition: ast.h:202
ASTNode_set_printable_attribute
void ASTNode_set_printable_attribute(ASTNode *node, const char *key, void *value, AttributeValueDOTPrinter dot_printer, Destructor dtor)
Add or change a printable attribute for an AST node.
Definition: ast.c:80
Attribute::dot_printer
AttributeValueDOTPrinter dot_printer
Pointer to DOT-printing function (can be NULL if not printable)
Definition: ast.h:426
VarDeclNode::is_array
bool is_array
True if the variable is an array, false if it's a scalar.
Definition: ast.h:94
BinaryOpType
BinaryOpType
Binary operator.
Definition: ast.h:274
ASTNode_has_attribute
bool ASTNode_has_attribute(ASTNode *node, const char *key)
Check to see if a node has a particular attribute.
Definition: ast.c:120
BinaryOpNode
struct BinaryOpNode BinaryOpNode
AST binary operation expression structure.
ASTNode_new
ASTNode * ASTNode_new(NodeType type, int line)
Allocate a new AST node.
Definition: ast.c:59
VarDeclNode
AST variable structure.
Definition: ast.h:91
MAX_LINE_LEN
#define MAX_LINE_LEN
Maximum length (in characters) of any single line of input.
Definition: common.h:42
Attribute
struct Attribute Attribute
AST attribute (basically a key-value store for nodes)
BlockNode
AST block structure.
Definition: ast.h:161
ASTNode
struct ASTNode ASTNode
Main AST node structure.
VarDeclNode_new
struct ASTNode * VarDeclNode_new(const char *name, DecafType type, bool is_array, int array_length, int source_line)
Allocate a new variable declaration AST node.
Definition: ast.c:230
UnaryOpNode::child
struct ASTNode * child
Child operand expression.
Definition: ast.h:329
WhileLoopNode
AST while loop structure.
Definition: ast.h:222
Parameter::type
DecafType type
Parameter type.
Definition: ast.h:116
common.h
Includes, constants, declarations, and macros used across the compiler.
BinaryOpToString
const char * BinaryOpToString(BinaryOpType op)
Convert a BinaryOpType to a string.
Definition: ast.c:300
FuncCallNode::name
char name[256]
Function name.
Definition: ast.h:366
Attribute
AST attribute (basically a key-value store for nodes)
Definition: ast.h:422
VarDeclNode::array_length
int array_length
Length of array (should be 1 if not an array)
Definition: ast.h:95
MAX_ID_LEN
#define MAX_ID_LEN
Maximum length (in characters) of any identifier.
Definition: common.h:57
BreakNode_new
struct ASTNode * BreakNode_new(int source_line)
Allocate a new break statement AST node.
Definition: ast.c:290
ASTNode_set_int_attribute
void ASTNode_set_int_attribute(ASTNode *node, const char *key, int value)
Add or change an integer attribute for an AST node.
Definition: ast.c:75
ParameterList_add_new
void ParameterList_add_new(ParameterList *list, const char *name, DecafType type)
Allocate and add a new parameter to a list of parameters.
Definition: ast.c:50
FuncCallNode
struct FuncCallNode FuncCallNode
AST function call expression structure.
BinaryOpNode::left
struct ASTNode * left
Left operand expression.
Definition: ast.h:292
ConditionalNode_new
struct ASTNode * ConditionalNode_new(struct ASTNode *condition, struct ASTNode *if_block, struct ASTNode *else_block, int source_line)
Allocate a new conditional statement AST node.
Definition: ast.c:266
LiteralNode_new_bool
struct ASTNode * LiteralNode_new_bool(bool value, int source_line)
Allocate a new boolean literal expression AST node.
Definition: ast.c:370
FuncDeclNode
struct FuncDeclNode FuncDeclNode
AST function structure.
LocationNode_new
struct ASTNode * LocationNode_new(const char *name, struct ASTNode *index, int source_line)
Allocate a new location expression AST node.
Definition: ast.c:346
Parameter
struct Parameter Parameter
AST parameter (used in function declarations)
ProgramNode
AST root structure.
Definition: ast.h:72
ASTNode_free
void ASTNode_free(ASTNode *node)
Deallocate an AST node structure.
Definition: ast.c:152
Destructor
void(* Destructor)(void *)
Function pointer used to store references to custom "free" routines.
Definition: ast.h:23
UnaryOpType
UnaryOpType
Unary operator.
Definition: ast.h:312
Attribute::next
struct Attribute * next
Next attribute (if stored in a list)
Definition: ast.h:431
AssignmentNode::value
struct ASTNode * value
Right-hand side of assignment.
Definition: ast.h:181
VarDeclNode::type
DecafType type
Variable type.
Definition: ast.h:93
UnaryOpNode_new
struct ASTNode * UnaryOpNode_new(UnaryOpType operator, struct ASTNode *child, int source_line)
Allocate a new unary operation expression AST node.
Definition: ast.c:338
int_attr_print
void int_attr_print(void *, FILE *)
Simple "print" function that prints an attribute value as an integer.
Definition: ast.c:9