From d2e883cf32ea9fa82f33768104bc5993bfc60a04 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Mon, 30 Nov 2015 06:11:09 -0800 Subject: func-get-name calculates a name for methods. * eval.c (func_get_name): Use try to use new method_name function, if unable to get name from the lexical or global environment for functions. * struct.c (meth_s): New symbol variable. (struct_init): Initialize meth_s variable. (method_name): New function. * struct.h (method_name): Declared. * txr.1: Re-documented func-get-name. --- struct.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'struct.c') diff --git a/struct.c b/struct.c index f7768756..05a1a372 100644 --- a/struct.c +++ b/struct.c @@ -74,7 +74,7 @@ struct struct_inst { val slot[1]; }; -val struct_type_s; +val struct_type_s, meth_s; static cnum struct_id_counter; static val struct_type_hash; @@ -95,6 +95,7 @@ void struct_init(void) protect(&struct_type_hash, &slot_hash, &struct_type_finalize_f, convert(val *, 0)); struct_type_s = intern(lit("struct-type"), user_package); + meth_s = intern(lit("meth"), user_package); struct_type_hash = make_hash(nil, nil, nil); slot_hash = make_hash(nil, nil, t); struct_type_finalize_f = func_n1(struct_type_finalize); @@ -1044,6 +1045,29 @@ static val struct_inst_equalsub(val obj) return nil; } +val method_name(val fun) +{ + val sth_iter = hash_begin(struct_type_hash); + val sth_cell; + + while ((sth_cell = hash_next(sth_iter))) { + val sym = car(sth_cell); + val stype = cdr(sth_cell); + val sl_iter; + struct struct_type *st = coerce(struct struct_type *, stype->co.handle); + + for (sl_iter = st->slots; sl_iter; sl_iter = cdr(sl_iter)) { + val slot = car(sl_iter); + loc ptr = lookup_static_slot(stype, st, slot); + + if (!nullocp(ptr) && deref(ptr) == fun) + return list(meth_s, sym, slot, nao); + } + } + + return nil; +} + static_def(struct cobj_ops struct_type_ops = cobj_ops_init(eq, struct_type_print, struct_type_destroy, struct_type_mark, cobj_hash_op)) -- cgit v1.2.3