-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
854 lines (714 loc) · 23.5 KB
/
main.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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
#define MAX_LINE_SIZE 200
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
// Types
// Token types
typedef enum {
TOKEN_INT,
TOKEN_STR,
TOKEN_PLUS,
TOKEN_MINUS,
TOKEN_MUL,
TOKEN_DIV,
TOKEN_LPAREN,
TOKEN_RPAREN,
TOKEN_EOF,
TOKEN_IDENTIFIER,
TOKEN_EQUALS,
TOKEN_NL,
TOKEN_IF,
TOKEN_ELSE,
TOKEN_SOFUNC,
TOKEN_EOFUNC,
TOKEN_SEPARATOR,
TOKEN_PERIOD,
TOKEN_COMMENT,
TOKEN_LOG,
TOKEN_CONCAT,
TOKEN_SCOPY,
TOKEN_INDEXER,
TOKEN_SIN,
TOKEN_COS,
TOKEN_TAN,
TOKEN_PI
} TokenType;
// Token structure
typedef struct {
TokenType type;
char* value;
} Token;
// Variable structure to store variable type, name an int and a string
// Multi type structure allows for ease of development as all variables are in one single struct array
typedef struct {
char* type;
char* name;
int value;
char* word;
} Variable;
// System function declarations
void writeToConsole(Token** tokens, Variable* variables);
Variable findVariable(Token** tokens, Variable* variables);
// Lexer function declarations
Token getNextToken(char* line, int* position, char** value);
// Parser function declarations
int parseFactor(Token** tokens, Variable* variables);
int parseTerm(Token** tokens, Variable* variables);
int parseExpression(Token** tokens, Variable* variables);
Variable parseVariableDeclaration(Token** tokens, Variable* variables);
// String methods declarations
void concatStrings(Token** tokens, Variable* variables);
char* copyString(Token** tokens, Variable* variables);
// Math library declarations
int esin(Token** tokens, Variable* variables);
int ecos(Token** tokens, Variable* variables);
int etan(Token** tokens, Variable* variables);
// System functions
// Writes a single line to the "console"
void writeToConsole(Token** tokens, Variable* variables) {
// Parse expression inside arguments
int result = parseExpression(tokens, variables);
// Print if expression is a string
if (result != 0) {
printf("%d\n", result);
}
else {
return;
}
}
// Finds a variable given a position in the tokens array and the variables array
Variable findVariable(Token** tokens, Variable* variables) {
// Declare variable index
int variableIndex = -1;
// Find variable in variables array
for (int i = 0; i < 10; i++) {
// If found
if (strcmp(variables[i].name, (*tokens)->value) == 0) {
// Set variable index to that variable
variableIndex = i;
break;
}
}
// If variable index didn't change
if (variableIndex == -1) {
printf("Error: Variable not found\n");
exit(EXIT_FAILURE);
}
return (variables[variableIndex]);
}
// Lexer
// Returns the next token in the .agp file
Token getNextToken(char* line, int* position, char** value) {
// Set current char
char currentChar = line[*position];
// Skip whitespace and tab characters
while (isspace(currentChar) || currentChar == '\t') {
(*position)++;
currentChar = line[*position];
}
// Tokenize specific characters
if (currentChar == '\0') {
return (Token) { TOKEN_EOF, NULL };
}
else if (currentChar == '\n') {
(*position)++;
return (Token) { TOKEN_NL, "\n" };
}
else if (currentChar == '+') {
(*position)++;
return (Token) { TOKEN_PLUS, "+" };
}
else if (currentChar == '-') {
(*position)++;
return (Token) { TOKEN_MINUS, "-" };
}
else if (currentChar == '*') {
(*position)++;
return (Token) { TOKEN_MUL, "*" };
}
else if (currentChar == '/') {
(*position)++;
return (Token) { TOKEN_DIV, "/" };
}
else if (currentChar == '(') {
(*position)++;
return (Token) { TOKEN_LPAREN, "(" };
}
else if (currentChar == ')') {
(*position)++;
return (Token) { TOKEN_RPAREN, ")" };
}
else if (currentChar == '=') {
(*position)++;
return (Token) { TOKEN_EQUALS, "=" };
}
else if (currentChar == ':') {
(*position)++;
return (Token) { TOKEN_SOFUNC, ":" };
}
else if (currentChar == '_') {
(*position)++;
return (Token) { TOKEN_EOFUNC, "_" };
}
else if (currentChar == ',') {
(*position)++;
return (Token) { TOKEN_SEPARATOR, "," };
}
else if (currentChar == '.') {
(*position)++;
return (Token) { TOKEN_PERIOD, "." };
}
else if (currentChar == '"') { // if char is a number
// Index start of token
(*position)++;
int start = *position;
// Read whole number
while (line[*position] != '"' && line[*position] != '\0') {
(*position)++;
}
if (line[*position] == '\0') {
printf("Error: Unterminated string literal\n");
exit(EXIT_FAILURE);
}
// Calculate length of token
int length = *position - start;
(*position)++;
// Reallocate data for values depending on length of token
char* valueLocation = realloc(*value, length + 1);
// Update value pointer location for freeing
if (valueLocation != NULL) {
*value = valueLocation;
}
// Copy number at start with length of number
strncpy(*value, &line[start], length);
// add NULL char
(*value)[length] = '\0';
return (Token) { TOKEN_STR, strdup(*value) };
}
else if (currentChar == '$') { // if char is a number
// Index start of token
int start = *position;
(*position)++;
// Read whole number
while (line[*position] == '$') {
(*position)++;
}
// Calculate length of token
int length = *position - start;
if (length != 3) {
printf("Error: Unexpected token: $. Comment with $$$. \n");
exit(EXIT_FAILURE);
}
// Reallocate data for values depending on length of token
char* valueLocation = realloc(*value, length + 1);
// Update value pointer location for freeing
if (valueLocation != NULL) {
*value = valueLocation;
}
// Copy number at start with length of number
strncpy(*value, &line[start], length);
// add NULL char
(*value)[length] = '\0';
return (Token) { TOKEN_COMMENT, strdup(*value) };
}
else if (currentChar == '[') { // if char is a number
// Index start of token
int start = *position;
(*position)++;
// Read whole number
while (isdigit(line[*position])) {
(*position)++;
}
if (line[*position] != ']') {
printf("Error: Unterminated array\n");
exit(EXIT_FAILURE);
}
(*position)++;
// Calculate length of token
int length = *position - start;
// Reallocate data for values depending on length of token
char* valueLocation = realloc(*value, length + 1);
// Update value pointer location for freeing
if (valueLocation != NULL) {
*value = valueLocation;
}
// Copy number at start with length of number
strncpy(*value, &line[start], length);
// add NULL char
(*value)[length] = '\0';
return (Token) { TOKEN_INDEXER, strdup(*value) };
}
else if (isdigit(currentChar)) { // if char is a number
// Index start of token
int start = *position;
// Read whole number
while (isdigit(line[*position])) {
(*position)++;
}
// Calculate length of token
int length = *position - start;
// Reallocate data for values depending on length of token
char* valueLocation = realloc(*value, length + 1);
// Update value pointer location for freeing
if (valueLocation != NULL) {
*value = valueLocation;
}
// Copy number at start with length of number
strncpy(*value, &line[start], length);
// add NULL char
(*value)[length] = '\0';
return (Token) { TOKEN_INT, strdup(*value) };
}
else if (isalpha(currentChar)) { // if char is letter
// Index start of token
int start = *position;
// Read whole word
while (isalnum(line[*position])) {
(*position)++;
}
// Calculate length of token
int length = *position - start;
// Reallocate data for values depending on length of token
char* valueLocation = realloc(*value, length + 1);
// Update value pointer location for freeing
if (valueLocation != NULL) {
*value = valueLocation;
}
// Copy number at start with length of number
strncpy(*value, &line[start], length);
// add NULL char
(*value)[length] = '\0';
// Tokenize if-else tokens
if (strcmp(*value, "if") == 0) {
(*position)++;
return (Token) { TOKEN_IF, "if" };
}
else if (strcmp(*value, "else") == 0) {
(*position)++;
return (Token) { TOKEN_ELSE, "else" };
}
else if (strcmp(*value, "log") == 0) {
(*position)++;
return (Token) { TOKEN_LOG, "log" };
}
else if (strcmp(*value, "concat") == 0) {
(*position)++;
return (Token) { TOKEN_CONCAT, "concat" };
}
else if (strcmp(*value, "copy") == 0) {
(*position)++;
return (Token) { TOKEN_SCOPY, "copy" };
}
else if (strcmp(*value, "esin") == 0) {
(*position)++;
return (Token) { TOKEN_SIN, "esin" };
}
else if (strcmp(*value, "ecos") == 0) {
(*position)++;
return (Token) { TOKEN_COS, "etan" };
}
else if (strcmp(*value, "etan") == 0) {
(*position)++;
return (Token) { TOKEN_TAN, "etan" };
}
else if (strcmp(*value, "PI") == 0) {
(*position)++;
return (Token) { TOKEN_PI, "PI" };
}
return (Token) { TOKEN_IDENTIFIER, strdup(*value) };
}
// If the current character is not recognized, return an error token
printf("Tokenizing error: Unrecognized character '%c'\n", currentChar);
exit(EXIT_FAILURE);
}
// Parser
// Parses a variables declaration
// Returns a variable that can be stored in variable array
// Examples: int x = 2, int y = x
Variable parseVariableDeclaration(Token** tokens, Variable* variables) {
// Variable type
if ((*tokens)->type != TOKEN_IDENTIFIER) {
printf("Error: Expected variable type\n");
exit(EXIT_FAILURE);
}
// Save variable type
char* variableType = (*tokens)->value;
// Move forward on tokens array
(*tokens)++;
// Variable name
if ((*tokens)->type != TOKEN_IDENTIFIER) {
printf("Error: Expected variable name\n");
exit(EXIT_FAILURE);
}
// Save variable name
char* variableName = (*tokens)->value;
// Move forward on tokens array
(*tokens)++;
// Equals sign
if ((*tokens)->type != TOKEN_EQUALS) {
printf("Error: Expected equals sign (=)\n");
exit(EXIT_FAILURE);
}
// Move forward on tokens array
(*tokens)++;
// Calculate length of token string
int length = strlen((*tokens)->value);
// Declare value for output
int value = 0;
// Allocate memory for one word of length of token
char* word = malloc(length + 1);
if ((*tokens)->type == TOKEN_INT || (*tokens)->type == TOKEN_STR) {
// Check for the variable type
// If is int
if (strcmp(variableType, "int") == 0) {
value = parseExpression(tokens, variables);
}
// If is string
else if (strcmp(variableType, "string") == 0) {
strncpy(word, (*tokens)->value, length);
word[length] = '\0';
}
else {
printf("Error: Unidentified variable type\n");
exit(EXIT_FAILURE);
}
}
else if ((*tokens)->type == TOKEN_SCOPY) {
(*tokens)++;
word = copyString(tokens, variables);
}
else if ((*tokens)->type == TOKEN_CONCAT) {
(*tokens)++;
concatStrings(tokens, variables);
}
else if ((*tokens)->type == TOKEN_IDENTIFIER) {
Variable var = findVariable(tokens, variables);
word = var.word;
value = var.value;
}
// Move forward on tokens array
(*tokens)++;
return (Variable) { variableType, variableName, value, word };
}
// Parses a factor
// Returns an integer that can be used to solve a term
// Examples: x, 2, ()
int parseFactor(Token** tokens, Variable* variables) {
// If token is an integer
if ((*tokens)->type == TOKEN_INT) {
int result = atoi((*tokens)->value);
// Move forward in tokens array
(*tokens)++;
return result;
}
// If token is a (
else if ((*tokens)->type == TOKEN_LPAREN) {
// Move forward in tokens array
(*tokens)++;
// Parse expression inside parenthesis
int result = parseExpression(tokens, variables);
// Check for lack of )
if ((*tokens)->type != TOKEN_RPAREN) {
printf("Error: Missing closing parenthesis\n");
exit(EXIT_FAILURE);
}
// Move forward in tokens array
(*tokens)++;
return result;
}
else if ((*tokens)->type == TOKEN_IDENTIFIER) {
Variable var = findVariable(tokens, variables);
if ((*tokens + 1)->type == TOKEN_INDEXER && strcmp(var.type, "string") == 0) {
int index = ((*tokens + 1)->value)[1] - '0';
printf("%s\n", (*var.word + index) + '\0');
}
// Assign result to that variable's value
int result = var.value;
if (var.word != NULL && result == 0) {
printf("%s\n", var.word);
}
// Move forward in tokens array
(*tokens)++;
return result;
}
else if ((*tokens)->type == TOKEN_PI) {
return 3;
}
else {
printf("Error: Unexpected token\n");
exit(EXIT_FAILURE);
}
}
// Parses a term
// Returns an int that can be used to solve expression
// Examples: (2*2), ((2*3)/2)
int parseTerm(Token** tokens, Variable* variables) {
// Parse first factor in term
int result = parseFactor(tokens, variables);
// Loop so you can do multiple computations
while ((*tokens)->type == TOKEN_MUL || (*tokens)->type == TOKEN_DIV) {
Token* currentToken = *tokens;
// Move forward in tokens array
(*tokens)++;
// Parse next factor in term
int nextFactor = parseFactor(tokens, variables);
if (currentToken->type == TOKEN_MUL) {
result *= nextFactor;
}
else if (currentToken->type == TOKEN_DIV) {
if (nextFactor == 0) {
printf("Error: Division by zero\n");
exit(EXIT_FAILURE);
}
result /= nextFactor;
}
}
return result;
}
// Parses an expression
// Returns the result to the expression
// Examples: (10/2) + 1, 1+1, ((3*5+1)/10)+10, 1 == 2
int parseExpression(Token** tokens, Variable* variables) {
// Parse first term in expression
int result = parseTerm(tokens, variables);
// Check if expression is an equality
if ((*tokens)->type == TOKEN_EQUALS && ((*tokens) + 1)->type == TOKEN_EQUALS) {
// Move forward two spaces in tokens array (two equals)
(*tokens) += 2;
// Parse next term in expression
int nextTerm = parseTerm(tokens, variables);
// Return a boolean equality
return (result == nextTerm);
}
else {
// Loop so you can do multiple computations
while ((*tokens)->type == TOKEN_PLUS || (*tokens)->type == TOKEN_MINUS) {
Token* currentToken = *tokens;
// Move forward in tokens array
(*tokens)++;
// Parse next term in expression
int nextTerm = parseTerm(tokens, variables);
// Compute result depending on the operation
if (currentToken->type == TOKEN_PLUS) {
result += nextTerm;
}
else if (currentToken->type == TOKEN_MINUS) {
result -= nextTerm;
}
}
return result;
}
}
// String functions
// Concatenates two strings
// Returns a string that can be assigned to a variable
// concat(dest, src)
void concatStrings(Token** tokens, Variable* variables) {
if ((*tokens)->type == TOKEN_IDENTIFIER && (*tokens + 1)->type == TOKEN_SEPARATOR && (*tokens + 2)->type == TOKEN_IDENTIFIER) {
char* string1 = findVariable(tokens, variables).word;
(*tokens) += 2;
char* string2 = findVariable(tokens, variables).word;
strcat(string1, string2);
return;
}
else {
printf("Error: Unexpected call. Expecting concat(dest, src). \n");
exit(EXIT_FAILURE);
}
}
// Copies a string into a variable
// string copy(src)
char* copyString(Token** tokens, Variable* variables) {
if ((*tokens)->type == TOKEN_IDENTIFIER && (*tokens + 1)->type == TOKEN_RPAREN) {
char* string = findVariable(tokens, variables).word;
return string;
}
else if ((*tokens)->type == TOKEN_IDENTIFIER && (*tokens + 1)->type == TOKEN_INDEXER && (*tokens + 2)->type == TOKEN_RPAREN) {
int index = ((*tokens + 1)->value)[1] - '0';
char* word = findVariable(tokens, variables).word;
if (index >= strlen(word)) {
printf("Error: Index is out of bounds. \n");
exit(EXIT_FAILURE);
}
char* value = malloc(2);
strncpy(value, &word[index], 1);
value[2] = '\0';
return value;
}
else {
printf("Error: Unexpected call. Expecting copy(src). \n");
exit(EXIT_FAILURE);
}
}
// Math functions
// Takes an integer and returns the truncted sin result back
// Also prints the float value
// Angle in degrees
// int sin(angle)
int esin(Token** tokens, Variable* variables) {
double result = (double)parseExpression(tokens, variables);
result = sin(result);
printf("Float result: %lf\n", result);
return result;
}
// Takes an integer and returns the truncted cos result back
// Also prints the float value
// Angle in degrees
// int cos(angle)
int ecos(Token** tokens, Variable* variables) {
double result = (double)parseExpression(tokens, variables);
result = cos(result);
printf("Float result: %lf\n", result);
return result;
}
// Takes an integer and returns the truncted tan result back
// Also prints the float value
// Angle in degrees
// int tan(angle)
int etan(Token** tokens, Variable* variables) {
double result = (double)parseExpression(tokens, variables);
result = cos(result);
printf("Float result: %lf\n", result);
return result;
}
int main(void) {
// Declare variables
char line[MAX_LINE_SIZE];
int variableCount = 0;
int result = 0;
int condition = 1;
// Condition copy is used to check previous condition for if-else statements
int conditionCopy = condition;
// Initialize memory for 20 variables
Variable* variables = malloc(sizeof(Variable) * 20);
if (variables == NULL) {
printf("System error: Memory allocation failed\n");
exit(EXIT_FAILURE);
}
// Read file
FILE* file;
file = fopen("program.el", "r");
if (file == NULL) {
printf("System error: Couldn't open file\n");
exit(EXIT_FAILURE);
}
// Read lines
while (fgets(line, 199, file)) {
// Declare variables
int position = 0;
int tokenCount = 0;
// Print out current line
printf("%s", line);
// Allocate memory for tokens
Token* tokens = malloc(sizeof(Token) * strlen(line));
if (tokens == NULL) {
printf("Error: Memory allocation failed\n");
exit(EXIT_FAILURE);
}
// Save tokens location for freeing
void* tokensLocation = tokens;
// Tokenize line
// Create flag for stopping tokenizing
int flag = 1;
// Allocate memory for values of tokens
char* value = malloc(sizeof(char));
// Declare next token
Token nextToken;
// Start tokenizing one line
while (flag) {
nextToken = getNextToken(line, &position, &value);
tokens[tokenCount] = nextToken;
tokenCount++;
// If token is EOF or \n
if (nextToken.type == TOKEN_EOF) {
flag = 0;
break;
}
}
// If you are in loop mode
// Check if condition is true now
// Rerun line
// Parse tokens
// Check if you can read line
if (condition) {
// If line is a variable declaration
if (tokens[0].type == TOKEN_IDENTIFIER && tokens[1].type == TOKEN_IDENTIFIER) {
variables[variableCount] = parseVariableDeclaration(&tokens, variables);
variableCount++;
}
// If line is an if statement
else if (tokens[0].type == TOKEN_IF) {
tokens++;
condition = parseExpression(&tokens, variables);
}
// If line is an else or else if statement
else if (tokens[0].type == TOKEN_ELSE) {
tokens++;
if ((*tokens).type == TOKEN_IF) {
tokens++;
condition = parseExpression(&tokens, variables);
}
else {
condition = !conditionCopy;
}
}
// If line is a log statement
else if (tokens[0].type == TOKEN_LOG) {
tokens++;
writeToConsole(&tokens, variables);
}
// If line is a concat statement
else if (tokens[0].type == TOKEN_CONCAT) {
tokens++;
concatStrings(&tokens, variables);
}
// If line is a sin statement
else if (tokens[0].type == TOKEN_SIN) {
tokens++;
result = esin(&tokens, variables);
}
// If line is a cos statement
else if (tokens[0].type == TOKEN_COS) {
tokens++;
result = ecos(&tokens, variables);
}
// If line is a tan statement
else if (tokens[0].type == TOKEN_TAN) {
tokens++;
result = etan(&tokens, variables);
}
// If line is a comment
else if (tokens[0].type == TOKEN_COMMENT) {
tokens++;
}
// If line is an end of function line
else if (tokens[0].type == TOKEN_EOFUNC) {
tokens++;
}
// If line is an expression
else {
result = parseExpression(&tokens, variables);
}
}
// Reset condition after finishing a function
if (tokens[0].type == TOKEN_EOFUNC) {
conditionCopy = condition;
condition = 1;
}
// Update tokens pointer for freeing
if (tokensLocation != NULL) {
tokens = tokensLocation;
}
// Free memory allocations
free(value);
free(tokens);
}
// Display result
printf("Result: %d\n", result);
// Free more memory and close file
free(variables);
fclose(file);
return 0;
}