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
|
/*
* Copyright (C) 2023 Johnny Richard
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ast.h"
#include "ast_pretty_printer.h"
#include "gas_assembly_generator.h"
#include "lexer.h"
#include "parser.h"
#include "scope.h"
#include "string_view.h"
static void
generate_gas_x86_64_linux(ast_node_t *ns)
{
gas_assembly_generator_t gen;
gas_assembly_generator_init(&gen, stdout);
gas_assembly_generator_compile_linux_main(&gen, ns);
}
static void
pretty_print_ast(ast_node_t *ast)
{
ast_pretty_printer_t printer;
ast_pretty_printer_init(&printer, stdout);
ast_pretty_printer_print_ast(&printer, ast);
}
static void
print_usage(void)
{
fputs("pipac <filename.pipa>\n", stderr);
}
static void
parser_print_errors(parser_t *parser)
{
for (int i = 0; i < parser->errors_len; i++) {
parser_error_t error = parser->errors[i];
fprintf(
stderr, "%s:%d:%d: [ERROR]: %s\n", error.token.filepath, error.token.row + 1, error.token.col + 1, error.message);
}
}
int
main(int argc, char **argv)
{
if (argc < 2) {
print_usage();
return EXIT_FAILURE;
}
char *filepath;
bool should_dump_ast = false;
// TODO: Handle command line arguments properly
if (argc < 3) {
filepath = argv[1];
} else {
if (strcmp(argv[1], "--ast-dump") != 0) {
print_usage();
return EXIT_FAILURE;
}
should_dump_ast = true;
filepath = argv[2];
}
lexer_t lexer;
lexer_init(&lexer, filepath);
scope_t *scope = scope_new();
parser_t parser;
parser_init(&parser, &lexer, scope);
ast_node_t *ns = parser_parse_ns(&parser);
if (ns == NULL) {
parser_print_errors(&parser);
return EXIT_FAILURE;
}
if (should_dump_ast) {
pretty_print_ast(ns);
} else {
generate_gas_x86_64_linux(ns);
}
scope_destroy(scope);
ast_node_destroy(ns);
return EXIT_SUCCESS;
}
|