-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
114 lines (109 loc) · 2.9 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
/* Austin Hester
CS 4280 sp18
C.Z. Janikow */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "metafile.h"
#include "parser.h"
#include "filter.h"
#include "wordlist.h"
#include "token.h"
#include "tree.h"
#include "treehelper.h"
#include "treetrim.h"
#include "node.h"
#include "staticsem.h"
#include "codegen.h"
int
main(int argc, char** argv)
{
// deal with command line arguments or lack thereof
int keyboardin = 1;
char* fname = "out";
FILE* fp = stdin;
// Parse arguments
if (argc > 1) {
keyboardin = 0;
fname = getbasefilename(argv[1]);
if (fname == (char*)NULL) {
perror("Input error");
return 1;
}
fp = openinputfile(fname);
}
if (fp == (FILE*)NULL) {
perror("Input error");
return 1;
}
// filter the file line by line to get a wordlist_t
wordlist_t* filter = filtersource(fp);
fclose(fp);
if (filter == (wordlist_t*)NULL) {
free(filter);
perror("Filter error");
return 1;
}
// Display filtered wordlist
displayfilter(filter);
// Initialize the token list
token_t** tokenlist = (token_t**) malloc(256*sizeof(token_t*));
if (tokenlist == (token_t**)NULL ) {
free(tokenlist);
perror("Memory error");
return 1;
}
int n = 0; // num_tokens
node_t* root = NULL;
// calls parser() function until EOF
root = parser(root, tokenlist, filter, &n);
//Print the complete parse tree
printf("\nFull Tree:");
traversepreorder(root);
// Trim the tree and print
node_t* newroot = NULL;
newroot = treetrim(newroot, root);
printf("\nTrimmed Tree:");
traversepreorder(newroot);
// Static semantic analysis
//int pass_static = 0;
int pass_static = staticsem(newroot);
if (pass_static == 0)
printf("\nPass static semantic check. OK\n");
else if (pass_static > 0) {
printf("********************************\n");
printf("Fail static semantic check. FAIL\n");
switch (pass_static)
{
case 1:
printf("Variable already defined\n");
break;
case 2:
printf("Variable not yet defined\n");
break;
default:
printf("Check yourself. No idea.\n");
}
printf("********************************\n");
return -1;
} else {
fprintf(stderr, "Error while checking static semantics.");
return -1;
}
char outfile[64];
sprintf(outfile, "%s%s", fname, ".asm");
// Passed static semantics
FILE* fout = fopen(outfile, "w");
gen_program(newroot, fout);
printf("%s\n", outfile);
fclose(fout);
// Free them
free(filter);
free(tokenlist);
free(root);
free(newroot);
// free fname if it was generated.
if (!keyboardin)
free(fname);
return 0;
}