-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.c
446 lines (425 loc) · 16.6 KB
/
compiler.c
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#include <stdio.h>
#include <stdint.h>
#include <time.h>
#include <stdlib.h>
// START DATA SECTION
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
uint16_t currVariable = 0;
typedef struct variable{
uint32_t id;
int32_t value;
} colourVariable_t;
enum RESERVED_COLOURS{
INC=0x00a249a4,
CBRACKET=0,
COMMA=0x00c3c3c3,
PRINTC=0x00b7e61d,
PRINTI=0x007cd6d6,
DEC=0x004a90e2,
MUL=0x00ec277c,
DIV=0x00fffa4f,
JMP=0x006e120e,
IFE=0x0000ff12,
IFL=0x009aa6ad,
IFG=0x00397463,
RAN=0x00194d33,
USRI=0x00ad456e,
USRC=0x00887d3e
};
char ERRORCODES[][255] = {"Reserved Colour Found Outside Of Function Call","Function Expects 1 Or 2 Arguments","Function Arguments Takes Up More Than 1 Pixel","Too Many Arguments","Variable Referenced As Argument Not Found","Function Expects 1 Argument","Function Expects 1 Or 0 Arguments","Jumped Out Of Bounds","If Functions Expect 4 Arguments","Function Expects 3 Arguments"};
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// END DATA SECTION
// START BASIC TOOLS
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#define _WARN(warning) printf("\e[0;33mWarning: %s\e[0m\n",warning);
#define _ERROR(error,exit) printf("\e[0;31mError: %s\e[0m\n",error); if(exit) return 1
#define _READTOVAR(var,where,size,file) fseek(file,where,SEEK_SET); fread(&var,size,1,file)
#define _READTOARRAY(var,where,size,length,file) fseek(file,where,SEEK_SET); fread(&var,size,length,file)
#define _GETRED(rgb) rgb>>16
#define _GETGREEN(rgb) (rgb&65280)>>8
#define _GETBLUE(rgb) (rgb&255)
#define _isReserved(rgb) ((rgb == INC || rgb == CBRACKET || rgb == COMMA || rgb == PRINTC || rgb == PRINTI || rgb == DEC || rgb == MUL || rgb == DIV || rgb == JMP || rgb == IFE || rgb == IFL || rgb == IFG || rgb == RAN || rgb == USRI || rgb == USRC) ? 1 : 0)
#define addVariable(rgb) (variables[currVariable++] = (colourVariable_t){rgb,1})
#define getPixel(data,pos) (data[pos*3+2]<<16)+(data[pos*3+1]<<8)+(data[pos*3])
char alreadyVariable(colourVariable_t vars[], uint32_t id){
for(int i = 0; i<currVariable; i++){
if(vars[i].id == id)
return i;
}
return -1;
}
int getRandNum(int start, int end){
int out = rand();
return (out % (end-start) + start);
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// END BASIC TOOLS
// START FUNCTION DECLERATIONS
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
int getPixelPosFromInstr(uint8_t data[], int length, int instr);
int iterateThrough(uint8_t data[], int length);
int getAmountOfVariables(uint8_t data[], int length);
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// END FUNCTION DECLERATIONS
int main(int argc, char **argv){
srand((int)time(0));
if(argc < 2){
_ERROR("Missing Argument", 1);
}
FILE *fp = fopen(argv[1],"rb");
if(!fp){
_ERROR("Failed to open file", 1);
}
uint8_t type = 0;
_READTOVAR(type,0,2,fp);
if(type != 0x42 && type != 0x4d){
_ERROR("Not a BMP",1);
}
uint32_t sX = 0,sY = 0,off = 0;
_READTOVAR(sX,18,4,fp);
_READTOVAR(sY,22,4,fp);
_READTOVAR(off,10,4,fp);
if(sY > 1){
_WARN("Image contains y component\n\tthis will be ignored and ONLY the first row will be read");
}
uint32_t rowSize = ((4-((3*sX)%4))%4)+sX*3;
uint8_t data[sX*3];
_READTOARRAY(data,off+(rowSize*(sY-1)),1,sX*3,fp);
int result = iterateThrough(data,sX);
fclose(fp);
if(result!=-1){ // There was a zero and an errorcode was returned from _ERRORCODES
_ERROR(ERRORCODES[result],0);
}
}
// START FUNCTION INITIALISATIONS
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
int getPixelPosFromInstr(uint8_t data[], int length, int instr){
int currInstr = 0;
int currPix = 0;
uint32_t currColr = 0;
while(currPix < length){
currColr = (getPixel(data,currPix));
currInstr++;
if(currInstr == instr){
return currPix;
}
if(currColr == 0x00000000){ // function call
currPix++;
currColr = getPixel(data,currPix);
while(currColr != 0x00000000){
currPix++;
currColr = getPixel(data,currPix);
}
currPix++;
} else { // declaration
while(currColr == (getPixel(data,currPix))){
currPix++;
}
}
}
return -1; // handle error
}
int32_t getVariableValue(colourVariable_t variables[], uint32_t colour){
for(int i = 0; i < currVariable; i++){
if(variables[i].id == colour)
return variables[i].value;
}
return 0;
}
int _in(colourVariable_t variables[], uint32_t colour){
for(int i = 0; i < currVariable; i++){
if(variables[i].id==colour)
return 1;
}
return 0;
}
int iterateThrough(uint8_t data[],int length){
colourVariable_t variables[getAmountOfVariables(data, length)]; // getAmount should keep track of all the variables and increment, or just estimate idgaf
int curr = 0;
char c = 0;
while(curr < length-1 && getPixel(data,curr)!=0xffffff){
uint32_t currPixel = getPixel(data,curr);
switch(currPixel){
case CBRACKET:{
curr++;
uint32_t args[4];
int argsCount = 0;
for(;!_isReserved(getPixel(data,curr));curr+=2){
if(argsCount+1 > 4)
return 3;
args[argsCount++] = getPixel(data,curr);
}
int funcLength = 1; // <-- for functions that use the length of the function as an argument
uint32_t function = getPixel(data,curr);
if(curr+1 < length-1){
for(++curr;getPixel(data,curr) == function;++curr){
funcLength++;
}
}
switch(function){
case USRC:{
if(argsCount == 1){
if(!_in(variables,args[0]))
return 4;
for(int i = 0; i < currVariable; i++){
if(variables[i].id == args[0]){
scanf("%lc",&(variables[i].value));
while ( (c = getchar()) != '\n' && c != EOF);
}
}
} else
return 5;
break;
}
case USRI:{
if(argsCount == 1){
if(!_in(variables,args[0]))
return 4;
for(int i = 0; i < currVariable; i++){
if(variables[i].id == args[0]){
scanf("%d",&(variables[i].value));
while ( (c = getchar()) != '\n' && c != EOF);
}
}
} else
return 5;
break;
}
case RAN:{
if(argsCount == 3){
if(!_in(variables,args[0]) || !_in(variables,args[1]) || !_in(variables,args[2]))
return 4;
if(getVariableValue(variables,args[1])<getVariableValue(variables,args[2])){
for(int i = 0; i < currVariable; i++){
if(variables[i].id == args[0])
variables[i].value = getRandNum(getVariableValue(variables,args[1]),getVariableValue(variables,args[2]));
}
} else {
for(int i = 0; i < currVariable; i++){
if(variables[i].id == args[0])
variables[i].value = getRandNum(getVariableValue(variables,args[2]),getVariableValue(variables,args[1]));
}
}
} else
return 9;
break;
}
case IFL:{
if(argsCount == 4){
if(!_in(variables,args[0]) || !_in(variables,args[1]) || !_in(variables,args[2]) || !_in(variables,args[3]))
return 4;
if(getVariableValue(variables,args[0]) < getVariableValue(variables,args[1])){
curr = getPixelPosFromInstr(data, length, getVariableValue(variables,args[2]))-1;
if(curr == -1)
return 7;
} else {
curr = getPixelPosFromInstr(data, length, getVariableValue(variables,args[3]))-1;
if(curr == -1)
return 7;
}
} else
return 8;
break;
}
case IFG:{
if(argsCount == 4){
if(!_in(variables,args[0]) || !_in(variables,args[1]) || !_in(variables,args[2]) || !_in(variables,args[3]))
return 4;
if(getVariableValue(variables,args[0]) > getVariableValue(variables,args[1])){
curr = getPixelPosFromInstr(data, length, getVariableValue(variables,args[3]))-1;
if(curr == -2)
return 7;
} else {
curr = getPixelPosFromInstr(data, length, getVariableValue(variables,args[3]))-1;
if (curr == -2)
return 7;
}
} else
return 8;
break;
}
case IFE:{
if(argsCount == 4){
if(!_in(variables,args[0]) || !_in(variables,args[1]) || !_in(variables,args[2]) || !_in(variables,args[3]))
return 4;
if(getVariableValue(variables,args[0]) == getVariableValue(variables,args[1])){
curr = getPixelPosFromInstr(data, length, getVariableValue(variables,args[3]))-1;
if(curr == -2)
return 7;
} else {
curr = getPixelPosFromInstr(data, length, getVariableValue(variables,args[3]))-1;
if(curr==-2)
return 7;
}
} else
return 8;
break;
}
case JMP:{
if(argsCount == 1){
if(!_in(variables,args[0]))
return 4;
for(int i = 0; i < currVariable; i++){
if(variables[i].id == args[0])
curr = getPixelPosFromInstr(data, length, getVariableValue(variables,args[3]))-1;
if(curr == -2)
return 7;
}
} else if(argsCount == 0){
curr = getPixelPosFromInstr(data, length, funcLength)-1;
if(curr == -2)
return 7;
} else {
return 6;
}
break;
}
case PRINTI:{
if(argsCount != 1)
return 5;
if(!_in(variables,args[0]))
return 4;
for(int i = 0; i < currVariable; i++){
if(variables[i].id == args[0])
printf("%d",getVariableValue(variables,args[0]));
}
break;
}
case PRINTC:{
if(argsCount != 1)
return 5;
if(!_in(variables,args[0]))
return 4;
for(int i = 0; i < currVariable; i++){
if(variables[i].id == args[0])
printf("%c",(char)getVariableValue(variables,args[0]));
}
break;
}
case INC:{
if(argsCount == 1){
if(!_in(variables,args[0]))
return 4;
for(int i = 0; i < currVariable; i++){
if(variables[i].id == args[0])
variables[i].value = getVariableValue(variables,args[0])+funcLength;
}
} else if(argsCount == 2){
if(!_in(variables,args[0]) || !_in(variables,args[1]))
return 4;
for(int i = 0; i < currVariable; i++){
if(variables[i].id == args[0])
variables[i].value = getVariableValue(variables,args[0])+getVariableValue(variables,args[1]);
}
} else {
return 1;
}
break;
}
case DEC:{
if(argsCount == 1){
if(!_in(variables,args[0]))
return 4;
for(int i = 0; i < currVariable; i++){
if(variables[i].id == args[0])
variables[i].value = getVariableValue(variables,args[0])-funcLength;
}
} else if(argsCount == 2){
if(!_in(variables,args[0]) || !_in(variables,args[1]))
return 4;
for(int i = 0; i < currVariable; i++){
if(variables[i].id == args[0])
variables[i].value = getVariableValue(variables,args[0])-getVariableValue(variables,args[1]);
}
} else {
return 1;
}
break;
}
case MUL:{
if(argsCount == 1){
if(!_in(variables,args[0]))
return 4;
for(int i = 0; i < currVariable; i++){
if(variables[i].id == args[0])
variables[i].value = getVariableValue(variables,args[0])*funcLength;
}
} else if(argsCount == 2){
if(!_in(variables,args[0]) || !_in(variables,args[1]))
return 4;
for(int i = 0; i < currVariable; i++){
if(variables[i].id == args[0])
variables[i].value = getVariableValue(variables,args[0])*getVariableValue(variables,args[1]);
}
} else {
return 1;
}
break;
}
case DIV:{
if(argsCount == 1){
if(!_in(variables,args[0]))
return 4;
for(int i = 0; i < currVariable; i++){
if(variables[i].id == args[0])
variables[i].value = getVariableValue(variables,args[0])/funcLength;
}
} else if(argsCount == 2){
if(!_in(variables,args[0]) || !_in(variables,args[1]))
return 4;
for(int i = 0; i < currVariable; i++){
if(variables[i].id == args[0])
variables[i].value = getVariableValue(variables,args[0])/getVariableValue(variables,args[1]);
}
} else {
return 1;
}
break;
}
case COMMA:
return 2; // surely a variable is longer than 1 pixel so end the program
break;
}
curr++;
break;
}
default:{
if(_isReserved(currPixel)){ // a function is used outside brackets
return 0;
}
int exists = alreadyVariable(variables, currPixel);
if(exists == -1){ // variable doesn't exist
addVariable(currPixel);
for(++curr;getPixel(data,curr) == currPixel;++curr){
variables[currVariable-1].value++;
}
} else {
variables[exists].value = 1;
for(++curr;getPixel(data,curr) == currPixel;++curr){
variables[exists].value++;
}
}
break;
}
}
}
return -1;
}
int __in(uint32_t data[], int length, uint32_t colour){
for(int i = 0; i < length; i++){
if(data[i]==colour)
return 1;
}
return 0;
}
int getAmountOfVariables(uint8_t data[], int length){
uint32_t colours[length]; // could use a linked list to dynamically expand the size in order not to spam memeory ¯\_(ツ)_/¯
int count = 0;
for(int i = 0; i < length; i++){
if(!_isReserved(getPixel(data,i))){
if(!__in(colours, count, data[i]))
colours[count++] = data[i];
}
}
return count;
}