-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathast.h
153 lines (125 loc) · 2.84 KB
/
ast.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
// AST definitions
#ifndef __ast_h__
#define __ast_h__
// AST for expressions
struct _Expr
{
int negative; // number of MINUS's ('-') preceding the expression
enum
{
E_VAR,
E_INTEGER,
E_OPERATION
} kind;
union
{
char* var; // for integer variables
int value; // for integer values
struct
{
int operator; // PLUS, MINUS, etc
struct _Expr* left;
struct _Expr* right;
} op; // for binary expressions
} attr;
};
typedef struct _Expr Expr; // Convenience typedef
// AST for boolean expressions
struct _BoolExpr
{
int negation; // number of NOT's ('!') preceding the boolean expression
enum
{
BE_VAR,
BE_VALUE,
BE_OPERATION,
BE_LOGIC
} kind;
union
{
char* var; // for boolean variables
int value; // for true(1)/false(0) values
struct
{
int operator; // EQ, NE, LT, etc
Expr* left;
Expr* right;
} op; // to compare expressions
struct
{
int operator; // &, |
struct _BoolExpr* left;
struct _BoolExpr* right;
} logic; // for logic operations
} attr;
};
typedef struct _BoolExpr BoolExpr; // Convenience typedef
// AST for commands
struct _Cmd
{
struct _Cmd* nextcmd; // command/sequence of commands that come after this one (if any)
enum
{
C_ASSIGN_EXPR,
C_ASSIGN_BOOL,
C_IF,
C_IF_ELSE,
C_WHILE,
C_PRINT,
C_READ
} kind;
union
{
struct
{
char* var;
Expr* value;
} assignexpr; // for integer assignments
struct
{
char* var;
BoolExpr* value;
} assignbool; // for boolean assignments
struct
{
BoolExpr* cond;
struct _Cmd* cmd;
} ifthen; // for if then conditional commands
struct
{
BoolExpr* cond;
struct _Cmd* cmd1;
struct _Cmd* cmd2;
} ifthenelse; // for if then else conditional commands
struct
{
BoolExpr* cond;
struct _Cmd* cmd;
} whileloop; // for while loops
struct
{
char* var;
} println; // for the println function
struct
{
char* var;
} readline; // for the read_line function
} attr;
};
typedef struct _Cmd Cmd; // Convenience typedef
// Constructor functions (implementation ast.c)
Expr* ast_var(char var[]);
Expr* ast_integer(int v);
Expr* ast_operation(int operator, Expr* left, Expr* right);
BoolExpr* ast_boolVar(char var[]);
BoolExpr* ast_boolVal(int v);
BoolExpr* ast_boolExpr(int operator, Expr* left, Expr* right);
BoolExpr* ast_logicExpr(int operator, BoolExpr* left, BoolExpr* right);
Cmd* ast_assign_expr(char var[], Expr* v);
Cmd* ast_assign_boolexpr(char var[], BoolExpr* v);
Cmd* ast_ifthen(BoolExpr* cond, Cmd* cmd);
Cmd* ast_ifthenelse(BoolExpr* cond, Cmd* cmd1, Cmd* cmd2);
Cmd* ast_whileloop(BoolExpr* cond, Cmd* cmd);
Cmd* ast_println(char var[]);
Cmd* ast_readline(char var[]);
#endif