-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeclarations.cpp
More file actions
290 lines (243 loc) · 7.36 KB
/
Copy pathdeclarations.cpp
File metadata and controls
290 lines (243 loc) · 7.36 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//memory
#include <string>
#include <deque>
#include <stack>
#include <map>
//processing (gestion affichage)
#include <iostream>
#include <cstdio> //getchar
#include <iomanip>
#include <cmath>
//files
#include <fstream>
#include <vector>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#define DEFAULT_FOLDER "programFiles/"
#define PROGRAM_EXTENSION ".choco"
#define COMMAND_EXTENSION ".command"
#define COMPILED_EXTENSION ".chocapic"
using namespace std;
/********************************************************/
/* PARTIE I : ENUMERATIONS */
/********************************************************/
enum class valType {//fixe types
_void_,
_bool_,
_int_,
_double_,
_string_
};
enum class operation {//fixe operations
_plus_,
_moins_,
_fois_,
_divisePar_
};
enum class comparaison {//fixe comparaison
_and_,
_or_,
_equiv_,
_diff_,
_inferieur_,
_superieur_,
_inf_egal_,
_sup_egal_
};
enum class tabAction {//fixe operations
_empile_size_,
_empile_case_,
_create_,
_add_,
_update_,
_remove_,
_erase_
};
enum class command {
//MEMOIRE
_ENTER_BLOCK_,
_EXIT_BLOCK_,
//EMPILEMENT
_EMPILE_VALUE_,
_EMPILE_VARIABLE_,
_EMPILE_TABLE_SIZE_,
_EMPILE_TABLE_ELEMENT_,
//OPERATIONS (var to var)
_PLUS_CREMENT_,
_MOINS_CREMENT_,
_FOIS_CREMENT_,
_DIVISE_CREMENT_,
_PLUS_,
_MOINS_,
_FOIS_,
_DIVISE_PAR_,
//COMPARAISON
_AND_,
_OR_,
_EQUIV_,
_DIFF_,
_SUPERIEUR_,
_INFERIEUR_,
_SUP_EGAL_,
_INF_EGAL_,
//SAUTS (conditions, boucles, fonctions)
_GOTO_,
_GOTO_TEST_,
//VARIABLES
_CREATE_VARIABLE_,
_UPDATE_VARIABLE_,
//TABLEAUX
_CREATE_TABLE_,
_ADD_TABLE_ELEMENT_,
_UPDATE_TABLE_ELEMENT_,
_REMOVE_TABLE_ELEMENT_,
_CLEAR_TABLE_,
//FONCTIONS
_ENTER_FUNCTION_,
_EXIT_FUNCTION_,
_END_FUNCTION_,
_CREATE_FUNCTION_,
_CALL_FUNCTION_,
//ENTREE SORTIE
_WRITE_,
_STOP_,
_READ_,
_DELAY_
};
enum class errorCode {
folderCreationFailed,
unknowCommand,
conversionType,
unknowVariable,
alreadyUseVariable,
emptyExecutionStack,
alreadyDeclaredFunction,
unknowFunction,
notEnoughArgument,
tooMuchArgument,
missingReturn,
unsupportedOperation,
unknowArray,
alreadyUseArray
};
/********************************************************/
/* PARTIE II : TYPES PERSONNALISES */
/********************************************************/
typedef struct {//initialiser dans ordre de déclaration
valType type = valType::_int_;
int tabPos = -1;//valeur par defaut : flag d'invalidation
} valAccess;
typedef struct {//stockage tableau a part : doit preserver valeur ajoutee dans couche memoire differente de celle supportant la declaration
unsigned int memoryLayer = 0;
valType type = valType::_int_;
deque<int> valuesPos;
} tabAccess;
typedef struct {
unsigned int boolListSize = 0;
unsigned int intListSize = 0;
unsigned int doubleListSize = 0;
unsigned int stringListSize = 0;
} memoryState;
typedef struct {//initialiser dans ordre de déclaration
valType type = valType::_int_;
int intVal = -1;
double doubleVal = -1;
string stringVal = "";
} valInstruct;
typedef pair<string,valType> param;
typedef struct {
int refInstruct = -1;
valType returnType = valType::_int_;
deque<param> listParam;
} functionAccess;
typedef struct {
string name;
unsigned int returnAdress;
//references locales
map<string, valAccess> variables;
map<string, tabAccess> tableaux;
} functionCall;
typedef struct {
//valeurs simples : pile, variables
deque<bool> boolList; //intene
deque<int> intList;
deque<double> doubleList;
deque<string> stringList;
//valeurs multiples : tableaux
deque<int> intArray;
deque<double> doubleArray;
deque<string> stringArray;
//couche memoire
stack<memoryState> memoryLayer;
//references globales
map<string, functionAccess> fonctions;
//piles d'execution
stack<valAccess> executionPile;
stack<functionCall> currentExecution;
//instructions
unsigned int indexInstruction = 0;// compteur instruction
//erreurs
map<errorCode, string> errorMessage = { //peut etre passe en parametres
{errorCode::folderCreationFailed, "[SYSTEME] Echec de création du dossier"},
{errorCode::emptyExecutionStack, "[EXECUTION] pile vide"},
{errorCode::conversionType, "[TYPE] types incompatibles - échec de conversion"},
{errorCode::unknowCommand, "[EXECUTION] commande inconnue"},
{errorCode::unsupportedOperation, "[EXECUTION] operation non supportée"},
{errorCode::unknowVariable, "[VARIABLE] nom de variable inconnu"},
{errorCode::unknowArray, "[VARIABLE] nom de liste inconnu"},
{errorCode::unknowFunction, "[FONCTION] nom de fonction inconnu"},
{errorCode::alreadyUseVariable, "[VARIABLE] nom de variable déjà en utilisation"},
{errorCode::alreadyUseArray, "[VARIABLE] nom de liste déjà en utilisation"},
{errorCode::alreadyDeclaredFunction,"[FONCTION] nom de fonction déjà utilisé"},
{errorCode::notEnoughArgument, "[FONCTION] pas assez de valeurs en paramètres"},
{errorCode::tooMuchArgument, "[FONCTION] trop de valeurs en paramètres"},
{errorCode::missingReturn, "[FONCTION] valeur de retour attendue"}
};
} globalVariables;
typedef pair<command, valInstruct> instruction;
typedef void (*functionPointer)(valInstruct& instructContent, globalVariables& allVariables);//necessaire pour map de commandes - fonctions lambda
/********************************************************/
/* PARTIE III : VARIABLE GLOBALE OBLIGATOIRE (BISON) */
/********************************************************/
deque<instruction> instructionList;//necessairement global
/********************************************************/
/* PARTIE IV : PROTOTYPES */
/********************************************************/
// Memory
// Part 1
valAccess addVal(globalVariables& allVariables, valInstruct instructContent);
valAccess addVar(globalVariables& allVariables, valInstruct instructContent);
// Part 2
void delVal(globalVariables& allVariables, valAccess val);
void delVar(globalVariables& allVariables, string name);
void delTabVal(globalVariables& allVariables, string tabName, int tabCase);
void delTab(globalVariables& allVariables, string tabName);
// Part 3
void enterMemoryLayer(globalVariables& allVariables);
void exitMemoryLayer(globalVariables& allVariables);
// Utils
// Part 1
void printVal(globalVariables& allVariables, string beginMessage, valAccess val, string endMessage = "");
void pauseProcess(string message = "");
void replaceString(string& subject, const string& search, const string& replace);
void error(globalVariables& allVariables, errorCode cause);
valAccess depiler(globalVariables& allVariables);
// Part 2
valAccess castVal(globalVariables& allVariables, valAccess value, valType cast, bool isVar = 0);
void executeOperation(globalVariables& allVariables, operation operation);
void executeComparaison(globalVariables& allVariables, comparaison comparaison);
void executeCrement(globalVariables& allVariables, string varName, operation operation);
// Part 3
void executeTabAction(globalVariables& allVariables, instruction& instructContent, tabAction action);
// Processing
// Part 1
void addInstruct(command command);
void addInstruct(command command, int intValue);
void addInstruct(command command, double doubleValue);
void addInstruct(command command, string stringValue);
//const map<command, functionPointer> executeCommand;
// Part 2
void displayGeneratedProgram(globalVariables& allVariables);
void saveCommandProgramFile(string folderName, string programName);