aboutsummaryrefslogtreecommitdiff
path: root/include/ast.h
blob: 12dce55ff7a3860c61da456bb8e4a36c661a8c7f (plain)
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
#ifndef AST_H
#define AST_H


/* AST Node Types */
typedef enum {
  NT_SKIP,     /* nop */
  NT_ASSIGN,   /* assignment */
  NT_SEQ,      /* sequential composition */
  NT_IF,       /* if-then-else block */
  NT_WHILE,    /* while loop */
  NT_INT,      /* integer constant */
  NT_VAR,      /* integer variable */
  NT_AOP,      /* arithmetic operation */
  NT_BOP,      /* boolean operation */
  NT_NOT,      /* boolean not */
  NT_ROP,      /* relational operation */
  NT_LET,      /* let-in-end block (i.e. local variable) */
  NT_PROCDECL, /* procedure declaration */
  NT_PROCCALL  /* procedure call */
} NodeType;

/* Arithmetic Operations */
typedef enum { 
  AOP_ADD, /* addition */
  AOP_SUB, /* subtraction */
  AOP_MUL  /* multiplication */
} AOp;

/* Boolean Operations */
typedef enum { 
  BOP_AND, /* conjunction */
  BOP_OR   /* disjunction */
} BOp;

/* Relational Operations */
typedef enum { 
  ROP_EQ, /* equals */
  ROP_NE, /* not equals */
  ROP_LT, /* less than */
  ROP_LE, /* less or equals */
  ROP_GT, /* greater than */
  ROP_GE  /* greater or equals */
} ROp;

/* AST Node */
typedef struct ASTNode {
  NodeType type;
  union { /* specific data for each node type */
    struct { struct ASTNode *var, *aexp; } d_assign;
    struct { struct ASTNode *stm1, *stm2; } d_seq;
    struct { struct ASTNode *bexp, *stm1, *stm2; } d_if;
    struct { struct ASTNode *bexp, *stm; } d_while;
    struct { int val; } d_int ;
    struct { char *name; } d_var;
    struct { AOp aop; struct ASTNode *aexp1, *aexp2; } d_aop;
    struct { BOp bop; struct ASTNode *bexp1, *bexp2; } d_bop;
    struct { struct ASTNode *bexp; } d_not;
    struct { ROp rop; struct ASTNode *aexp1, *aexp2; } d_rop;
    struct { struct ASTNode *var, *aexp, *stm; } d_let;
    struct { char *name; struct ASTNodeList *args,  *vargs; struct ASTNode *stm; } d_procdecl;
    struct { char *name; struct ASTNodeList *args,  *vargs; } d_proccall;
  } u;
} ASTNode;

/* AST Node List (i.e. linked list of AST Nodes) */
typedef struct ASTNodeList {
  ASTNode *node;
  struct ASTNodeList *next;
} ASTNodeList;


/* 
  Creates new node of specific type.
    The caller is responsible for freeing the top-most node, by calling ast_free.
    Any strings passed to the node are copied (i.e. strdup) and will also automatically be freed.
*/
ASTNode *ast_skip(void);
ASTNode *ast_assign(ASTNode *var, ASTNode *aexp);
ASTNode *ast_seq(ASTNode *stm1, ASTNode *stm2);
ASTNode *ast_if(ASTNode *bexp, ASTNode *stm1, ASTNode *stm2);
ASTNode *ast_while(ASTNode *bexp, ASTNode *stm);
ASTNode *ast_int(int val);
ASTNode *ast_var(char *name);
ASTNode *ast_aop(AOp aop, ASTNode *aexp1, ASTNode *aexp2);
ASTNode *ast_bop(BOp bop, ASTNode *bexp1, ASTNode *bexp2);
ASTNode *ast_not(ASTNode *bexp);
ASTNode *ast_rop(ROp rop, ASTNode *aexp1, ASTNode *aexp2);
ASTNode *ast_let(ASTNode *var, ASTNode *aexp, ASTNode *stm);
ASTNode *ast_procdecl(const char *name, ASTNodeList *args, ASTNodeList *vargs, ASTNode *stm);
ASTNode *ast_proccall(const char *name, ASTNodeList *args, ASTNodeList *vargs);

/* 
  Creates node list, 
    Must be used in conjunction with a node that takes a list (i.e. ast_procdecl).
    Will automatically be freed when the parent node is freed.
  */
ASTNodeList *ast_node_list(ASTNode *node, ASTNodeList *list);

/* 
  Creates a deep copy of a node and it's sub nodes (recursively).
    caller is responsible for freeing top-most node.
*/
ASTNode *ast_clone(ASTNode *node);

/* Recursively frees node and all sub nodes */
void ast_free(ASTNode *node);


#endif