This repository was archived by the owner on Sep 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader.c
55 lines (40 loc) · 1.43 KB
/
loader.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
#include <stdio.h>
#include <stdlib.h>
#include "headers/utilities.h"
#include "headers/loader.h"
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Wrong format.\nUse: <executable file>.exe <out variables>.txt");
exit(1);
}
char *fileNameProgram = argv[1];
char *fileNameVariables = argv[2];
int fileLenght = countFileChars(fileNameProgram);
char *memory = (char*) malloc((size_t) (fileLenght + 1));
if (memory == NULL)
printError("Unable to allocate the memory.");
readFile(fileNameProgram, memory);
struct Program executableProgram;
executableProgram.startChar = memory;
executableProgram.currentToken = *((struct Lexem*) malloc(sizeof(struct Lexem)));
executableProgram.currentToken.name = NULL;
executableProgram.currentChar = memory;
executableProgram.fileNameOutVariables = fileNameVariables;
executableProgram.variablesCounter = 0;
executableProgram.variablesPointer = (struct Variable*) malloc(sizeof(struct Variable));
executableProgram.state = INITIALIZED;
execute(executableProgram);
return 0;
}
void readFile(char *fileNameProgram, char *memory) {
FILE *file;
if (!(file = fopen(fileNameProgram, "r")))
printError("Unable to read the program file.");
char *point = memory;
do {
*point = (char) getc(file);
point++;
} while (!feof(file));
*(point - 1) = '\0';
fclose(file);
}