Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   Macros in Ruby (http://www.velocityreviews.com/forums/t815928-macros-in-ruby.html)

George Moschovitis 08-02-2004 09:14 AM

Macros in Ruby
 
Hello everyone,

one of the features of the LISP family of languages that is missing from
Ruby are macros. I think they are useful on a lot of occasions so
I would like to see Ruby support macros in the future.

However i think it is quite easy to emulate some form of macro
functionality in Ruby. Here is some simple code:

macros.rb:
----------

$__macros__ = {}
$__required__ = {}

module Kernel

alias_method :old_require, :require
def require(path)
return if $__required__[path]

source = open(path) { |f|
f.sysread(f.stat().size())
}

# parse macro
source.gsub!(/defmacro\s*\/(.*?)\/\s(.*?)endmacro/m) {
$__macros__[Regexp.new($1)] = $2 ; ""
}

# expand macros
$__macros__.each { |match, replace|
source.gsub!(match, replace)
}

$__required__[path] = true

eval(source)
end

end

require "test1.rb"
require "test2.rb"


test1.rb:
---------

defmacro /my_macro\((.*)\)/
begin
my_function(\1)
rescue => ex
puts ex
end
endmacro

# php style foreach
defmacro /foreach\s*\((.*)\s*as(.*)\)/
for \2 in \1
endmacro

def my_function(str)
puts str
end

class TestClass
def another_test
words = %w{ simple test }
foreach(words as word)
puts k
end
end
end


test2.rb:
---------

value = "Hello World!"
my_macro(value)

numbers = [1, 2, 3, 4]

foreach (numbers as i)
puts i
end

a = TestClass.new
a.another_test()



Once again i was suprised to find out that Ruby is so powerful!

However I would like to hear your opinion on this code. Is there any
problem I have overlooked? Is there a better implementation? Could this
be better designed?

If anyone finds it usefull I could create a small package with
doc+examples and upload it to RAA.


have fun,
George Moschovitis

--
www.navel.gr
www.joy.gr

jim@freeze.org 08-02-2004 03:45 PM

Re: Macros in Ruby
 
* George Moschovitis <gm@navel.gr> [2004-08-02 18:16:35 +0900]:

> Hello everyone,
>
> one of the features of the LISP family of languages that is missing from
> Ruby are macros. I think they are useful on a lot of occasions so
> I would like to see Ruby support macros in the future.
>
> However i think it is quite easy to emulate some form of macro
> functionality in Ruby. Here is some simple code:


I've heard Matz say that Ruby will not support macros.
They are too easily abused and can mutate the language.
Besides, you can achieve the same powerful affect using
blocks.

Jim



Joel VanderWerf 08-02-2004 04:50 PM

Re: Macros in Ruby
 
George Moschovitis wrote:
> Hello everyone,
>
> one of the features of the LISP family of languages that is missing from
> Ruby are macros. I think they are useful on a lot of occasions so
> I would like to see Ruby support macros in the future.


What you have defined are more like C macros, rather than LISP macros,
which are hygienic (they operate at the level of syntactic elements,
rather than characters).



George Moschovitis 08-02-2004 05:18 PM

Re: Macros in Ruby
 
> I've heard Matz say that Ruby will not support macros.
> They are too easily abused and can mutate the language.


I 've heard that, but macros ARE usefull. Take a look
at www.paulgraham.com for example.

> Besides, you can achieve the same powerful affect using
> blocks.


How can i use blocks to emulate macros ? I cannot understand
that.

George

--
www.navel.gr
www.joy.gr

George Moschovitis 08-02-2004 05:21 PM

Re: Macros in Ruby
 
Joel VanderWerf wrote:
> What you have defined are more like C macros, rather than LISP macros,
> which are hygienic (they operate at the level of syntactic elements,
> rather than characters).


You are of course right. But you can do still usefull things. And I
think it can be improved. Any ideas are welcome. Even better would be
support for macros in Ruby 1.9 :)

George.


--
www.navel.gr
www.joy.gr

James Britt 08-02-2004 05:29 PM

Re: Macros in Ruby
 
Joel VanderWerf wrote:
> George Moschovitis wrote:
>
>> Hello everyone,
>>
>> one of the features of the LISP family of languages that is missing from
>> Ruby are macros. I think they are useful on a lot of occasions so
>> I would like to see Ruby support macros in the future.

