]> git.lizzy.rs Git - nothing.git/blob - src/game/level/script.c
Make Lt dynamic again
[nothing.git] / src / game / level / script.c
1 #include "system/stacktrace.h"
2 #include "ebisp/gc.h"
3 #include "ebisp/interpreter.h"
4 #include "ebisp/parser.h"
5 #include "ebisp/scope.h"
6 #include "ebisp/std.h"
7 #include "game/level.h"
8 #include "script.h"
9 #include "system/str.h"
10 #include "system/line_stream.h"
11 #include "system/log.h"
12 #include "system/log_script.h"
13 #include "system/lt.h"
14 #include "system/nth_alloc.h"
15 #include "ui/console.h"
16 #include "broadcast.h"
17
18 struct Script
19 {
20     Lt *lt;
21     Gc *gc;
22     struct Scope scope;
23 };
24
25 Script *create_script_from_line_stream(LineStream *line_stream, Broadcast *broadcast)
26 {
27     trace_assert(line_stream);
28
29     Lt *lt = create_lt();
30
31     Script *script = PUSH_LT(lt, nth_calloc(1, sizeof(Script)), free);
32     if (script == NULL) {
33         RETURN_LT(lt, NULL);
34     }
35     script->lt = lt;
36
37     script->gc = PUSH_LT(lt, create_gc(), destroy_gc);
38     if (script->gc == NULL) {
39         RETURN_LT(lt, NULL);
40     }
41
42     script->scope = create_scope(script->gc);
43
44     load_std_library(script->gc, &script->scope);
45     load_log_library(script->gc, &script->scope);
46     struct EvalResult eval_result = broadcast_load_library(broadcast, script->gc, &script->scope);
47     if (eval_result.is_error) {
48         print_expr_as_sexpr(stderr, eval_result.expr);
49         log_fail("\n");
50         RETURN_LT(lt, NULL);
51     }
52
53     size_t n = 0;
54     sscanf(line_stream_next(line_stream), "%lu", &n);
55
56     char *source_code = NULL;
57     for (size_t i = 0; i < n; ++i) {
58         /* TODO(#466): maybe source_code should be constantly replaced in the Lt */
59         source_code = string_append(
60             source_code,
61             line_stream_next(line_stream));
62     }
63     PUSH_LT(lt, source_code, free);
64
65     struct ParseResult parse_result =
66         read_all_exprs_from_string(
67             script->gc,
68             source_code);
69     if (parse_result.is_error) {
70         log_fail("Parsing error: %s\n", parse_result.error_message);
71         RETURN_LT(lt, NULL);
72     }
73
74     eval_result = eval(
75         script->gc,
76         &script->scope,
77         CONS(script->gc,
78              SYMBOL(script->gc, "begin"),
79              parse_result.expr));
80     if (eval_result.is_error) {
81         print_expr_as_sexpr(stderr, eval_result.expr);
82         log_fail("\n");
83         RETURN_LT(lt, NULL);
84     }
85
86     gc_collect(script->gc, script->scope.expr);
87
88     free(RELEASE_LT(lt, source_code));
89
90     return script;
91 }
92
93 void destroy_script(Script *script)
94 {
95     trace_assert(script);
96     RETURN_LT0(script->lt);
97 }
98
99 int script_eval(Script *script, const char *source_code)
100 {
101     trace_assert(script);
102     trace_assert(source_code);
103
104     struct ParseResult parse_result = read_expr_from_string(
105         script->gc,
106         source_code);
107     if (parse_result.is_error) {
108         log_fail("Parsing error: %s\n", parse_result.error_message);
109         return -1;
110     }
111
112     struct EvalResult eval_result = eval(
113         script->gc,
114         &script->scope,
115         parse_result.expr);
116     if (eval_result.is_error) {
117         log_fail("Evaluation error: ");
118         /* TODO(#521): Evalation error is prepended with `[FAIL]` at the end of the message */
119         /* TODO(#486): print_expr_as_sexpr could not be easily integrated with log_fail */
120         print_expr_as_sexpr(stderr, eval_result.expr);
121         log_fail("\n");
122         return -1;
123     }
124
125     gc_collect(script->gc, script->scope.expr);
126
127     return 0;
128 }
129
130 bool script_has_scope_value(const Script *script, const char *name)
131 {
132     return !nil_p(
133         get_scope_value(
134             &script->scope,
135             SYMBOL(script->gc, name)));
136 }