/
main.c
71 lines (56 loc) · 1.76 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
#include <stdio.h>
#include <stdlib.h>
#include "debug.h"
#include "ioutils.h"
#include "filebuffer.h"
#include "tokenbuffer.h"
#include "instructionbuffer.h"
#include "symboltree.h"
#include "scanner/tokenizer.h"
#include "scanner/printtoken.h"
#include "parser/syntax.h"
int main(int argc, char **argv)
{
// Pseudo command-line args; uncomment the following lines and replace the
// main function's signature with "int main(void)" in order to hard-code
// the file paths to input.txt and output.txt.
#if 0
int argc = 5;
char* argv[] = {"./plrun", "-i", "input.txt", "-o", "output.txt"};
#endif
char *finName = NULL;
char *foutName = NULL;
FILE *fin;
FILE *fout;
parseCommandLineArgs(argc, argv, &foutName, &finName,
"A Compiler for PL/0 with an integrated virtual machine.");
fin = modified_fopen(finName, "r", stdin);
fout = modified_fopen(foutName, "w", stdout);
/* SCANNING */
FileBuffer *file_buffer = createFileBuffer(fin);
fclose(fin);
TokenBuffer *token_buffer = tokenize(file_buffer);
freeFileBuffer(file_buffer);
if (_IO_show_lexemes)
{
printTokenList(token_buffer, fout);
fputs("\n\n", fout);
printSymbolicTokenList(token_buffer, fout);
fputs("\n\n", fout);
}
InstructionBuffer *code = createInstructionBuffer();
SymbolTree *tree = createSymbolTree();
program(token_buffer, code, tree);
freeSymbolTree(tree);
freeTokenBuffer(token_buffer);
fputs("No errors, program is syntactically correct.\n\n", fout);
if (_IO_show_assembler)
{
fputs("Instructions: \n", fout);
disassemble(fout, code->buf);
}
run(fout, code->buf);
fclose(fout);
freeInstructionBuffer(code);
return EXIT_SUCCESS;
}