aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authorMarvin Borner2023-04-11 20:56:14 +0200
committerMarvin Borner2023-04-11 20:56:14 +0200
commit5c8f9487ec11035cbd1accfb67f1a4943a2b8db1 (patch)
tree8a294e7efe801986b6e7f9e19a5ed962aa816c4f /src/main.c
parent4b40b490d3948a2ccf736bcf94b95abf44955a38 (diff)
Bootstrapping
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c100
1 files changed, 100 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c
new file mode 100644
index 0000000..454c9d9
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,100 @@
+// Copyright (c) 2023, Marvin Borner <dev@marvinborner.de>
+// SPDX-License-Identifier: MIT
+
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include <term.h>
+#include <print.h>
+#include <parse.h>
+#include <build.h>
+#include <free.h>
+
+#define BUF_SIZE 1024
+static char *read_stdin(void)
+{
+ char buffer[BUF_SIZE];
+ size_t size = 1;
+ char *string = malloc(sizeof(char) * BUF_SIZE);
+ if (!string)
+ return 0;
+ string[0] = '\0';
+ while (fgets(buffer, BUF_SIZE, stdin)) {
+ char *old = string;
+ size += strlen(buffer);
+ string = realloc(string, size);
+ if (!string) {
+ free(old);
+ return 0;
+ }
+ strcat(string, buffer);
+ }
+
+ if (ferror(stdin)) {
+ free(string);
+ fprintf(stderr, "Couldn't read from stdin\n");
+ return 0;
+ }
+ return string;
+}
+
+static char *read_file(const char *path)
+{
+ FILE *f = fopen(path, "rb");
+ if (!f) {
+ fprintf(stderr, "Can't open file %s: %s\n", path,
+ strerror(errno));
+ return 0;
+ }
+
+ fseek(f, 0, SEEK_END);
+ long fsize = ftell(f);
+ fseek(f, 0, SEEK_SET);
+
+ char *string = malloc(fsize + 1);
+ int ret = fread(string, fsize, 1, f);
+ fclose(f);
+
+ if (ret != 1) {
+ fprintf(stderr, "Can't read file %s: %s\n", path,
+ strerror(errno));
+ return 0;
+ }
+
+ string[fsize] = 0;
+ return string;
+}
+
+int main(int argc, char **argv)
+{
+ /* if (argc < 2) { */
+ /* fprintf(stderr, "Invalid arguments\n"); */
+ /* return 1; */
+ /* } */
+
+ /* char *input; */
+ /* if (argv[1][0] == '-') { */
+ /* input = read_stdin(); */
+ /* } else { */
+ /* input = read_file(argv[1]); */
+ /* } */
+
+ /* if (!input) */
+ /* return 1; */
+
+ /* struct term *parsed = parse_blc(input); */
+ /* print_bruijn(parsed); */
+
+ write_bloc(0, "test.bloc");
+
+ const char *input = read_file("test.bloc");
+ struct bloc_parsed *bloc = parse_bloc(input);
+ struct term *term = from_bloc(bloc);
+ print_blc(term);
+
+ /* free_term(term); // TODO: Fix sharing user-after-free */
+
+ return 0;
+}