-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
87 lines (79 loc) · 1.68 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <stdio.h>
#include <string.h>
#include "evaluator/evaluator.h"
#include "libs/vector.h"
#include "libs/tracer.h"
#include "parser/parser.h"
#include "ast/ast.h"
struct {
int ast;
const char *filepath;
} args;
static char *get_file_content(const char *filepath)
{
char *content;
FILE *file;
long fsize;
if ((file = fopen(filepath, "rb")) == NULL)
return (NULL);
fseek(file, 0, SEEK_END);
if ((fsize = ftell(file)) == -1)
goto fail_close;
fseek(file, 0, SEEK_SET);
content = malloc(sizeof(char) * (fsize + 1));
if (content == NULL)
goto fail_close;
fread(content, fsize, 1, file); // TODO: error check
fclose(file);
content[(size_t)fsize] = '\0';
return content;
fail_close:
fclose(file);
return NULL;
}
static void print_helper(const char *filepath)
{
printf("usage: ./%s <amVy file>\n", filepath);
printf("options:\n");
printf("--ast: print ast\n");
exit(0);
}
static void parse_arguments(int ac, char **av)
{
if (ac == 1)
print_helper(av[0]);
for (int i = 0; i < ac; i++) {
if (av[i][0] == '-' && av[i][1] == '-') {
if (strcmp(av[i], "ast"))
args.ast = 1;
} else {
args.filepath = av[i];
}
}
}
int main(int ac, char **av)
{
char *file_content;
struct Parser parser;
struct Program program;
parse_arguments(ac, av);
file_content = get_file_content(args.filepath);
if (file_content == NULL)
{
printf("failed to open %s\n", args.filepath);
return (1);
}
parser_init(&parser, file_content);
program = parser_parse(&parser);
if (args.ast) {
for (unsigned int i = 0; i < vector_size(program.statements); i++)
{
print_node(program.statements[i], 0);
}
goto quit;
}
evaluator_eval(program);
quit:
parser_destroy(&parser);
return (0);
}