-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogramgraph.h
More file actions
268 lines (231 loc) · 4.78 KB
/
programgraph.h
File metadata and controls
268 lines (231 loc) · 4.78 KB
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
/*programgraph.h*/
//
// Program graph data structure for nuPython.
//
#pragma once
#include <stdbool.h> // true, false
#include "tokenqueue.h"
//
// A nuPython program is 1 or more statements:
//
//
// nuPython statement types:
//
enum STMT_TYPES
{
STMT_ASSIGNMENT = 0,
STMT_FUNCTION_CALL,
STMT_IF_THEN_ELSE,
STMT_WHILE_LOOP,
STMT_PASS
};
struct STMT
{
//
// what kind of stmt do we have?
//
int stmt_type; // enum STMT_TYPES
int line; // what line # does it start on?
//
// pointer to that stmt struct:
//
union
{
struct STMT_ASSIGNMENT* assignment;
struct STMT_FUNCTION_CALL* function_call;
struct STMT_IF_THEN_ELSE* if_then_else;
struct STMT_WHILE_LOOP* while_loop;
struct STMT_PASS* pass;
} types;
};
struct STMT_ASSIGNMENT
{
//
// Examples: x = 123
// *p = x + y
//
char* var_name;
bool isPtrDeref;
struct VALUE* rhs; // rhs = "right-hand side"
struct STMT* next_stmt;
};
struct STMT_FUNCTION_CALL
{
//
// Examples: print()
// print("the output is")
//
char* function_name;
struct ELEMENT* parameter; // optional => could be NULL
struct STMT* next_stmt;
};
struct STMT_IF_THEN_ELSE
{
//
// Example: if x<0:
// { ... }
// elif x==0:
// { ... }
// else:
// { ... }
//
struct EXPR* condition;
struct STMT* true_path; // next stmt if the condition is true
struct STMT* false_path; // next stmt if the condition is false
//
// NOTE: the next stmt only has meaning for the initial if
// stmt. For any nested elif statements, those will have
// NULL for next_stmt. We need to know which stmt follows
// an IF statement for printing purposes, and freeing memory.
//
struct STMT* next_stmt; // next stmt following if stmt
};
struct STMT_WHILE_LOOP
{
//
// Example: while x<10:
// { ... }
//
struct EXPR* condition;
struct STMT* loop_body; // loop body if the condition is true
struct STMT* next_stmt; // next stmt after the loop is over
};
struct STMT_PASS
{
//
// Example: pass
//
struct STMT* next_stmt;
};
//
// nuPython values / expressions:
//
enum VALUE_TYPES
{
VALUE_FUNCTION_CALL = 0,
VALUE_EXPR
};
struct VALUE
{
//
// what kind of value do we have?
//
int value_type; // enum VALUE_TYPES
//
// pointer to that value struct:
//
union
{
struct FUNCTION_CALL* function_call;
struct EXPR* expr;
} types;
};
struct FUNCTION_CALL
{
char* function_name;
struct ELEMENT* parameter; // optional => could be NULL
};
struct EXPR
{
struct UNARY_EXPR* lhs; // lhs = "left-hand side"
bool isBinaryExpr; // true => we have operator_type and rhs
int operator_type; // enum OPERATORS
struct UNARY_EXPR* rhs; // optional => could be NULL
};
enum UNARY_EXPR_TYPES
{
UNARY_PTR_DEREF = 0,
UNARY_ADDRESS_OF,
UNARY_PLUS,
UNARY_MINUS,
UNARY_ELEMENT
};
struct UNARY_EXPR
{
//
// what kind of unary expression do we have?
//
int expr_type; // enum UNARY_EXPR_TYPES
//
// underlying element (identifier or literal):
//
struct ELEMENT* element;
};
//
// nuPython elements
//
enum ELEMENT_TYPES
{
ELEMENT_IDENTIFIER = 0,
ELEMENT_INT_LITERAL,
ELEMENT_REAL_LITERAL,
ELEMENT_STR_LITERAL,
ELEMENT_TRUE,
ELEMENT_FALSE,
ELEMENT_NONE
};
struct ELEMENT
{
//
// what kind of element do we have?
//
int element_type; // enum ELEMENT_TYPES
//
// underlying element (identifier or literal):
//
char* element_value; // e.g. "x" or "123" or "3.14" or "this is a string"
};
//
// nuPython operators
//
enum OPERATORS
{
OPERATOR_PLUS = 0,
OPERATOR_MINUS,
OPERATOR_ASTERISK,
OPERATOR_POWER,
OPERATOR_MOD,
OPERATOR_DIV,
OPERATOR_EQUAL,
OPERATOR_NOT_EQUAL,
OPERATOR_LT,
OPERATOR_LTE,
OPERATOR_GT,
OPERATOR_GTE,
OPERATOR_IS,
OPERATOR_IN,
OPERATOR_NO_OP // when there is no operator_type
};
//
// Public functions:
//
//
// programgraph_build
//
// Given a legal nuPython program in the form of a list
// of tokens, builds and returns a program graph
// representing the nuPython program. This is easier
// to work with than the raw tokens.
//
// Returns NULL if an error occurs and the program graph
// could not be built.
//
// NOTE: the program graph may contain semantic errors,
// e.g. type errors or calls to functions that don't exist.
// Semantic errors need to be detected during execution
// (it could also be done using a pre-execution pass
// through the graph).
//
struct STMT* programgraph_build(struct TokenQueue* tokens);
//
// programgraph_destroy
//
// Frees all the memory with in given program graph.
//
void programgraph_destroy(struct STMT* program);
//
// programgraph_print
//
// Prints the contents of the program graph to the console.
//
void programgraph_print(struct STMT* program);