return NULL;
}
-struct Atom *create_native_atom(Gc *gc, NativeFunction fun)
+struct Atom *create_native_atom(Gc *gc, NativeFunction fun, void *param)
{
struct Atom *atom = malloc(sizeof(struct Atom));
}
atom->type = ATOM_NATIVE;
- atom->fun = fun;
+ atom->native.fun = fun;
+ atom->native.param = param;
if (gc_add_expr(gc, atom_as_expr(atom)) < 0) {
goto error;
#define NUMBER(G, X) atom_as_expr(create_number_atom(G, X))
#define STRING(G, S) atom_as_expr(create_string_atom(G, S, NULL))
#define SYMBOL(G, S) atom_as_expr(create_symbol_atom(G, S, NULL))
-#define NATIVE(G, F) atom_as_expr(create_native_atom(G, F))
+#define NATIVE(G, F, P) atom_as_expr(create_native_atom(G, F, P))
#define CONS(G, CAR, CDR) cons_as_expr(create_cons(G, CAR, CDR))
#define NIL(G) SYMBOL(G, "nil")
struct Expr expr;
};
+
typedef struct EvalResult (*NativeFunction)(void *param, Gc *gc, struct Scope *scope, struct Expr args);
+struct Native
+{
+ NativeFunction fun;
+ void *param;
+};
+
enum AtomType
{
ATOM_SYMBOL = 0,
long int num; // ATOM_NUMBER
char *sym; // ATOM_SYMBOL
char *str; // ATOM_STRING
- NativeFunction fun; // ATOM_NATIVE
+ struct Native native; // ATOM_NATIVE
};
};
struct Atom *create_number_atom(Gc *gc, long int num);
struct Atom *create_string_atom(Gc *gc, const char *str, const char *str_end);
struct Atom *create_symbol_atom(Gc *gc, const char *sym, const char *sym_end);
-struct Atom *create_native_atom(Gc *gc, NativeFunction fun);
+struct Atom *create_native_atom(Gc *gc, NativeFunction fun, void *param);
void destroy_atom(struct Atom *atom);
void print_atom_as_sexpr(struct Atom *atom);
struct Expr callable,
struct Expr args) {
if (callable.type == EXPR_ATOM && callable.atom->type == ATOM_NATIVE) {
- return ((NativeFunction)callable.atom->fun)((void*)((long int) callable.atom->fun), gc, scope, args);
+ return ((NativeFunction)callable.atom->native.fun)(callable.atom->native.param, gc, scope, args);
}
return call_lambda(gc, scope, callable, args);
.expr = CONS(gc, NIL(gc), NIL(gc))
};
- set_scope_value(gc, &scope, SYMBOL(gc, "quit"), NATIVE(gc, quit));
+ set_scope_value(gc, &scope, SYMBOL(gc, "quit"), NATIVE(gc, quit, NULL));
while (true) {
printf("> ");