]> git.lizzy.rs Git - nothing.git/blobdiff - src/game/level/script.c
(#608) Introduce (send) function
[nothing.git] / src / game / level / script.c
index de99b149bfe5e983d9420d92183d14dd48cfb12d..07e91122e5c2cbb4c4a86680c50d549ccee4b103 100644 (file)
@@ -1,18 +1,21 @@
-#include <assert.h>
+#include "system/stacktrace.h"
 
 #include "ebisp/gc.h"
 #include "ebisp/interpreter.h"
 #include "ebisp/parser.h"
 #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/error.h"
 #include "system/line_stream.h"
 #include "system/log.h"
+#include "system/log_script.h"
 #include "system/lt.h"
 #include "system/nth_alloc.h"
 #include "ui/console.h"
+#include "game_script.h"
 
 struct Script
 {
@@ -21,69 +24,9 @@ struct Script
     struct Scope scope;
 };
 
-static struct EvalResult
-hide_goal(void *param, Gc *gc, struct Scope *scope, struct Expr args)
+Script *create_script_from_line_stream(LineStream *line_stream, Game *game)
 {
-    assert(param);
-    assert(gc);
-    assert(scope);
-
-    if (!list_p(args)) {
-        return wrong_argument_type(gc, "listp", args);
-    }
-
-    if (length_of_list(args) != 1) {
-        return eval_failure(
-            CONS(gc,
-                 SYMBOL(gc, "wrong-number-of-arguments"),
-                 NUMBER(gc, length_of_list(args))));
-    }
-
-    if (!string_p(CAR(args))) {
-        return wrong_argument_type(gc, "stringp", args);
-    }
-
-    const char * const goal_id = CAR(args).atom->str;
-    Level * const level = (Level*)param;
-
-    level_hide_goal(level, goal_id);
-
-    return eval_success(NIL(gc));
-}
-
-static struct EvalResult
-show_goal(void *param, Gc *gc, struct Scope *scope, struct Expr args)
-{
-    assert(param);
-    assert(gc);
-    assert(scope);
-
-    if (!list_p(args)) {
-        return wrong_argument_type(gc, "listp", args);
-    }
-
-    if (length_of_list(args) != 1) {
-        return eval_failure(
-            CONS(gc,
-                 SYMBOL(gc, "wrong-number-of-arguments"),
-                 NUMBER(gc, length_of_list(args))));
-    }
-
-    if (!string_p(CAR(args))) {
-        return wrong_argument_type(gc, "stringp", args);
-    }
-
-    const char * const goal_id = CAR(args).atom->str;
-    Level * const level = (Level*)param;
-
-    level_show_goal(level, goal_id);
-
-    return eval_success(NIL(gc));
-}
-
-Script *create_script_from_line_stream(LineStream *line_stream, Level *level)
-{
-    assert(line_stream);
+    trace_assert(line_stream);
 
     Lt *lt = create_lt();
     if (lt == NULL) {
@@ -92,7 +35,6 @@ Script *create_script_from_line_stream(LineStream *line_stream, Level *level)
 
     Script *script = PUSH_LT(lt, nth_alloc(sizeof(Script)), free);
     if (script == NULL) {
-        throw_error(ERROR_TYPE_LIBC);
         RETURN_LT(lt, NULL);
     }
     script->lt = lt;
@@ -104,6 +46,10 @@ Script *create_script_from_line_stream(LineStream *line_stream, Level *level)
 
     script->scope = create_scope(script->gc);
 
+    load_std_library(script->gc, &script->scope);
+    load_log_library(script->gc, &script->scope);
+    load_game_library(script->gc, &script->scope, game);
+
     size_t n = 0;
     sscanf(line_stream_next(line_stream), "%lu", &n);
 
@@ -116,22 +62,6 @@ Script *create_script_from_line_stream(LineStream *line_stream, Level *level)
     }
     PUSH_LT(lt, source_code, free);
 
-    set_scope_value(
-        script->gc,
-        &script->scope,
-        SYMBOL(script->gc, "rect-apply-force"),
-        NATIVE(script->gc, rect_apply_force, level));
-    set_scope_value(
-        script->gc,
-        &script->scope,
-        SYMBOL(script->gc, "hide-goal"),
-        NATIVE(script->gc, hide_goal, level));
-    set_scope_value(
-        script->gc,
-        &script->scope,
-        SYMBOL(script->gc, "show-goal"),
-        NATIVE(script->gc, show_goal, level));
-
     struct ParseResult parse_result =
         read_all_exprs_from_string(
             script->gc,
@@ -162,14 +92,14 @@ Script *create_script_from_line_stream(LineStream *line_stream, Level *level)
 
 void destroy_script(Script *script)
 {
-    assert(script);
+    trace_assert(script);
     RETURN_LT0(script->lt);
 }
 
 int script_eval(Script *script, const char *source_code)
 {
-    assert(script);
-    assert(source_code);
+    trace_assert(script);
+    trace_assert(source_code);
 
     struct ParseResult parse_result = read_expr_from_string(
         script->gc,
@@ -185,6 +115,7 @@ int script_eval(Script *script, const char *source_code)
         parse_result.expr);
     if (eval_result.is_error) {
         log_fail("Evaluation error: ");
+        /* TODO(#521): Evalation error is prepended with `[FAIL]` at the end of the message */
         /* TODO(#486): print_expr_as_sexpr could not be easily integrated with log_fail */
         print_expr_as_sexpr(stderr, eval_result.expr);
         log_fail("\n");