src/system/log.c
src/system/nth_alloc.h
src/system/nth_alloc.c
+ src/game/level_script.c
+ src/game/level_script.h
)
add_executable(repl
#include "ebisp/parser.h"
#include "ebisp/scope.h"
#include "game/level.h"
+#include "game/level_script.h"
#include "script.h"
#include "str.h"
#include "system/line_stream.h"
struct Scope scope;
};
-static struct EvalResult
-hide_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_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);
}
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));
+ load_level_library(script->gc, &script->scope, level);
struct ParseResult parse_result =
read_all_exprs_from_string(
--- /dev/null
+#include <assert.h>
+
+#include "ebisp/gc.h"
+#include "ebisp/interpreter.h"
+#include "ebisp/scope.h"
+#include "game/level/player/rigid_rect.h"
+#include "level.h"
+#include "level_script.h"
+#include "system/log.h"
+
+struct EvalResult
+hide_goal(void *param, Gc *gc, struct Scope *scope, struct Expr args)
+{
+ assert(param);
+ assert(gc);
+ assert(scope);
+
+ /* TODO: Manually parsing args of native functions is tedious. */
+ 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));
+}
+
+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));
+}
+
+struct EvalResult rect_apply_force(void *param, Gc *gc, struct Scope *scope, struct Expr args)
+{
+ assert(gc);
+ assert(scope);
+ assert(param);
+
+ /* TODO(#401): rect_apply_force doesn't sanitize it's input */
+
+ Level *level = (Level*) param;
+ const char *rect_id = CAR(args).atom->str;
+ struct Expr vector_force_expr = CAR(CDR(args));
+ const float force_x = (float) CAR(vector_force_expr).atom->num;
+ const float force_y = (float) CDR(vector_force_expr).atom->num;
+
+ print_expr_as_sexpr(stdout, args); printf("\n");
+
+ Rigid_rect *rigid_rect = level_rigid_rect(level, rect_id);
+ if (rigid_rect != NULL) {
+ log_info("Found rect `%s`\n", rect_id);
+ log_info("Applying force (%f, %f)\n", force_x, force_y);
+ rigid_rect_apply_force(rigid_rect, vec(force_x, force_y));
+ } else {
+ log_fail("Couldn't find rigid_rect `%s`\n", rect_id);
+ }
+
+ return eval_success(NIL(gc));
+}
+
+void load_level_library(Gc *gc, struct Scope *scope, Level *level)
+{
+ set_scope_value(
+ gc,
+ scope,
+ SYMBOL(gc, "rect-apply-force"),
+ NATIVE(gc, rect_apply_force, level));
+ set_scope_value(
+ gc,
+ scope,
+ SYMBOL(gc, "hide-goal"),
+ NATIVE(gc, hide_goal, level));
+ set_scope_value(
+ gc,
+ scope,
+ SYMBOL(gc, "show-goal"),
+ NATIVE(gc, show_goal, level));
+}
--- /dev/null
+#ifndef LEVEL_SCRIPT_H_
+#define LEVEL_SCRIPT_H_
+
+#include "ebisp/expr.h"
+
+typedef struct Gc Gc;
+struct Scope;
+typedef struct Level Level;
+
+struct EvalResult
+rect_apply_force(void *param, Gc *gc, struct Scope *scope, struct Expr args);
+
+struct EvalResult
+hide_goal(void *param, Gc *gc, struct Scope *scope, struct Expr args);
+
+struct EvalResult
+show_goal(void *param, Gc *gc, struct Scope *scope, struct Expr args);
+
+void load_level_library(Gc *gc, struct Scope *scope, Level *level);
+
+#endif // LEVEL_SCRIPT_H_
#include "ebisp/scope.h"
#include "game/level.h"
#include "game/level/player/rigid_rect.h"
+#include "game/level_script.h"
#include "sdl/renderer.h"
#include "system/log.h"
#include "system/lt.h"
/* TODO(#358): Console does not support copy, cut, paste operations */
/* TODO(#503): hide-goal and show-goal are not accessible in Console */
-struct EvalResult rect_apply_force(void *param, Gc *gc, struct Scope *scope, struct Expr args)
-{
- assert(gc);
- assert(scope);
- assert(param);
-
- /* TODO(#401): rect_apply_force doesn't sanitize it's input */
-
- Level *level = (Level*) param;
- const char *rect_id = CAR(args).atom->str;
- struct Expr vector_force_expr = CAR(CDR(args));
- const float force_x = (float) CAR(vector_force_expr).atom->num;
- const float force_y = (float) CDR(vector_force_expr).atom->num;
-
- print_expr_as_sexpr(stdout, args); printf("\n");
-
- Rigid_rect *rigid_rect = level_rigid_rect(level, rect_id);
- if (rigid_rect != NULL) {
- log_info("Found rect `%s`\n", rect_id);
- log_info("Applying force (%f, %f)\n", force_x, force_y);
- rigid_rect_apply_force(rigid_rect, vec(force_x, force_y));
- } else {
- log_fail("Couldn't find rigid_rect `%s`\n", rect_id);
- }
-
- return eval_success(NIL(gc));
-}
-
Console *create_console(Level *level,
const Sprite_font *font)
{
console->scope.expr = CONS(console->gc,
NIL(console->gc),
NIL(console->gc));
- set_scope_value(
- console->gc,
- &console->scope,
- SYMBOL(console->gc, "rect-apply-force"),
- NATIVE(console->gc, rect_apply_force, level));
+
+ load_level_library(console->gc, &console->scope, level);
console->edit_field = PUSH_LT(
lt,
void console_slide_down(Console *console);
-struct EvalResult rect_apply_force(void *param, Gc *gc, struct Scope *scope, struct Expr args);
-
#endif // CONSOLE_H_