summaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authorJohnny Richard <johnny@johnnyrichard.com>2023-05-03 23:43:57 +0200
committerCarlos Maniero <carlos@maniero.me>2023-05-03 18:58:03 -0300
commit7c239a624ca5f7252e19c67fa1407acf7e9e42b1 (patch)
tree84af58c8762d1f478b8bc542bcfd748523b2fa0d /src/main.c
parent7aebc787c5529997e422a054398ed739ca4cddfe (diff)
cli: Rename src/pipac.c to src/main.c
I found easier to understand the application entrypoint by calling it main.c. Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c82
1 files changed, 82 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c
new file mode 100644
index 0000000..e3caec1
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,82 @@
+/*
+ * 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 "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 *func)
+{
+ gas_assembly_generator_t gen;
+ gas_assembly_generator_init(&gen, stdout);
+ gas_assembly_generator_compile(&gen, func);
+}
+
+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 = argv[1];
+
+ lexer_t lexer;
+ lexer_init(&lexer, filepath);
+
+ scope_t *scope = scope_new();
+ parser_t parser;
+ parser_init(&parser, &lexer, scope);
+
+ ast_node_t *func = parser_parse_function_declaration(&parser);
+
+ if (func == NULL) {
+ parser_print_errors(&parser);
+ return EXIT_FAILURE;
+ }
+
+ generate_gas_x86_64_linux(func);
+
+ scope_destroy(scope);
+ ast_node_destroy(func);
+ return EXIT_SUCCESS;
+}