From: Ryan Pavlik Date: 2003-04-13T01:04:53+09:00 Subject: Re: The "!" and "?" characters. On Sat, 12 Apr 2003 20:50:58 +0900 matz@ruby-lang.org (Yukihiro Matsumoto) wrote: > (b) it would make syntax vary program to program, which would make > programs hard to read. > > And (b) is also the reason _not_ to have macros in Ruby. I wonder at this... Lisp has macros, and they don't make it harder to read. Actually, Lisp-style macros applied to ruby would be somewhat more like a simple eval, where you might go (in pseodoruby): defmacro c_for(st1, st2, st3) #{st1} while(#{st2}) yield #{st3} end end Now you can go: c_for(i = 0, i < 200, i += 1) { ... } The advantage to this over eval() is probably apparent: * The macro can be mostly preparsed, making execution faster than eval * The statement import allows for a much more natual syntax * The above is _more_ readable, in both creation and use, than an equivalent eval. Would it be possible for one to abuse defmacro in ruby? Of course. It's possible to abuse any language feature: blocks, continuations, exceptions, catch/throw, etc. But it would be pretty useful, too. This doesn't address the reader aspect. Doing that, and allowing one to add operators, would probably fall under (a), although there may be some brilliant scheme to avoid it. (OTOH, with byte compilation that might be a different story. ;) There is a single parser I know of that allows one to add such syntax, and that's the Lout typesetting package, where you can define a function with @Left and @Right parameters, and function names can be anything (including "operators"), so you can: def = @Left { x } @Right { y } { x equals y } foo = bar % outputs "foo equals bar"... although you could % of course make it do more interesting things ;) With perhaps some generalization and limitation, this could be made to work with yacc. Of course, it would require macros in order to accomplish some things like assignment: defmacro (var) += (var2) #{var} = #{var} + #{var2} end Or Everyone's Favorite Missing Operator ( ;) ): defmacro (var)++ #{var} += 1 end Anyhow, I'm kinda wandering off here, but thinking about this, it just seems kinda neat. later, -- Ryan Pavlik "My plans are *always* practical! It's the laws of physics that get in the way of my success." - 8BT