[Hecl-devel] Dynamically creating code
Brought to you by:
davidw
From: David W. <dav...@gm...> - 2008-05-01 19:18:37
|
Hi, Hecl has a tricky problem: how to create code dynamically. With Lisp, it's easy to just fiddle with a few parenthesis and tick' marks' and ,commas, and you can do pretty much anything. With Tcl, you just create some strings and keep running your code until you get the quoting just right. Since Hecl has ObjectThings, which can't be represented as strings, anything that passes through a string representation gets killed. So this means that, for instance this code has problems: java java.lang.System sys java java.io.PrintStream ps set prints [sys -field err] proc out {str} " $prints println \$str " As might be possible in Tcl. I've been playing around with different solutions to this for a while, and here's what I currently have: proc out {str} [code { $prints println $str } [list prints $prints]] Although... I'm not entirely happy with that, as it's a bit hacky: /* Get the code stanzas - CodeThing.get transforms the argument into parsed code. */ CodeThing code = CodeThing.get(interp, argv[1]); Vector stanzas = code.getStanzas(); Vector varnames = ListThing.get(argv[2]); /* Set command. */ Thing set = new Thing("set"); for (Enumeration e = varnames.elements(); e.hasMoreElements();) { Thing key = (Thing)e.nextElement(); Thing val = (Thing)e.nextElement(); Thing[] newargv = { set, key, val }; stanzas.insertElementAt(new Stanza(null, newargv, 0), 0); } return new Thing(code); It creates a 'set varname varval' stanza at the beginning of the code block. Thoughts? Comments? -- David N. Welton http://www.welton.it/davidw/ http://www.dedasys.com/ |