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"
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"
25 static Script *create_script(Broadcast *broadcast, const char *source_code)
27 trace_assert(source_code);
31 Script *script = PUSH_LT(lt, nth_calloc(1, sizeof(Script)), free);
37 script->gc = PUSH_LT(lt, create_gc(), destroy_gc);
38 if (script->gc == NULL) {
42 script->scope = create_scope(script->gc);
43 load_std_library(script->gc, &script->scope);
44 load_log_library(script->gc, &script->scope);
45 broadcast_load_library(broadcast, script->gc, &script->scope);
47 struct ParseResult parse_result =
48 read_all_exprs_from_string(
51 if (parse_result.is_error) {
52 log_fail("Parsing error: %s\n", parse_result.error_message);
56 struct EvalResult eval_result = eval(
60 SYMBOL(script->gc, "begin"),
62 if (eval_result.is_error) {
63 print_expr_as_sexpr(stderr, eval_result.expr);
68 gc_collect(script->gc, script->scope.expr);
73 Script *create_script_from_string(Broadcast *broadcast, const char *source_code)
75 return create_script(broadcast, string_duplicate(source_code, NULL));
78 Script *create_script_from_line_stream(LineStream *line_stream, Broadcast *broadcast)
80 trace_assert(line_stream);
82 const char *source_code = line_stream_collect_until_end(line_stream);
83 if (source_code == NULL) {
87 return create_script(broadcast, source_code);
90 void destroy_script(Script *script)
93 RETURN_LT0(script->lt);
96 int script_eval(Script *script, struct Expr expr)
100 struct EvalResult eval_result = eval(
104 if (eval_result.is_error) {
105 log_fail("Evaluation error: ");
106 /* TODO(#521): Evalation error is prepended with `[FAIL]` at the end of the message */
107 /* TODO(#486): print_expr_as_sexpr could not be easily integrated with log_fail */
108 print_expr_as_sexpr(stderr, eval_result.expr);
113 gc_collect(script->gc, script->scope.expr);
118 bool script_has_scope_value(const Script *script, const char *name)
123 SYMBOL(script->gc, name)));
126 Gc *script_gc(const Script *script)