]> git.lizzy.rs Git - nothing.git/blob - src/game/level/script.c
(#639) Introduce rigid_bodies_collide_with_itself
[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 "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     if (lt == NULL) {
31         return NULL;
32     }
33
34     Script *script = PUSH_LT(lt, nth_alloc(sizeof(Script)), free);
35     if (script == NULL) {
36         RETURN_LT(lt, NULL);
37     }
38     script->lt = lt;
39
40     script->gc = PUSH_LT(lt, create_gc(), destroy_gc);
41     if (script->gc == NULL) {
42         RETURN_LT(lt, NULL);
43     }
44
45     script->scope = create_scope(script->gc);
46
47     load_std_library(script->gc, &script->scope);
48     load_log_library(script->gc, &script->scope);
49     broadcast_load_library(broadcast, script->gc, &script->scope);
50
51     size_t n = 0;
52     sscanf(line_stream_next(line_stream), "%lu", &n);
53
54     char *source_code = NULL;
55     for (size_t i = 0; i < n; ++i) {
56         /* TODO(#466): maybe source_code should be constantly replaced in the Lt */
57         source_code = string_append(
58             source_code,
59             line_stream_next(line_stream));
60     }
61     PUSH_LT(lt, source_code, free);
62
63     struct ParseResult parse_result =
64         read_all_exprs_from_string(
65             script->gc,
66             source_code);
67     if (parse_result.is_error) {
68         log_fail("Parsing error: %s\n", parse_result.error_message);
69         RETURN_LT(lt, NULL);
70     }
71
72     struct EvalResult eval_result = eval(
73         script->gc,
74         &script->scope,
75         CONS(script->gc,
76              SYMBOL(script->gc, "begin"),
77              parse_result.expr));
78     if (eval_result.is_error) {
79         print_expr_as_sexpr(stderr, eval_result.expr);
80         log_fail("\n");
81         RETURN_LT(lt, NULL);
82     }
83
84     gc_collect(script->gc, script->scope.expr);
85
86     free(RELEASE_LT(lt, source_code));
87
88     return script;
89 }
90
91 void destroy_script(Script *script)
92 {
93     trace_assert(script);
94     RETURN_LT0(script->lt);
95 }
96
97 int script_eval(Script *script, const char *source_code)
98 {
99     trace_assert(script);
100     trace_assert(source_code);
101
102     struct ParseResult parse_result = read_expr_from_string(
103         script->gc,
104         source_code);
105     if (parse_result.is_error) {
106         log_fail("Parsing error: %s\n", parse_result.error_message);
107         return -1;
108     }
109
110     struct EvalResult eval_result = eval(
111         script->gc,
112         &script->scope,
113         parse_result.expr);
114     if (eval_result.is_error) {
115         log_fail("Evaluation error: ");
116         /* TODO(#521): Evalation error is prepended with `[FAIL]` at the end of the message */
117         /* TODO(#486): print_expr_as_sexpr could not be easily integrated with log_fail */
118         print_expr_as_sexpr(stderr, eval_result.expr);
119         log_fail("\n");
120         return -1;
121     }
122
123     gc_collect(script->gc, script->scope.expr);
124
125     return 0;
126 }
127
128 bool script_has_scope_value(const Script *script, const char *name)
129 {
130     return !nil_p(
131         get_scope_value(
132             &script->scope,
133             SYMBOL(script->gc, name)));
134 }