-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
118 lines (107 loc) · 3.53 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "hotaru.h"
#include "hvm.h"
#include "utils.h"
#include <stdio.h>
void usage(FILE *f, const char *program)
{
fprintf(f, "USAGE: %s SUBCOMMAND <ARGS> [FLAGS]\n", program);
fprintf(f, "Available subcommand:\n");
fprintf(f, " com <file.ht> -o <output.hbc>\n");
fprintf(f, " run <file.ht>\n");
fprintf(f, " dump <file.hbc>\n");
fprintf(f, " help\n");
}
typedef struct Args {
int count;
const char **items;
} Args;
const char *shift_args(Args *args, const char *on_empty_message)
{
if(args->count <= 0) {
fprintf(stderr, "ERROR: %s\n", on_empty_message);
exit(EXIT_FAILURE);
return NULL;
}
const char *result = args->items[0];
args->items += 1;
args->count -= 1;
return result;
}
enum cli_mode {
cli_mode_run,
cli_mode_compile,
cli_mode_dump,
};
int main(int argc, const char **argv)
{
Args args = {argc, argv};
const char *program_name = shift_args(&args, "Unreachable");
StringView subcommand = sv_from_cstr(shift_args(&args, "Please provide a subcommand"));
enum cli_mode mode = ut_false;
if(sv_eq(subcommand, SV("help"))) {
usage(stdout, program_name);
return 0;
} else if(sv_eq(subcommand, SV("com"))) {
mode = cli_mode_compile;
} else if(sv_eq(subcommand, SV("run"))) {
mode = cli_mode_run;
} else if(sv_eq(subcommand, SV("bcdump"))) {
mode = cli_mode_dump;
} else {
fprintf(stderr, "ERROR: Invalid subcommand %s\n", subcommand.data);
usage(stderr, program_name);
return -1;
}
const char *source_file = shift_args(&args, "Provide the source file path");
const char *output_file = "output.hbc";
if(mode == cli_mode_compile && args.count > 0) {
StringView flag = sv_from_cstr(shift_args(&args, "Unreachable"));
if(!sv_eq(flag, SV("-o"))) {
fprintf(stderr, "ERROR: Invalid flag %s\n", flag.data);
usage(stderr, program_name);
return -1;
}
output_file = shift_args(&args, "Expecting output file path");
}
Arena a = {0};
hState state;
hstate_init(&state);
switch(mode) {
case cli_mode_run:
{
char *source = load_file_text_with_arena(source_file, &a);
if(!source) {
fprintf(stderr, "ERROR: Could not load file %s\n", argv[1]);
usage(stderr, argv[0]);
return -1;
}
hstate_exec_source(&state, source);
hvm_dump(&state.vm);
} break;
case cli_mode_dump:
{
HVM_Module mod = {0};
if(!hvm_module_load_from_file(&mod, source_file, &a)) {
fprintf(stderr, "ERROR: Could not load file %s\n", argv[1]);
fprintf(stderr, "USAGE: %s <program.hbc>\n", argv[0]);
return -1;
}
hvm_module_dump(mod);
hvm_module_deinit(&mod);
} break;
case cli_mode_compile:
{
char *source = load_file_text_with_arena(source_file, &a);
if(!source) {
fprintf(stderr, "ERROR: Could not load file %s\n", argv[1]);
usage(stderr, argv[0]);
return -1;
}
hstate_compile_source(&state, source);
hvm_module_save_to_file(state.mod, output_file);
} break;
}
arena_free(&a);
hstate_deinit(&state);
return 0;
}