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
#include <stdio.h>
#include <stdlib.h>
#include "compile.h"
#include "parser.h"
void usage(char *program) {
fprintf(stderr, "USAGE: %s <program file>\n", program);
exit(1);
}
/**
* Prints the start of the the x86-64 assembly output.
* The assembly code implementing the TeenyBASIC statements
* goes between the header and the footer.
*/
void header(void) {
printf(
"# The code section of the assembly file\n"
".text\n"
".globl basic_main\n"
"basic_main:\n"
" # The main() function\n");
}
/**
* Prints the end of the x86-64 assembly output.
* The assembly code implementing the TeenyBASIC statements
* goes between the header and the footer.
*/
void footer(void) {
printf(" ret\n");
}
int main(int argc, char *argv[]) {
if (argc != 2) {
usage(argv[0]);
}
FILE *program = fopen(argv[1], "r");
if (program == NULL) {
usage(argv[0]);
}
header();
node_t *ast = parse(program);
fclose(program);
if (ast == NULL) {
fprintf(stderr, "Parse error\n");
return 2;
}
// Display the AST for debugging purposes
print_ast(ast);
// Compile the AST into assembly instructions
if (!compile_ast(ast)) {
free_ast(ast);
fprintf(stderr, "Compilation error\n");
return 3;
}
free_ast(ast);
footer();
}