-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
73 lines (64 loc) · 1.73 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
/*****************************************************************************************
*
* IFJ - interpret of IFJ15 language
*
* main module
* open input file and call others modules to process it and interpret the code
*
* author: Miroslav Karásek (xkaras31)
* created: 2015-10-30
* modified: 2015-11-17
*
*****************************************************************************************/
#include <stdio.h>
#include "parser.h"
#include "instlist.h"
#include "ial.h"
#include "interpret.h"
int main(int argc, char const **argv)
{
FILE *programFile = NULL;
block_T mainBlock;
// opening program file
if (argc == 2)
{
programFile = fopen(argv[1], "r");
if (programFile == NULL)
{
fprintf(stderr, "File '%s' can not be opened\n", argv[1]);
return 99;
}
}
else if (argc < 2)
{
fprintf(stderr, "No source file selected\n");
return 99;
}
else
{
fprintf(stderr, "Bad count of arguments\n");
return 99;
}
// will be set to nonezero value if some fails
int errorCode = 0;
// parse program and generate instructions
if (errorCode == 0)
errorCode = parse(programFile, &mainBlock);
// interpret code
if (errorCode == 0)
errorCode = interpret(&mainBlock);
// free allocations
freeParser();
freeInterpret();
destroyST(&mainBlock.symbols);
destroyIL(&mainBlock.program);
// closing program file
if (fclose(programFile) == EOF)
{
fprintf(stderr, "Error when closing '%s'\n", argv[1]);
if (errorCode == 0)
errorCode = 99;
}
// if some operation fails return code is not 0
return errorCode;
}