swissChili | f3e7f18 | 2021-04-20 13:57:22 -0700 | [diff] [blame] | 1 | #include "compiler.h" |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 2 | #include "gc.h" |
swissChili | c0acce4 | 2022-07-31 13:38:17 -0700 | [diff] [blame] | 3 | #include "lisp.h" |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 4 | #include "plat/plat.h" |
swissChili | 9d428a8 | 2022-08-01 20:47:40 -0700 | [diff] [blame] | 5 | #include "lib/std.h" |
6 | #include <stdlib.h> | ||||
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 7 | |
swissChili | 53472e8 | 2021-05-08 16:06:32 -0700 | [diff] [blame] | 8 | int main(int argc, char **argv) |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 9 | { |
swissChili | 8056031 | 2022-07-31 21:05:47 -0700 | [diff] [blame] | 10 | gc_push_segment(NULL, 0); |
11 | |||||
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 12 | struct environment *env = NULL; |
13 | struct error compile_error; | ||||
swissChili | 9d428a8 | 2022-08-01 20:47:40 -0700 | [diff] [blame] | 14 | |
15 | if (argc < 2) | ||||
swissChili | 923b536 | 2021-05-09 20:31:43 -0700 | [diff] [blame] | 16 | { |
swissChili | 9d428a8 | 2022-08-01 20:47:40 -0700 | [diff] [blame] | 17 | env = malloc(sizeof(struct environment)); |
18 | env->first = NULL; | ||||
19 | env->first_loaded = NULL; | ||||
20 | |||||
21 | if (!IS_OKAY((compile_error = load_std(env)))) | ||||
22 | { | ||||
23 | ereport(compile_error); | ||||
24 | goto done; | ||||
25 | } | ||||
26 | |||||
27 | if (!load_library(env, "repl")) | ||||
28 | goto done; | ||||
swissChili | c0acce4 | 2022-07-31 13:38:17 -0700 | [diff] [blame] | 29 | } |
swissChili | 9d428a8 | 2022-08-01 20:47:40 -0700 | [diff] [blame] | 30 | else |
31 | { | ||||
32 | if (!IS_OKAY((compile_error = compile_file(argv[1], &env)))) | ||||
33 | { | ||||
34 | ereport(compile_error); | ||||
35 | goto done; | ||||
36 | } | ||||
37 | } | ||||
38 | |||||
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 39 | struct function *lisp_main_f = find_function(env, "main"); |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 40 | |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 41 | if (lisp_main_f) |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 42 | { |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 43 | value_t (*lisp_main)() = lisp_main_f->def0; |
swissChili | 8056031 | 2022-07-31 21:05:47 -0700 | [diff] [blame] | 44 | gc_prepare_call(0); |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 45 | lisp_main(); |
46 | } | ||||
47 | else | ||||
48 | { | ||||
49 | fprintf(stderr, "No MAIN function defined! nothing to do\n"); | ||||
50 | } | ||||
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 51 | |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 52 | done: |
swissChili | b8fd471 | 2021-06-23 15:32:04 -0700 | [diff] [blame] | 53 | free_all(); |
swissChili | 6d02af4 | 2021-08-05 19:49:01 -0700 | [diff] [blame] | 54 | if (env) |
55 | del_env(env); | ||||
swissChili | 16156be | 2022-07-31 21:14:02 -0700 | [diff] [blame] | 56 | gc_pop_segment(); |
swissChili | 7a6f5eb | 2021-04-13 16:46:02 -0700 | [diff] [blame] | 57 | } |