4 #include "ebisp/interpreter.h"
5 #include "ebisp/parser.h"
6 #include "ebisp/scope.h"
9 #include "system/error.h"
10 #include "system/line_stream.h"
11 #include "system/log.h"
12 #include "system/lt.h"
13 #include "system/nth_alloc.h"
14 #include "ui/console.h"
23 static struct EvalResult
24 hide_goal(void *param, Gc *gc, struct Scope *scope, struct Expr args)
31 /* TODO(#497): hide_goal is not implemented */
33 return eval_success(NIL(gc));
36 static struct EvalResult
37 show_goal(void *param, Gc *gc, struct Scope *scope, struct Expr args)
44 /* TODO(#498): show_goal is not implemented */
46 return eval_success(NIL(gc));
49 Script *create_script_from_line_stream(LineStream *line_stream, Level *level)
58 Script *script = PUSH_LT(lt, nth_alloc(sizeof(Script)), free);
60 throw_error(ERROR_TYPE_LIBC);
65 script->gc = PUSH_LT(lt, create_gc(), destroy_gc);
66 if (script->gc == NULL) {
70 script->scope = create_scope(script->gc);
73 sscanf(line_stream_next(line_stream), "%lu", &n);
75 char *source_code = NULL;
76 for (size_t i = 0; i < n; ++i) {
77 /* TODO(#466): maybe source_code should be constantly replaced in the Lt */
78 source_code = string_append(
80 line_stream_next(line_stream));
82 PUSH_LT(lt, source_code, free);
87 SYMBOL(script->gc, "rect-apply-force"),
88 NATIVE(script->gc, rect_apply_force, level));
92 SYMBOL(script->gc, "hide-goal"),
93 NATIVE(script->gc, hide_goal, level));
97 SYMBOL(script->gc, "show-goal"),
98 NATIVE(script->gc, show_goal, level));
100 struct ParseResult parse_result =
101 read_all_exprs_from_string(
104 if (parse_result.is_error) {
105 log_fail("Parsing error: %s\n", parse_result.error_message);
109 struct EvalResult eval_result = eval(
113 SYMBOL(script->gc, "begin"),
115 if (eval_result.is_error) {
116 print_expr_as_sexpr(stderr, eval_result.expr);
121 gc_collect(script->gc, script->scope.expr);
123 free(RELEASE_LT(lt, source_code));
128 void destroy_script(Script *script)
131 RETURN_LT0(script->lt);
134 int script_eval(Script *script, const char *source_code)
139 struct ParseResult parse_result = read_expr_from_string(
142 if (parse_result.is_error) {
143 log_fail("Parsing error: %s\n", parse_result.error_message);
147 struct EvalResult eval_result = eval(
151 if (eval_result.is_error) {
152 log_fail("Evaluation error: ");
153 /* TODO(#486): print_expr_as_sexpr could not be easily integrated with log_fail */
154 print_expr_as_sexpr(stderr, eval_result.expr);
159 gc_collect(script->gc, script->scope.expr);
164 bool script_has_scope_value(const Script *script, const char *name)
169 SYMBOL(script->gc, name)));