Code Comments
Programming Forum and web based access to our favorite programming groups.Hi, I need to implement a kind of template processor in which simple constructs like this: (define-activity :name activity-name :subactivities subactivity-list) get translated into a bunch of replacement s-expressions. I have a simple define-syntax macro definition that seems to work on forms when they appear in the same Scheme code as the define-syntax form or are entered interactively after the macro is defined. I thought that by using (load ...) in the scope of a (let-syntax ...) form that includes the same macro definition would apply the macro to the expressions loaded from the file, but I get diagnostics that "define-activity" is undefined. What is the general technique for implementing template-driven data -> data transformations (as opposed to form -> form transformations). I'll admit, as if it's necessary, that this is my first foray into Scheme and the last time I did any Lisp it was Common Lisp and quite a long time ago. So while I understand Lisp basics, I'm pretty naive at this point with Scheme. Thanks. Randall Schulz
Post Follow-up to this messageAt Thursday 27 March 2008 12:49 in comp.lang.scheme Randall Schulz wrote: > Hi, > > I need to implement a kind of template processor in which simple > constructs like this: > > (define-activity > :name activity-name > :subactivities subactivity-list) > > > get translated into a bunch of replacement s-expressions. > > I have a simple define-syntax macro definition that seems to work on > forms when they appear in the same Scheme code as the define-syntax form > or are entered interactively after the macro is defined. > > I thought that by using (load ...) in the scope of a (let-syntax ...) > form that includes the same macro definition would apply the macro to > the expressions loaded from the file, but I get diagnostics > that "define-activity" is undefined. > > What is the general technique for implementing template-driven data -> > data transformations (as opposed to form -> form transformations). I came up with one thing that kind of works: read the s-expressions in my target language and use (eval ...) to get the macro-expanded. Of course, this means the input could execute arbitrary forms, so it's surely not an ideal approach. Is there a portable or standards-compliant way to determine if a symbol is bound to a macro within a given environment? > ... Randall Schulz
Post Follow-up to this messageOn Mar 27, 2:49 pm, Randall Schulz <sinkh...@tarpit.net> wrote: > I need to implement a kind of template processor in which simple > constructs like this: > > (define-activity > :name activity-name > :subactivities subactivity-list) Is this an option? (define-activity (name activity-name) (subactivities subactivity-list)) > I thought that by using (load ...) in the scope of a (let-syntax ...) form > that includes the same macro definition would apply the macro to the > expressions loaded from the file, but I get diagnostics > that "define-activity" is undefined. 'eval' only works in the top-level environment. That is why it works for you interactively, but not in bound expressions. > What is the general technique for implementing template-driven data -> > data transformations (as opposed to form -> form transformations). Here is what I did: http://list.cs.brown.edu/pipermail/...ust/020352.html > I'll admit, as if it's necessary, that this is my first foray into Scheme > and the last time I did any Lisp it was Common Lisp and quite a long time > ago. So while I understand Lisp basics, I'm pretty naive at this point > with Scheme. You should ping the PLT discussion list, those guys are the experts! http://www.plt-scheme.org/maillist/
Post Follow-up to this messageGriff, Thanks. At Saturday 29 March 2008 15:04 in comp.lang.scheme Griff wrote: > On Mar 27, 2:49 pm, Randall Schulz <sinkh...@tarpit.net> wrote: > > > > Here is what I did: > > http://list.cs.brown.edu/pipermail/...ust/020352.html I'm not sure how I see that addresses my requirements. > > You should ping the PLT discussion list, those guys are the experts! > > http://www.plt-scheme.org/maillist/ All well and good, but I need a solution that is R5RS-portable, since I'm using SISC embedded in my Java application. I'd be willing to consider using a non-portable solution if it works in a Java-embeddable Scheme implementation. In the interim, I've discovered Andrew Wright's pattern-matching code and am currently exploring it as a mechanism to perform the transformations I require. Randall Schulz
Post Follow-up to this messageHi Randall, Somehow I remembered that thread being a lot more detailed! The gist of the discussion, in or out of that thread, was that it would be easier to use a pattern matching library than macros to do the kind of data transformation that I wanted, which is that to which you allude. I ended up using PLT's "match.ss" which is a port of Wright and Duba's library: http://download.plt-scheme.org/doc/... chap_27 In the meantime I see you've come to the same conclusion.
Post Follow-up to this messageOn Sat, 29 Mar 2008, Randall Schulz wrote: > > All well and good, but I need a solution that is R5RS-portable, since I'm > using SISC embedded in my Java application. I'd be willing to consider I'm not quite sure what you are trying to do, perhaps some examples would help, but in SISC, you can use "include" which includes text from a file directly into your code. It sounds like that might solve your problem, although I don't quite know what the problem is. Also, "Griff" mentioned that "eval" only works globally. In strict R5RS, thats correct, but in SISC it also works locally when you don't specify the environment argument for eval, which I guess is what you have already found out.
Post Follow-up to this messageOn Sun, 30 Mar 2008, Kjetil S. Matheussen wrote: > > Also, "Griff" mentioned that "eval" only works globally. In strict R5RS, > thats correct, but in SISC it also works locally when you don't specify > the environment argument for eval, which I guess is what you have > already found out. > Sorry, thats wrong. This works though: (let ((a (lambda () 5))) (eval `(,a))) which is a nice feature.
Post Follow-up to this messageAt Sunday 30 March 2008 04:07 in comp.lang.scheme Kjetil S. Matheussen wrote: > On Sat, 29 Mar 2008, Randall Schulz wrote: > > > I'm not quite sure what you are trying to do, perhaps some examples > would help, but in SISC, you can use "include" which > includes text from a file directly into your code. It sounds > like that might solve your problem, although I don't quite know > what the problem is. Well, it's entirely possible that my problem is in my approach (caused or exacerbated by my naivete)... SISC's (include ...) escaped my attention, probably because it's described in the "Modules and Libraries" chapter of "SISC for Seasoned Schemers." Here's a simple toy example of the sort of transformations I need to write: Macro: (define-syntax define-object (syntax-rules (:name :properties) ((define-object :name object-name) `(object object-name)) ((define-object :name object-name :properties object-property ...) `((object object-name) (object-property object-name) ...)) )) Input: (define-object :name wrench-1 :properties Wrench Durable) (define-object :name wrench-2 :properties Wrench Durable CarbonFiber) Output: (((object wrench-1) (Wrench wrench-1) (Durable wrench-1)) ((object wrench-2) (Wrench wrench-2) (Durable wrench-2) (CarbonFiber wrench-1))) Right now I'm experimenting with both Wright's "match.scm" and native Scheme (R5RS) macros. So far, it looks like Scheme macros are going to be easier to write (more transparent w.r.t. to the forms it recognizes, in part), while Wright's matcher is pretty clearly more powerful in terms of the pattern recognizers and transformers it can produce but writing them looks like it's going to tougher and that the patterns they recognize are themselves a bit hard to see in its various "match..." forms. Because I'll eventually need to turn this facility over to end users, the more straightforward encoding of the transformations provided by Scheme macros is a very big plus. > Also, "Griff" mentioned that "eval" only works globally. In strict R5RS, > thats correct, but in SISC it also works locally when you don't specify > the environment argument for eval, which I guess is what you have > already found out. While I illustrated my toy example above with define-syntax, I'm actually using let-syntax and then reading s-expressions from a file and applying eval using (interaction-environment) to provide eval's environment. This works, but as I mentioned earlier, will require me to explicitly restrict the s-expressions I accept lest the input become a backdoor to arbitrary execution code, for either nefarious or simply inadvertent havoc. Thanks for the help, guys. Randall Schulz
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.