-
Notifications
You must be signed in to change notification settings - Fork 0
/
ir.h
209 lines (186 loc) · 4.83 KB
/
ir.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
/*
* Project: IFJ16, a programming language interpreter
* FIT VUT Brno
* Authors: xzaryb00 - Zarybnický Jakub
* xtamas01 - Tamaškovič Marek
* xvasko12 - Vaško Martin
* xvasko14 - Vaško Michal
* xzales12 - Záleský Jiří
*/
#ifndef IFJ_IR_H
#define IFJ_IR_H
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "error.h"
#include "int_memory_management.h"
typedef enum {
T_STRING,
T_INTEGER,
T_DOUBLE,
T_BOOLEAN,
T_VOID,
} ValueType;
typedef enum {
E_FUNCALL,
E_VALUE,
E_REFERENCE,
E_BINARY,
E_UNARY,
} ExpressionType;
typedef enum {
U_ID,
U_PREINC,
U_POSTINC,
U_PREDEC,
U_POSTDEC,
U_NOT,
U_NEG,
} UnaryOperation;
typedef enum {
EB_EQUAL,
EB_NOT_EQUAL,
EB_LESS,
EB_LESS_EQUAL,
EB_GREATER,
EB_GREATER_EQUAL,
EB_MULTIPLY,
EB_DIVIDE,
EB_ADD,
EB_SUBTRACT,
EB_AND,
EB_OR,
} BinaryOperation;
typedef enum {
C_DECLARE,
C_DEFINE,
C_ASSIGN,
C_BLOCK,
C_IF,
C_WHILE,
C_DO_WHILE,
C_FOR,
C_EXPRESSION,
C_CONTINUE,
C_BREAK,
C_RETURN,
} CommandType;
typedef struct tValue {
ValueType type;
bool undefined;
union {
int integer;
double dbl;
bool boolean;
char *str;
} data;
} Value;
/* to shorten assigns or comparision data content of value */
#define I(x) (x)->data.integer
#define D(x) (x)->data.dbl
#define B(x) (x)->data.boolean
#define S(x) (x)->data.str
#define DVAL(x) \
((x)->type == T_DOUBLE ? D(x) : \
(x)->type == T_INTEGER ? (double) I(x) : \
0.0)
typedef struct tExpression {
ExpressionType type;
struct tExpression *next; //for argument lists
union {
struct {
char *name;
int argCount;
struct tExpression *argHead;
} funcall;
Value *value;
char *reference;
struct {
UnaryOperation op;
struct tExpression *e;
} unary;
struct {
BinaryOperation op;
struct tExpression *left;
struct tExpression *right;
} binary;
} data;
} Expression;
typedef struct tDeclaration {
ValueType type;
char *name;
struct tDeclaration *next; //for argument lists
} Declaration;
struct tCommand;
typedef struct tBlock {
struct tCommand *head;
struct tCommand *tail;
} Block;
typedef struct tCommand {
CommandType type;
struct tCommand *next;
union {
Declaration declare;
struct { Declaration declaration; Expression *expr; } define;
struct { char *name; Expression *expr; } assign;
Block block;
struct { Expression *cond; Block thenBlock; Block elseBlock; } ifC;
struct { Expression *cond; Block bodyBlock; } whileC;
struct { Block bodyBlock; Expression *cond; } doWhileC;
struct {
Declaration var; Expression *initial;
Expression *cond; struct tCommand *iter;
Block bodyBlock;
} forC;
Expression *expr; //C_EXPRESSION + C_RETURN
} data;
} Command;
typedef struct {
char *name;
ValueType returnType;
int argCount;
Declaration *argHead;
Block body;
bool builtin;
} Function;
Value *createValue(ValueType);
void freeValue(Value *);
void printValue(Value *);
Expression *createExpression(ExpressionType);
void freeExpression(Expression *);
void printExpression(Expression *);
Declaration *createDeclaration(ValueType, char *);
void freeDeclaration(Declaration *);
void printDeclaration(Declaration *);
void appendToBlock(Block *, Command *);
void freeBlock(Block);
void printBlock(Block *);
Command *createCommandDeclare(Declaration);
Command *createCommandDefine(Declaration, Expression *);
Command *createCommandAssign(char *, Expression *);
Command *createCommandBlock();
Command *createCommandIf(Expression *);
Command *createCommandWhile(Expression *);
Command *createCommandDoWhile();
Command *createCommandFor(Declaration, Expression *, Expression *, Command *);
Command *createCommandExpression(Expression *);
Command *createCommandContinue();
Command *createCommandBreak();
Command *createCommandReturn(Expression *);
void freeCommand(Command *);
void printCommand(Command *);
Function *createFunction(char *, ValueType, int, Declaration *);
void freeFunction(Function *);
void printFunction(Function *);
void traverseCommands(Function *, void (*f)(Function *, Command *), void (*onForExit)(Command *));
char *showValueType(ValueType);
char *showExpressionType(ExpressionType);
char *showUnaryOperation(UnaryOperation);
char *showBinaryOperation(BinaryOperation);
char *showCommandType(CommandType);
char *getClassName(char *);
char *strdup_(const char *);
Value *copyValue (Value *);
double hexToDbl(char *str);
#endif /* IFJ_IR_H */