1 #include "system/stacktrace.h"
3 #include "ebisp/interpreter.h"
4 #include "ebisp/parser.h"
5 #include "ebisp/scope.h"
7 #include "game/level.h"
8 #include "game/level_script.h"
11 #include "system/line_stream.h"
12 #include "system/log.h"
13 #include "system/log_script.h"
14 #include "system/lt.h"
15 #include "system/nth_alloc.h"
16 #include "ui/console.h"
17 #include "broadcast.h"
26 Script *create_script_from_line_stream(LineStream *line_stream, Broadcast *broadcast)
28 trace_assert(line_stream);
35 Script *script = PUSH_LT(lt, nth_alloc(sizeof(Script)), free);
41 script->gc = PUSH_LT(lt, create_gc(), destroy_gc);
42 if (script->gc == NULL) {
46 script->scope = create_scope(script->gc);
48 load_std_library(script->gc, &script->scope);
49 load_log_library(script->gc, &script->scope);
50 broadcast_load_library(broadcast, script->gc, &script->scope);
53 sscanf(line_stream_next(line_stream), "%lu", &n);
55 char *source_code = NULL;
56 for (size_t i = 0; i < n; ++i) {
57 /* TODO(#466): maybe source_code should be constantly replaced in the Lt */
58 source_code = string_append(
60 line_stream_next(line_stream));
62 PUSH_LT(lt, source_code, free);
64 struct ParseResult parse_result =
65 read_all_exprs_from_string(
68 if (parse_result.is_error) {
69 log_fail("Parsing error: %s\n", parse_result.error_message);
73 struct EvalResult eval_result = eval(
77 SYMBOL(script->gc, "begin"),
79 if (eval_result.is_error) {
80 print_expr_as_sexpr(stderr, eval_result.expr);
85 gc_collect(script->gc, script->scope.expr);
87 free(RELEASE_LT(lt, source_code));
92 void destroy_script(Script *script)
95 RETURN_LT0(script->lt);
98 int script_eval(Script *script, const char *source_code)
100 trace_assert(script);
101 trace_assert(source_code);
103 struct ParseResult parse_result = read_expr_from_string(
106 if (parse_result.is_error) {
107 log_fail("Parsing error: %s\n", parse_result.error_message);
111 struct EvalResult eval_result = eval(
115 if (eval_result.is_error) {
116 log_fail("Evaluation error: ");
117 /* TODO(#521): Evalation error is prepended with `[FAIL]` at the end of the message */
118 /* TODO(#486): print_expr_as_sexpr could not be easily integrated with log_fail */
119 print_expr_as_sexpr(stderr, eval_result.expr);
124 gc_collect(script->gc, script->scope.expr);
129 bool script_has_scope_value(const Script *script, const char *name)
134 SYMBOL(script->gc, name)));