forked from gnoack/fn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fn.c
81 lines (73 loc) · 2.21 KB
/
fn.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
#include "runtime/cons.h" // make_cons.
#include "runtime/deserialize.h" // deserialize_from_bootstrap_file.
#include "runtime/eval.h" // register_globally.
#include "runtime/gc.h" // gc serialization functions.
#include "runtime/runtime.h"
#include "runtime/strings.h" // make_string.
#include <stdbool.h> // bool.
#include <string.h> // strcmp.
bool deserialize_from_bootstrap_file_arg = false;
bool deserialize_from_image_arg = false;
bool exit_arg = false;
bool serialize_to_image_arg = false;
const char* file_to_load = NULL;
// Returns the first index of the remaining arguments,
// argc if no argument is given.
int parse_args(int argc, char* argv[]) {
int i;
for (i=1; i<argc; i++) {
if (strcmp(argv[i], "-s") == 0) {
serialize_to_image_arg = true; continue;
}
if (strcmp(argv[i], "-S") == 0) {
deserialize_from_image_arg = true; continue;
}
if (strcmp(argv[i], "-B") == 0) {
deserialize_from_bootstrap_file_arg = true; continue;
}
if (strcmp(argv[i], "-x") == 0) {
exit_arg = true; continue;
}
file_to_load = argv[i];
return i + 1;
}
return argc;
}
int main(int argc, char* argv[]) {
int remainder_idx = parse_args(argc, argv);
fn_runtime_init();
if (deserialize_from_image_arg) {
gc_deserialize_from_file("fn.img");
} else if (deserialize_from_bootstrap_file_arg) {
deserialize_from_bootstrap_file("bootstrap.out");
} else {
fn_runtime_init_lisp_decls();
}
if (serialize_to_image_arg) {
gc_serialize_to_file("fn.img");
}
if (exit_arg) {
exit(0);
}
oop args = NIL;
int x;
for (x = argc - 1; x >= remainder_idx; x--) {
args = make_cons(make_string(argv[x]), args);
}
register_globally("*args*", args);
if (file_to_load != NULL) {
fn_load_file(file_to_load);
} else {
puts("FN " __DATE__ ".");
puts("Usage:\n"
" ./fn FILENAME\n"
" ./fn -2 -S -x\n\n"
"-2 load all main modules twice (compiling on the second go)\n"
"-s save memory image after loading modules\n"
"-S load memory image instead of loading modules\n"
"-B load bootstrap file instead of loading modules\n"
"-x quit without executing tests\n");
exit(0);
}
return 0;
}