-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.c
82 lines (71 loc) · 1.31 KB
/
driver.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
/*
This is the driver for Almalki's Spring 2022 Systems Software Project
If you choose to alter this, you MUST make a note of that in your
readme file, otherwise you will lose 5 points.
*/
#include <stdlib.h>
#include <stdio.h>
#include "compiler.h"
#define MAX_PROGRAM_LENGTH 3000
int main(int argc, char *argv[])
{
FILE *ifp;
char *input;
char c;
lexeme *list;
instruction *code;
int i;
int tokens = 0, symbols = 0, codes = 0, outputs = 0;
if (argc < 2)
{
printf("Error : please include the file name\n");
return 0;
}
ifp = fopen(argv[1], "r");
input = malloc(MAX_PROGRAM_LENGTH * sizeof(char));
i = 0;
c = fgetc(ifp);
while (1)
{
input[i++] = c;
c = fgetc(ifp);
if (c == EOF)
break;
}
input[i] = '\0';
for (i = 2; i < argc; i++)
{
if (argv[i][1] == 'l')
tokens = 1;
else if (argv[i][1] == 's')
symbols = 1;
else if (argv[i][1] == 'a')
codes = 1;
else if (argv[i][1] == 'v')
outputs = 1;
else
{
printf("Error : unrecognized directive\n");
free(input);
return 0;
}
}
list = lexanalyzer(input, tokens);
if (list == NULL)
{
free(input);
return 0;
}
code = parse(list, symbols, codes);
if (code == NULL)
{
free(input);
free(list);
return 0;
}
execute_program(code, outputs);
free(input);
free(list);
free(code);
return 0;
}