>
>
> What you have defined are more like C macros, rather than LISP macros,
> which are hygienic (they operate at the level of syntactic elements,
> rather than characters).


Would Ruby macros, in the Lisp sense, have to manipulate the AST?

And a question (ideally) for Matz, but comments from anyone else are of
course welcome:

Would true macros in Ruby be more prone to abuse than they are in Lisp?

Is there something different about Ruby such that what (supposedly)
works to such acclaim in Lisp would be inappropriate in Ruby?


Thanks,



James




Lennon Day-Reynolds 08-02-2004 05:45 PM

Re: Macros in Ruby
 
On Tue, 3 Aug 2004 02:29:34 +0900, James Britt
<jamesunderbarb@neurogami.com> wrote:
> Is there something different about Ruby such that what (supposedly)
> works to such acclaim in Lisp would be inappropriate in Ruby?


A: S-expressions, or the lack thereof in Ruby. Lisp code can always be
manipulated as a simple tree of cons cells; Ruby code, even if a
high-level programmatic interface existed to the AST, has a much
richer syntax, and therefore more complex structure.

Also, as has been mentioned, Ruby largely replaces macros with blocks
-- they allow delayed evaluation, specialized iteration and
pre/post-condition checks, etc. Many of the common macros you'll see
in Lisp are things like this:

(with-open-file "foo.dat" my-file (case (read-char my-file) ...))

[my apologies for the weird function names; it's been a couple of
years since I was actively developing any Lisp code]

Obviously, that example is quite handily supported by the more
Ruby-esque example below:

open("foo.dat") {|my_file| case my_file.getc ...}

Personally, I think macros could be an interesting tool for very
special cases, but they're not an essential feature for 90% of
developers. Now, exposing the AST with a high-level API could have
other benefits: code analysis and optimization, JIT compilers,
documentation generators, etc.

Lennon



Phil Tomson 08-02-2004 06:14 PM

Re: Macros in Ruby
 
In article <cel0la$2sto$1@ulysses.noc.ntua.gr>,
George Moschovitis <gm@navel.gr> wrote:
>Hello everyone,
>
>one of the features of the LISP family of languages that is missing from
>Ruby are macros. I think they are useful on a lot of occasions so
>I would like to see Ruby support macros in the future.
>

<code snipped>


>Once again i was suprised to find out that Ruby is so powerful!
>
>However I would like to hear your opinion on this code. Is there any
>problem I have overlooked? Is there a better implementation? Could this
>be better designed?
>
>If anyone finds it usefull I could create a small package with
>doc+examples and upload it to RAA.
>
>
>have fun,
>George Moschovitis


I've never used Lisp so I'm not too familiar with Lisp macros and I know
that Matz isn't too fond of macros. However, I can't help feeling that
you've created something quite cool here and I'd certainly like to see
more.



Please do post something on the RAA! Maybe you could put it up on
rubyforge?

Phil

Phil Tomson 08-02-2004 06:17 PM

Re: Macros in Ruby
 
In article <20040802154500.GB67163@freeze.org>, <jim@freeze.org> wrote:
>* George Moschovitis <gm@navel.gr> [2004-08-02 18:16:35 +0900]:
>
>> Hello everyone,
>>
>> one of the features of the LISP family of languages that is missing from
>> Ruby are macros. I think they are useful on a lot of occasions so
>> I would like to see Ruby support macros in the future.
>>
>> However i think it is quite easy to emulate some form of macro
>> functionality in Ruby. Here is some simple code:

>
>I've heard Matz say that Ruby will not support macros.


True, Matz has said that. But if we can do something Macro-like with a
module (as George has shown) then it's optional.

>They are too easily abused and can mutate the language.
>Besides, you can achieve the same powerful affect using
>blocks.
>


Not quite. Notice that George introduced totally new 'syntax' using his
'macros' - you can't do that with blocks.


Phil

Phil Tomson 08-02-2004 06:22 PM

Re: Macros in Ruby
 
In article <5d4c6124040802104572179afa@mail.gmail.com>,
Lennon Day-Reynolds <rcoder@gmail.com> wrote:
>
>Personally, I think macros could be an interesting tool for very
>special cases, but they're not an essential feature for 90% of
>developers. Now, exposing the AST with a high-level API could have
>other benefits: code analysis and optimization, JIT compilers,
>documentation generators, etc.
>


....and code translators, and lots of other cool things.

Yes, this would be great to have.

Phil


All times are GMT. The time now is 07:46 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.