#include "ebisp/scope.h"
#include "ebisp/std.h"
#include "game/level.h"
-#include "game/level_script.h"
#include "script.h"
-#include "str.h"
+#include "system/str.h"
#include "system/line_stream.h"
#include "system/log.h"
#include "system/log_script.h"
Lt *lt;
Gc *gc;
struct Scope scope;
+ const char *source_code;
};
-Script *create_script_from_line_stream(LineStream *line_stream, Broadcast *broadcast)
+static Script *create_script(Broadcast *broadcast, const char *source_code)
{
- trace_assert(line_stream);
+ trace_assert(source_code);
Lt *lt = create_lt();
- if (lt == NULL) {
- return NULL;
- }
- Script *script = PUSH_LT(lt, nth_alloc(sizeof(Script)), free);
+ Script *script = PUSH_LT(lt, nth_calloc(1, sizeof(Script)), free);
if (script == NULL) {
RETURN_LT(lt, NULL);
}
}
script->scope = create_scope(script->gc);
-
load_std_library(script->gc, &script->scope);
load_log_library(script->gc, &script->scope);
broadcast_load_library(broadcast, script->gc, &script->scope);
- size_t n = 0;
- sscanf(line_stream_next(line_stream), "%lu", &n);
-
- char *source_code = NULL;
- for (size_t i = 0; i < n; ++i) {
- /* TODO(#466): maybe source_code should be constantly replaced in the Lt */
- source_code = string_append(
- source_code,
- line_stream_next(line_stream));
+ script->source_code = PUSH_LT(
+ lt,
+ string_duplicate(source_code, NULL),
+ free);
+ if (script->source_code == NULL) {
+ RETURN_LT(lt, NULL);
}
- PUSH_LT(lt, source_code, free);
struct ParseResult parse_result =
read_all_exprs_from_string(
gc_collect(script->gc, script->scope.expr);
- free(RELEASE_LT(lt, source_code));
-
return script;
}
+Script *create_script_from_string(Broadcast *broadcast, const char *source_code)
+{
+ return create_script(broadcast, string_duplicate(source_code, NULL));
+}
+
+Script *create_script_from_line_stream(LineStream *line_stream, Broadcast *broadcast)
+{
+ trace_assert(line_stream);
+
+ const char *line = line_stream_next(line_stream);
+ if (line == NULL) {
+ return NULL;
+ }
+
+ size_t n = 0;
+ if (sscanf(line, "%lu", &n) == EOF) {
+ return NULL;
+ }
+
+ const char *source_code = line_stream_collect_n_lines(line_stream, n);
+ if (source_code == NULL) {
+ return NULL;
+ }
+
+ return create_script(broadcast, source_code);
+}
+
void destroy_script(Script *script)
{
trace_assert(script);
RETURN_LT0(script->lt);
}
-int script_eval(Script *script, const char *source_code)
+const char *script_source_code(const Script *script)
{
- trace_assert(script);
- trace_assert(source_code);
+ return script->source_code;
+}
- struct ParseResult parse_result = read_expr_from_string(
- script->gc,
- source_code);
- if (parse_result.is_error) {
- log_fail("Parsing error: %s\n", parse_result.error_message);
- return -1;
- }
+int script_eval(Script *script, struct Expr expr)
+{
+ trace_assert(script);
struct EvalResult eval_result = eval(
script->gc,
&script->scope,
- parse_result.expr);
+ expr);
if (eval_result.is_error) {
log_fail("Evaluation error: ");
/* TODO(#521): Evalation error is prepended with `[FAIL]` at the end of the message */
&script->scope,
SYMBOL(script->gc, name)));
}
+
+Gc *script_gc(const Script *script)
+{
+ return script->gc;
+}