From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: prj@po.cwru.edu (Paul Jarc) Newsgroups: gmane.lisp.guile.devel Subject: Scheme-defined smobs Date: Mon, 03 Nov 2003 12:39:38 -0500 Organization: What did you have in mind? A short, blunt, human pyramid? Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Message-ID: NNTP-Posting-Host: deer.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="Boundary_(ID_wj503XL7fXiZodyuxfeKkQ)" X-Trace: sea.gmane.org 1067881287 7429 80.91.224.253 (3 Nov 2003 17:41:27 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Mon, 3 Nov 2003 17:41:27 +0000 (UTC) Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Mon Nov 03 18:41:25 2003 Return-path: Original-Received: from monty-python.gnu.org ([199.232.76.173]) by deer.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 1AGihg-0003mp-00 for ; Mon, 03 Nov 2003 18:41:25 +0100 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.24) id 1AGihM-0003rz-0j for guile-devel@m.gmane.org; Mon, 03 Nov 2003 12:41:04 -0500 Original-Received: from list by monty-python.gnu.org with tmda-scanned (Exim 4.24) id 1AGih2-0003rO-Cc for guile-devel@gnu.org; Mon, 03 Nov 2003 12:40:44 -0500 Original-Received: from mail by monty-python.gnu.org with spam-scanned (Exim 4.24) id 1AGigU-0003aD-L0 for guile-devel@gnu.org; Mon, 03 Nov 2003 12:40:42 -0500 Original-Received: from [129.22.104.63] (helo=harris.CNS.CWRU.Edu) by monty-python.gnu.org with esmtp (Exim 4.24) id 1AGigS-0003OJ-GY for guile-devel@gnu.org; Mon, 03 Nov 2003 12:40:08 -0500 Original-Received: from conversion-daemon.smtp-a.cwru.edu by smtp-a.cwru.edu (iPlanet Messaging Server 5.2 HotFix 1.14 (built Mar 18 2003)) id <0HNS00B01EDCKB@smtp-a.cwru.edu> for guile-devel@gnu.org; Mon, 03 Nov 2003 12:39:38 -0500 (EST) Original-Received: from multivac.cwru.edu (multivac.ITS.CWRU.Edu [129.22.114.26]) by smtp-a.cwru.edu (iPlanet Messaging Server 5.2 HotFix 1.14 (built Mar 18 2003)) with SMTP id <0HNS00BNIEE2FQ@smtp-a.cwru.edu> for guile-devel@gnu.org; Mon, 03 Nov 2003 12:39:38 -0500 (EST) Original-Received: (qmail 1279 invoked by uid 500); Mon, 03 Nov 2003 17:40:00 +0000 Original-To: guile-devel@gnu.org Mail-followup-to: guile-devel@gnu.org Mail-Copies-To: nobody User-Agent: Gnus/5.1003 (Gnus v5.10.3) Emacs/21.3 (gnu/linux) Original-Lines: 30 X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.2 Precedence: list List-Id: Developers list for Guile, the GNU extensibility library List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: main.gmane.org gmane.lisp.guile.devel:2968 X-Report-Spam: http://spam.gmane.org/gmane.lisp.guile.devel:2968 --Boundary_(ID_wj503XL7fXiZodyuxfeKkQ) Content-type: TEXT/PLAIN Content-transfer-encoding: 7BIT It seems pointless at first, but I think it might be useful. The attached file (tested as an extension with 1.6.4) provides Scheme bindings for defining smobs - i.e., the "print" and "equalp" functions can be defined in Scheme. This might be useful for adding user-defined cases to equal?. But I admit my real motivation for writing this was to enable this gruesome hack: guile> (define (printer smob-data port state) (system smob-data)) guile> (define generator (make-smob-type "smob-name" printer #f)) guile> (define ls (generator "ls")) guile> (ls) "ls" guile> (ls "ls -a") guile> (ls) "ls -a" guile> ls ... which shows me the output of "ls -a". So I can use Guile as a shell with a bit less typing. Is there interest in adding this to libguile? (Regardless, I think the three snarfing macros at the top (along with corresponding SCM_SMOB_FREE, SCM_SMOB_PRINT, and SCM_SMOB_EQUALP) would be useful additions to smob.h, though perhaps the name SCM_ SMOB_APPLY is too similar to the existing SCM_SMOB_APPLY_*.) paul --Boundary_(ID_wj503XL7fXiZodyuxfeKkQ) Content-type: TEXT/PLAIN; NAME=dynsmob.c Content-transfer-encoding: 7BIT Content-disposition: attachment; filename=dynsmob.c #include #define SCM_SMOB(c_name, scheme_name, size) \ SCM_SNARF_HERE(static scm_t_bits scm_##c_name##_tag) \ SCM_SNARF_INIT(scm_##c_name##_tag=scm_make_smob_type((scheme_name), size);) #define SCM_SMOB_MARK(c_name, arg) \ SCM_SNARF_HERE(static SCM scm_##c_name##_mark(SCM arg)) \ SCM_SNARF_INIT(scm_set_smob_mark(scm_##c_name##_tag, scm_##c_name##_mark);) #define SCM_SMOB_APPLY(c_name, args, req, opt, rest) \ SCM_SNARF_HERE(static SCM scm_##c_name##_apply args) \ SCM_SNARF_INIT(scm_set_smob_apply(scm_##c_name##_tag, scm_##c_name##_apply, \ req, opt, rest);) /* define one static smob, to be used as a generator */ SCM_SMOB(dynsmob_static, "dynsmob", 0); SCM_SMOB_MARK(dynsmob_static, s_smob) { scm_gc_mark(SCM_CELL_OBJECT_2(s_smob)); return SCM_CELL_OBJECT_3(s_smob); } SCM_SMOB_APPLY(dynsmob_static, (SCM s_smob, SCM s_data), 1, 0, 0) { SCM_RETURN_NEWSMOB2(SCM_CELL_WORD_1(s_smob), s_smob, SCM_UNPACK(s_data)); } /* these are for Scheme-defined smobs */ static SCM mark_dynsmob(SCM s_smob) { SCM const s_type=SCM_CELL_OBJECT_1(s_smob); SCM const s_data=SCM_CELL_OBJECT_2(s_smob); scm_gc_mark(s_type); return s_data; } static int print_dynsmob(SCM s_smob, SCM s_port, scm_print_state* pstate) { SCM const s_type =SCM_CELL_OBJECT_1(s_smob); SCM const s_printer=SCM_CELL_OBJECT_2(s_type); SCM const s_data =SCM_CELL_OBJECT_2(s_smob); (void)pstate; /* silence "unused parameter" warning */ scm_call_3(s_printer, s_data, s_port, SCM_UNSPECIFIED/*pstate*/); return 1; } static SCM equalp_dynsmob(SCM s_smob1, SCM s_smob2) { SCM const s_type =SCM_CELL_OBJECT_1(s_smob1); SCM const s_equalp=SCM_CELL_OBJECT_3(s_type); SCM const s_data1 =SCM_CELL_OBJECT_2(s_smob1); SCM const s_data2 =SCM_CELL_OBJECT_2(s_smob2); return scm_call_2(s_equalp, s_data1, s_data2); } static SCM apply_dynsmob(SCM s_smob, SCM s_newdata) { if (SCM_UNBNDP(s_newdata)) { SCM const s_data=SCM_CELL_OBJECT_2(s_smob); return s_data; } SCM_SET_CELL_OBJECT_2(s_smob, s_newdata); return SCM_UNSPECIFIED; } #define FUNC_NAME s_scm_dynsmob_make_smob_type SCM_DEFINE(scm_dynsmob_make_smob_type, "make-smob-type", 3, 0, 0, (SCM s_name, SCM s_printer, SCM s_equalp), "Define a new smob type and return a generator for it.") { scm_t_bits tag; SCM_VALIDATE_STRING(SCM_ARG1, s_name); if (!SCM_FALSEP(s_printer)) SCM_VALIDATE_PROC(SCM_ARG2, s_printer); if (!SCM_FALSEP(s_equalp )) SCM_VALIDATE_PROC(SCM_ARG3, s_equalp); SCM_STRING_COERCE_0TERMINATION_X(s_name); tag=scm_make_smob_type(SCM_STRING_CHARS(s_name), 0); scm_set_smob_mark(tag, mark_dynsmob); if (!SCM_FALSEP(s_printer)) scm_set_smob_print (tag, print_dynsmob); if (!SCM_FALSEP(s_equalp )) scm_set_smob_equalp(tag, equalp_dynsmob); scm_set_smob_apply(tag, apply_dynsmob, 0, 1, 0); SCM_RETURN_NEWSMOB3(scm_dynsmob_static_tag, tag, SCM_UNPACK(s_printer), SCM_UNPACK(s_equalp)); } #undef FUNC_NAME void dynsmob__load__init(void) { #ifndef SCM_MAGIC_SNARFER #include "dynsmob.x" #endif } --Boundary_(ID_wj503XL7fXiZodyuxfeKkQ) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Guile-devel mailing list Guile-devel@gnu.org http://mail.gnu.org/mailman/listinfo/guile-devel --Boundary_(ID_wj503XL7fXiZodyuxfeKkQ)--